 |
Apache Ant External Wiki - http://nagoya.apache.org/wiki/apachewiki.cgi?AntProjectPages
Ant is an automated make tool, now part of the Apache Group's distributions. Though an IDE may support the idea of building the software internally, and may even have tools to build .jar files as well, it is often helpful to use Ant in parallel with the IDE, so that other developers who may not be using the same IDE can also build the software. Ant is very flexible; in fact, it can actually compile, test, create javadocs, build jars, and do so for multiple projects in the same build script! There are also examples of Ant being used to automate code check-ins to CVS, and nightly build creation. Examples here would be appreciated.
Ant is part of Apache, and can be found at http://ant.apache.org/
Ant depends on build scripts. An example of such follows below.
==
<project name="Product" default="all" basedir=".">
<property name = "productName" value = "Product" />
<property name = "source" value = "src" />
<property name = "code" value = "${source}/code" />
<property name = "tests" value = "${source}/tests" />
<property name = "resources" value = "${source}/resources" />
<property name = "test-resources" value = "${source}/test-resources" />
<property name = "build" value = "build" />
<property name = "build-code" value = "${build}/code" />
<property name = "build-tests" value = "${build}/tests" />
<property name = "build-resources" value = "${build}/resources" />
<property name = "documents" value = "${build}/docs" />
<property name = "javadoc" value = "${build}/docs/api" />
<property name = "lib" value = "lib" />
<property name = "dist" value = "dist" />
<property name = "dist-lib" value = "${dist}/lib" />
<property name = "package" value = "com.equinoxe" />
<property name = "versionClassName" value = "BuildInfo"/>
<property name = "versionClass" value = "${package}.${versionClassName}" />
<path id="project.classpath" >
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${build-resources}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${build-code}"/>
<pathelement location="${build-tests}"/>
</path>
<!-- Build Information Updaters -->
<!-- DO NOT MODIFY THIS SECTION -->
<target name="declare">
<taskdef name="versionupdate"
classname="com.ryangrier.ant.VersionUpdate"/>
<taskdef name="buildupdate"
classname="com.ryangrier.ant.BuildUpdate"/>
</target>
<target name="updateMajorVersion" depends="declare">
<versionupdate
debug="true"
srcdir="${code}"
classname="${versionClass}"
variablename="version"
versionupdatetype="Major">
</versionupdate>
</target>
<target name="updatePointVersion" depends="declare">
<versionupdate
debug="true"
srcdir="${code}"
classname="${versionClass}"
variablename="version"
versionupdatetype="Point">
</versionupdate>
</target>
<target name="updateMinorVersion" depends="declare">
<versionupdate
debug="true"
srcdir="${code}"
classname="${versionClass}"
variablename="version"
versionupdatetype="Minor">
</versionupdate>
</target>
<target name="updateBuildNumber" depends="declare">
<buildupdate
srcdir="${code}"
classname="${versionClass}"
variablename="build"
debug="true" >
</buildupdate>
</target>
<!-- End Build Information Updaters-->
<!-- Clean Build Directory-->
<target name="clean">
<delete dir="${build}" />
<mkdir dir="${build}" />
<mkdir dir="${build}/code" />
<mkdir dir="${build}/tests" />
<mkdir dir="${build}/resources" />
</target>
<!-- Code Compilers -->
<target name="compile" depends="compile-code,compile-tests,compile-resources" />
<target name="compile-code" >
<javac srcdir="${code}" destdir="${build-code}">
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="compile-tests" >
<javac srcdir="${tests}" destdir="${build-tests}">
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="compile-resources">
<copy todir="${build-resources}">
<fileset dir="${resources}"/>
</copy>
</target>
<target name="create-javadocs">
<javadoc packagenames="com.equinoxe.*"
sourcepath="${code}"
defaultexcludes="yes"
destdir="${javadoc}"
author="true"
version="true"
use="true"
classpathref="project.classpath"
windowtitle="Product API">
<doctitle><![CDATA[<h1>Product</h1>]]></doctitle>
<bottom><![CDATA[<i>Copyright © 2003 Equinoxe Solutions Corp. All Rights Reserved.</i>]]></bottom>
<tag name="todo" scope="all" description="To do:" />
</javadoc>
</target>
<!-- Code Testers -->
<target name="test" depends="compile-tests">
<junit dir="${build-tests}" showoutput="on" haltonerror="yes" haltonfailure="yes" >
<classpath>
<path refid="project.classpath"/>
<pathelement location="${test-resources}"/>
</classpath>
<batchtest fork="yes" todir="JUnitOutput">
<fileset dir="${build-tests}">
<include name="**/*Test*.class"/>
</fileset>
</batchtest>
</junit>
</target>
<!-- Code archivers -->
<target name="createCodeJar">
<jar jarfile="${build}/${productName}.jar" basedir="${build}/code" includes="**/*.class" />
</target>
<target name="createResourcesJar">
<jar jarfile="${build}/${productName}Resources.jar" basedir="${build}/resources" includes="**/*" />
</target>
<target name="createSourceJar">
<jar jarfile="${build}/${productName}Source.jar" basedir="${code}" includes="**/*"/>
</target>
<target name="createTestsJar">
<jar jarfile="${build}/${productName}Tests.jar" basedir="${tests}" includes="**/*"/>
</target>
<target name="createJavaDocJar" depends="create-javadocs">
<jar jarfile="${build}/${productName}API.jar" basedir="${javadoc}" includes="**/*" />
</target>
<target name="createAllJars" depends="createSourceJar,createTestsJar,createCodeJar,createResourcesJar,createJavaDocJar"/>
<!-- Actual building tasks -->
<target name="createBuild" depends="clean,updateBuildNumber,compile,test"/>
<target name="createDistribution" depends="createBuild,createAllJars">
<copy file="${build}/${productName}.jar" todir="${dist}"/>
<copy file="${build}/${productName}Resources.jar" todir="${dist}"/>
<copy file="${build}/${productName}API.jar" todir="${dist}"/>
<copy todir="${dist-lib}">
<fileset dir="${lib}">
<include name="**/*.jar"/>
<include name="**/*.dll"/>
</fileset>
</copy>
</target>
</project>
==
The above script comes from a production environment where we use this daily to build our software. I've renamed references to make it pretty plain. It contains a couple of tags that show off just how flexible Ant can be: these are the versionupdate and buildupdate tags, where Ant is actually modifying a Java class in the path which contains build information. To do this, I've modified a tool called version_tool by Ryan Grier, available at http://ant.ryangrier.com/ slightly (I changed the versioning string so that it uses the format I preferred). Again, this is just an example of what you can do.
-- GergisKhan - 27 Aug 2003
|