ImportTask.javaAPI DocApache Ant 1.705272Wed Dec 13 06:16:20 GMT


public class ImportTask extends
Task to import another build file into the current project.

It must be 'top level'. On execution it will read another Ant file into the same Project.

Important: we have not finalized how relative file references will be resolved in deep/complex build hierarchies - such as what happens when an imported file imports another file. Use absolute references for enhanced build file stability, especially in the imported files.


<import file="../common-targets.xml"/>

Import targets from a file in a parent directory.

<import file="${deploy-platform}.xml"/>

Import the project defined by the property deploy-platform.


Fields Summary
private String
private boolean
private static final
Constructors Summary
Methods Summary
public voidexecute()
This relies on the task order model.

        if (file == null) {
            throw new BuildException("import requires file attribute");
        if (getOwningTarget() == null
            || !"".equals(getOwningTarget().getName())) {
            throw new BuildException("import only allowed as a top-level task");

        ProjectHelper helper =
                (ProjectHelper) getProject().

        if (helper == null) {
            // this happens if the projecthelper was not registered with the project.
            throw new BuildException("import requires support in ProjectHelper");

        Vector importStack = helper.getImportStack();

        if (importStack.size() == 0) {
            // this happens if ant is used with a project
            // helper that doesn't set the import.
            throw new BuildException("import requires support in ProjectHelper");

        if (getLocation() == null || getLocation().getFileName() == null) {
            throw new BuildException("Unable to get location of import task");

        File buildFile = new File(getLocation().getFileName()).getAbsoluteFile();

        // Paths are relative to the build file they're imported from,
        // *not* the current directory (same as entity includes).

        File buildFileParent = new File(buildFile.getParent());
        File importedFile = FILE_UTILS.resolveFile(buildFileParent, file);

        getProject().log("Importing file " + importedFile + " from "
                         + buildFile.getAbsolutePath(), Project.MSG_VERBOSE);

        if (!importedFile.exists()) {
            String message =
                "Cannot find " + file + " imported from "
                + buildFile.getAbsolutePath();
            if (optional) {
                getProject().log(message, Project.MSG_VERBOSE);
            } else {
                throw new BuildException(message);

        if (importStack.contains(importedFile)) {
                "Skipped already imported file:\n   "
                + importedFile + "\n", Project.MSG_VERBOSE);

        try {
            helper.parse(getProject(), importedFile);
        } catch (BuildException ex) {
            throw ProjectHelper.addLocationToBuildException(
                ex, getLocation());
public voidsetFile(java.lang.String file)
the name of the file to import. How relative paths are resolved is still in flux: use absolute paths for safety.

file the name of the file

        // I don't think we can use File - different rules
        // for relative paths.
        this.file = file;
public voidsetOptional(boolean optional)
sets the optional attribute

optional if true ignore files that are not present, default is false

        this.optional = optional;