Ant Tips

Import Property File

* Import property file.

<project name="testproj" default="init">
  <dirname property="testproj.dir" file="${ant.file.testproj}"/>
  <property file="${testproj.dir}/build.properties"/>

* Here is good discussion on how to use multiple lines in property files. Basically, you use ‘\n\’ in property files to separate lines.

multiline=\nThis is a \n\
multiple \n\
line text.

Import Another Build File

* You can import another build file and directly call targets contained within the imported file.

<import file="build_misc.xml"/>

Escape Characters

Use the dollar sign to escape characters.

        <echo>$${my.zip.file}: ${goldengate.zip.file}</echo>

Check for OS

<condition property="is.win">
  <os family="windows"/>
</condition>
 
<condition property="is.unix">
  <os family="unix"/>
</condition>
 
<target name="do.something" depends="do.win, do.unix"/>
 
<target name="do.win" if="is.win">
  <echo message="This is Windows."/>
</target>
 
<target name="do.unix" if="is.unix">
  <echo message="This is Unix."/>
</target>

Find Hostname

<property environment="env"/>
<exec executable="hostname" 
    os="SunOS" <!-- use osfamily after Ant 1.7 -->
    failifexecutionfails="false" 
    outputproperty="env.COMPUTERNAME"/>
<property name="deploy.to" value="${env.COMPUTERNAME}"/>

Import

* Child build file to be imported:

<project name="childbuild" default="childtarget">
  <dirname property="childbuild.dir" file="${ant.file.childbuild}"/>
  <property file="${childbuild.dir}/childbuild.properties"/>
  <target name="childtarget">
    <echo>
$${ant.project.name}: ${ant.project.name}
$${ant.file.childbuild}: ${ant.file.childbuild}
$${prop1}: ${prop1}
$${prop2}: ${prop2}
    </echo>
  </target>
</project>

* Property file to be imported into child build file:

prop1=ThisIsPropertyOne
prop2=ThisIsPropertyTwo

* Main build file overriding child target:

project name="mainbuild" default="childtarget">
  <import file="childbuild.xml"/>
  <target name="childtarget"
    depends="childbuild.childtarget">
    <echo>
Overriding childtarget.
$${prop1}: ${prop1}
$${prop2}: ${prop2}
    </echo>
  </target>
</project>

* Call child target from main build file:

C:\TestAnt\ant childtarget
 
Buildfile: build.xml
 
childbuild.childtarget:
     [echo]
     [echo] ${ant.project.name}: mainbuild
     [echo] ${ant.file.childbuild}: C:\TestAnt\childbuild.xml
     [echo] ${prop1}: ThisIsPropertyOne
     [echo] ${prop2}: ThisIsPropertyTwo
     [echo]
 
childtarget:
     [echo]
     [echo] Overriding childtarget.
     [echo] ${prop1}: ThisIsPropertyOne
     [echo] ${prop2}: ThisIsPropertyTwo
     [echo]
 
BUILD SUCCESSFUL
Total time: 0 seconds

* List all targets
ant -p list all targets

This entry was posted in ant. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.