ant Notes


Links

Collection of great links to ant tutorials, docs, types, etc.


Setting up ant

If you're confused about environment variables or other issues on Linux or Windows, read these steps carefully as I tell you enough across all steps to perform what's needed on both platforms although you might be learning in a subsequent step how to do something on your platform in an earlier one. (Have I confused you more?)

  1. Go to the ant Binary Distributions and download the package that corresponds to your OS.
  2.  
  3. If on Windows, take care to extract the zip close to the root because of long path names sometimes too long for Windows.
  4.  
  5. Unzip or tar -xf to an appropriate installation point; I like to use my private filesystem: /home/russ/tools/apache-ant-1.8.1 or its Windows equivalent.
  6.  
  7. Create ANT_HOME to point to where you installed. On Windows use Start -> Control Panel -> System -> Advanced system settings -> Environment Variables -> User variables. Add this new variable as something like C:\Users\russ\bin\apache-ant-1.8.1.
  8.  
  9. Ensure your JAVA_HOME point to the right place to find the JRE, for example, on Windows,
    	C:\Users\russ\dev\workspaces\workspace\Fun>dir %JAVA_HOME%\bin\java.exe
    must work.
  10.  
  11. Ensure also that ant's bin subdirectory be on your search path:
    	~/dev/workspaces/workspace/Fun > set $PATH="$PATH:$ANT_HOME/bin"
    On Windows, you'll be executing ant.bat on that path.
  12.  
  13. Do a trial run with ant somewhere:
    	C:\Users\russ\dev\workspaces\workspace\Fun>ant -version
    	Apache Ant version 1.8.1 compiled on April 30 2010

ant built-in properties

These are hard to find for some reason.

 
basedir the absolute path of the project's basedir (as set with the basedir attribute of ).
ant.file the absolute path of the buildfile.
ant.version the version of ant
ant.project.name the name of the project that is currently executing; it is set in the name attribute of .
ant.java.version the JVM version ant detected; currently it can hold the values "1.2", "1.3", "1.4" and "1.5".
ant.home home directory of ant
java.version JRE version
java.vendor JRE vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version JVM specification version
java.vm.specification.vendor JVM specification vendor
java.vm.specification.name JVM specification name
java.vm.version JVM implementation version
java.vm.vendor JVM implementation vendor
java.vm.name JVM implementation name
java.specification.version JRE specification version
java.specification.vendor JRE specification vendor
java.specification.name JRE specification name
java.class.version Java class format version number
java.class.path Java class path
java.ext.dirs Path of extension directory(ies)
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory

System and personal variables

Here's how to get at your own system's environment from ant:

	<property environment="env" />

	<echo> Hostname: ${env.COMPUTERNAME} </echo>
	<echo> Path: ${env.Path} </echo>

Help me! Some of my variables won't expand!

If you see something very unpleasant like:

C:\Users\russ\1. dev\workspaces\android-workspace\ScoreModel>ant show-vars
Buildfile: C:\Users\russ\dev\workspaces\workspace\Fun\build.xml

show-vars:
     [echo]  build.dir=.
     [echo]  classes.dir=${build.dir}/classes
     [echo]  test.classes.dir=./test
     [echo]  jar.dir=${build.dir}/jar
     [echo]  src.dir=./src
     [echo]  lib.dir=./lib

BUILD SUCCESSFUL
Total time: 0 seconds

...and you're tempted to scream, "Why does ${build.dir} expand correctly some of the time, as used to define src.dir, and not others, for example, classes.dir?", stop and remember that you defined build.dir too late, or that you're working with two files, for example, build.xml which includes build-commons.xml (the one defining build.dir) later than classes.dir was defined by the first. Don't panic, just open your eyes!

