Import

Description

Imports another build file into the current project.

On execution it will read another Ant file into the same Project. This means that it basically works like the Entity Includes as explained in the Ant FAQ, as if the imported file was contained in the importing file, minus the top <project> tag.

The import task may only be used as a top-level task. This means that it may not be used in a target.

There are two further functional aspects that pertain to this task and that are not possible with entity includes:

Target overriding

If a target in the main file is also present in at least one of the imported files, the one from the main file takes precedence.

So if I import for example a docsbuild.xml file named builddocs, that contains a "docs" target, I can redefine it in my main buildfile and that is the one that will be called. This makes it easy to keep the same target name, so that the overriding target is still called by any other targets--in either the main or imported buildfile(s)--for which it is a dependency, with a different implementation. The target from docsbuild.xml is made available by the name "builddocs.docs". This enables the new implementation to call the old target, thus enhancing it with tasks called before or after it.

Special Properties

Imported files are treated as they are present in the main buildfile. This makes it easy to understand, but it makes it impossible for them to reference files and resources relative to their path. Because of this, for every imported file, Ant adds a property that contains the path to the imported buildfile. With this path, the imported buildfile can keep resources and be able to reference them relative to its position.

So if I import for example a docsbuild.xml file named builddocs, I can get its path as ant.file.builddocs, similarly to the ant.file property of the main buildfile.

Note that "builddocs" is not the filename, but the name attribute present in the imported project tag.

If import file does not have a name attribute, the ant.file.projectname property will not be set.

Resolving files against the imported file

Suppose your main build file called importing.xml imports a build file imported.xml, located anywhere on the file system, and imported.xml reads a set of properties from imported.properties:

<!-- importing.xml -->
<project name="importing" basedir="." default="...">
  <import file="${path_to_imported}/imported.xml"/>
</project>

<!-- imported.xml -->
<project name="imported" basedir="." default="...">
  <property file="imported.properties"/>
</project>

This snippet however will resolve imported.properties against the basedir of importing.xml, because the basedir of imported.xml is ignored by Ant. The right way to use imported.properties is:

<!-- imported.xml -->
<project name="imported" basedir="." default="...">
  <dirname property="imported.basedir" file="${ant.file.imported}"/>
  <property file="${imported.basedir}/imported.properties"/>
</project>

As explained above ${ant.file.imported} stores the path of the build script, that defines the project called imported, (in short it stores the path to imported.xml) and <dirname> takes its directory. This technique also allows imported.xml to be used as a standalone file (without being imported in other project).

Parameters

Attribute Description Required
file The file to import. If this is a relative file name, the file name will be resolved relative to the importing file. Note, this is unlike most other ant file attributes, where relative files are resolved relative to /Users/antoine/dev/asf/ant-core. Yes
optional If true, do not stop the build if the file does not exist, default is false. No

Examples

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

Imports targets from the common-targets.xml file that is in a parent directory.

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

Imports the project defined by the property deploy-platform