java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow Ant arrow How to use Import task
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3936
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use Import task E-mail
User Rating: / 8
PoorBest 

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
  • special properties

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.

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 ${basedir}. 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

Source: Apache Ant


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.