FileDocCategorySizeDatePackage
Basename.javaAPI DocApache Ant 1.703635Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs

Basename

public class Basename extends org.apache.tools.ant.Task
Sets a property to the base name of a specified file, optionally minus a suffix. This task can accept the following attributes:
  • file
  • property
  • suffix
The file and property attributes are required. The suffix attribute can be specified either with or without the ".", and the result will be the same (ie., the returned file name will be minus the .suffix).

When this task executes, it will set the specified property to the value of the last element in the specified file. If file is a directory, the basename will be the last directory element. If file is a full-path filename, the basename will be the simple file name. If a suffix is specified, and the specified file ends in that suffix, the basename will be the simple file name without the suffix.

since
Ant 1.5
ant.task
category="property"

Fields Summary
private File
file
private String
property
private String
suffix
Constructors Summary
Methods Summary
public voidexecute()
do the work

throws
BuildException if required attributes are not supplied property and attribute are required attributes

        if (property == null) {
            throw new BuildException("property attribute required", getLocation());
        }
        if (file == null) {
            throw new BuildException("file attribute required", getLocation());
        }
        String value = file.getName();
        if (suffix != null && value.endsWith(suffix)) {
            // if the suffix does not starts with a '.' and the
            // char preceding the suffix is a '.', we assume the user
            // wants to remove the '.' as well (see docs)
            int pos = value.length() - suffix.length();
            if (pos > 0 && suffix.charAt(0) != '."
                && value.charAt(pos - 1) == '.") {
                pos--;
            }
            value = value.substring(0, pos);
        }
        getProject().setNewProperty(property, value);
    
public voidsetFile(java.io.File file)
file or directory to get base name from

param
file file or directory to get base name from

        this.file = file;
    
public voidsetProperty(java.lang.String property)
Property to set base name to.

param
property name of property

        this.property  = property;
    
public voidsetSuffix(java.lang.String suffix)
Optional suffix to remove from base name.

param
suffix suffix to remove from base name

        this.suffix = suffix;