AntImportTask

HomePage | RecentChanges | Preferences

Working with the Ant 'import' task.

References

A simple test in a single directory

Files

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

</project>

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

</project>

# imported.properties
name=Wendy

Output

Works as advertised

E:\java\ant-include\import>ant -f imported.xml
Buildfile: imported.xml

init:
     [echo] name = Wendy

BUILD SUCCESSFUL
Total time: 0 seconds
E:\java\ant-include\import>ant -f importing.xml
Buildfile: importing.xml

init:
     [echo] name = Wendy

BUILD SUCCESSFUL
Total time: 0 seconds
E:\java\ant-include\import>


Now including a file from the parent directory

Files

No change to imported.xml

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

</project>

In importing.xml, changed path to imported.xml (added ../)

<!-- dirname/importing.xml -->
<project name="importing" basedir="." default="init">
  
  <import file="../imported.xml"/>
  
  <target name="init" >
    <echo  message="name = ${name}"/>
  </target>

</project>

Slightly more complex properties

# imported.properties
first = Wendy
last = Smoak
name=${first} ${last}

Output

Still works as advertised...

 Directory of E:\java\ant-include\subdir

07/24/2005  04:30 AM    <DIR>          .
07/24/2005  04:30 AM    <DIR>          ..
07/24/2005  04:34 AM    <DIR>          core
07/24/2005  04:23 AM                35 imported.properties
07/24/2005  04:19 AM               307 imported.xml
               2 File(s)            342 bytes
               3 Dir(s)  19,986,845,696 bytes free

E:\java\ant-include\subdir>ant -f imported.xml
Buildfile: imported.xml

init:
     [echo] name = Wendy Smoak

BUILD SUCCESSFUL
Total time: 0 seconds
E:\java\ant-include\subdir>cd core

E:\java\ant-include\subdir\core>ant -f importing.xml
Buildfile: importing.xml

init:
     [echo] name = Wendy Smoak

BUILD SUCCESSFUL
Total time: 0 seconds
E:\java\ant-include\subdir\core>


More complex properties

Files

<!-- imported.xml -->
<project name="imported" basedir="." default="init">
  
  <!-- importing properties -->
  <dirname property="shale.home" file="${ant.file.imported}"/>
  <property file="${shale.home}/build.properties"/>
  
  <!-- setting a property directly in the imported file -->
  <property name="testprop" value="somevalue" />

  <!-- avoiding making the user specify root.dir -->  
  <dirname property="root.dir" file="${ant.file}"/>
  
  <target name="init">
    <echo  message="name = ${name}"/>
  </target>

</project>

<!-- dirname/importing.xml -->
<project name="importing" basedir="." default="init">
  
  <import file="../imported.xml"/>
  
  <target name="init" >
    <echo  message="name = ${name}"/>
    <echo  message="root.dir = ${root.dir}"/>
    <echo  message="testprop = ${testprop}"/>
  </target>

</project>

# imported.properties
first = Wendy
last = Smoak
name=${first} ${last}

Output

E:\java\ant-include\subdir\core>ant -f importing.xml
Buildfile: importing.xml

init:
     [echo] name = Wendy Smoak
     [echo] root.dir = E:\java\ant-include\subdir\core
     [echo] testprop = somevalue

BUILD SUCCESSFUL
Total time: 0 seconds
E:\java\ant-include\subdir\core>

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 24, 2005 7:03 am by WendySmoak (diff)
Search: