Apache Ant Import Task

Import tasks is used to import a build file into the current project. The imported files are parsed by using ProjectHelper. The ProjectHelper is actually responsible for parsing the imported files.

The import task is a top level task so it can't be used inside target element. There are two functional aspects which are related to this task.

Target Overriding

If a target in the current file is also available into the imported file, the current file's target will get precedence.

Special Properties

Imported files are local to the current main project. To distinguish between imported and local files, Ant adds a property which contains path to the imported build file.

Note: If the imported file does not contain name attribute, it property will not set.

Apache Ant Import Task Attributes

AttributeDescriptionRequired
fileThe file to import.Yes
optionalIf true, do not stop the build if the file does not exist.No
asSpecifies the prefix prepended to the target names.No
prefixSeparatorA separator to be used between the prefix and the target name.No

Apache Ant Import Task Example

The below code imports targets from the common-targets.xml file located into parent directory.

It imports a project

It imports target from the targets.xml file which is inside the common directory.





Latest Courses