(Obviously, I'm embarrassing myself by even noting a solution to this problem, but then I've a lot to be humble about: I'm not always the brightest Crayola™ in the box.)


How to manage buildnumber

Stackoverflow answers this question.


Some good ant targets I want to remember

This stuff was generated by Eclipse pursuant to an Export command.

Setting up the classpath(s).

      <path id="junit4.libraryclasspath">
          <pathelement location="${ECLIPSE_HOME}/plugins/org.junit_4.8.1.v4_8_1_v20100427-1100/junit.jar" />
          <pathelement location="${ECLIPSE_HOME}/plugins/org.hamcrest.core_1.1.0.v20090501071000.jar" />
      </path>
          ...
      <path id="myapplication.classpath">
          <pathelement location="build/classes" />
          <path refid="Apache Tomcat v6.0 [Apache Tomcat v6.0].libraryclasspath" />
          <path refid="junit4.libraryclasspath" />
          <pathelement location="lib/apache/log4j-1.2.16.jar" />
          <pathelement location="lib/apache/servlet-api.jar" />
          <pathelement location="lib/hibernate/antlr-2.7.6.jar" />
          <pathelement location="lib/hibernate/c3p0-0.9.1.jar" />
          <pathelement location="lib/hibernate/hibernate-jpa-2.0-api-1.0.1.Final.jar" />
          <pathelement location="lib/hibernate/hibernate3.jar" />
          <pathelement location="lib/hibernate/javassist-3.12.0.GA.jar" />
          <pathelement location="lib/jboss/webplatform-servicelocator-1.0.jar" />
          <pathelement location="lib/jersey/asm-3.1.jar" />
          <pathelement location="lib/jersey/jersey-client-1.6.jar" />
          <pathelement location="lib/jersey/jersey-core-1.6.jar" />
          <pathelement location="lib/jersey/jersey-json-1.6.jar" />
          <pathelement location="lib/jersey/jersey-server-1.6.jar" />
          <pathelement location="lib/jersey/oauth-server-1.10.jar" />
          <pathelement location="lib/jersey/oauth-signature-1.10.jar" />
          <pathelement location="lib/mysql/mysql-connector-java-5.1.16-bin.jar" />
          <pathelement location="lib/mongo/mongo-2.6.3.jar" />
          <pathelement location="lib/mongo/morphia-0.99.jar" />
          <pathelement location="lib/apache/commons-collections-3.1.jar" />
          <pathelement location="lib/apache/commons-configuration-1.7.jar" />
          <pathelement location="lib/apache/commons-lang-2.6.jar" />
          <pathelement location="lib/gson/gson-1.7.1.jar" />
          <pathelement location="lib/hibernate/jta-1.1.jar" />
          <pathelement location="lib/postgres/postgresql-9.1-901.jdbc4.jar" />
          <pathelement location="lib/spring/org.springframework.transaction-3.0.6.RELEASE.jar" />
          <pathelement location="lib/hibernate/dom4j-1.6.1.jar" />
          <pathelement location="lib/slf4j/slf4j-api-1.6.1.jar" />
          <pathelement location="lib/slf4j/slf4j-nop-1.6.1.jar" />
      </path>

Building both the application and the JUnit tests.

      <target name="build-project" depends="init">
          <echo message="${ant.project.name}: ${ant.file}" />

          <javac debug="true"
	                  debuglevel="${debuglevel}"
	                  destdir="build/classes"
	                  source="${source}"
	                  target="${target}"
	                  includeantruntime="false">
              <src path="src" />
              <classpath refid="myapplication.classpath" />
          </javac>

          <javac debug="true"
	                  debuglevel="${debuglevel}"
	                  destdir="build/classes"
	                  source="${source}"
	                  target="${target}"
	                  includeantruntime="false">
              <src path="test" />
              <classpath refid="myapplication.classpath" />
          </javac>

      </target>

Building the JUnit report. This turns up as a subdirectory with index.html you can browse. Builds one actual test, named AccountManagerTest.

      <property name="junit.output.dir" value="junit" />
      ...
      <target name="AccountManagerTest">
          <mkdir dir="${junit.output.dir}"/>
          <junit fork="yes" printsummary="withOutAndErr">
              <formatter type="xml"/>
              <test name="com.acme.web.user.manager.AccountManagerTest" todir="${junit.output.dir}"/>
          <classpath refid="rest-server.classpath"/>
          </junit>
      </target>
      ...
      <target name="junitreport">
          <junitreport todir="${junit.output.dir}">
              <fileset dir="${junit.output.dir}">
                  <include name="TEST-*.xml" />
              </fileset>
              <report format="frames" todir="${junit.output.dir}" />
          </junitreport>
      </target>