Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Ant 101 (See related posts)

// Ah Ant. How I love thee, how much better a build tool
// than my ex build tool Make.
//
// This is my current starting build.xml Ant script for any
// Java project. It specifies my usual directories, it will
// build source code, make a Jar, build a War, clean up build
// products, create Javadoc, and even run unit tests.
//
// Notice that I use the JReleaseInfo library to automatically
// generate a Java class that contains version numbers, build
// numbers, etc. Be sure to put the Jar from that library in
// the library directory to get that feature:
// http://jreleaseinfo.sourceforge.net/

   1  
   2  <?xml version="1.0"?>
   3  <project name="New Project" basedir="." default="all">
   4      <target name="init">
   5          <property name="appName" value="newProject"/>
   6      
   7          <property name="buildDir" value="${basedir}/build"/>
   8          <property name="libDir" value="${basedir}/lib"/>
   9          <property name="javaSourceDir" value="${basedir}/src"/>
  10          <property name="testDir" value="${basedir}/test"/>
  11          <property name="webSourceDir" value="${basedir}/web"/>
  12          <property name="etcDir" value="${basedir}/etc"/>
  13          <property name="tempDir" value="${basedir}/tmp"/>
  14          <property name="classDir" value="${buildDir}/classes"/>
  15  
  16          <!-- This may get overridden when we are called from Anthill. -->
  17          <property name="version" value="Local Build"/>
  18          <property name="deployDir" value="${buildDir}"/>
  19  
  20          <property name="distDir" value="${deployDir}/dist"/>
  21          <property name="junitDir" value="${deployDir}/JUnit"/>
  22          <property name="javadocDir" value="${deployDir}/apidoc"/>
  23  
  24          <!-- Create the directories where we put all the build products. -->    
  25          <mkdir dir="${buildDir}"/>
  26          <mkdir dir="${classDir}"/>
  27          <mkdir dir="${distDir}"/>
  28          <mkdir dir="${junitDir}"/>
  29          <mkdir dir="${javadocDir}"/>
  30  
  31          <path id="compileClasspath">
  32              <fileset dir="${libDir}"/>
  33          </path>
  34  
  35          <!-- Make available build info in such a way that we can display it to the user. -->
  36          <taskdef name="jreleaseinfo" classname="ch.oscg.jreleaseinfo.anttask.JReleaseInfoAntTask">
  37              <classpath refid="compileClasspath"/>
  38          </taskdef>
  39  
  40          <jreleaseinfo className="MyReleaseInfo" packageName="com.johnmunsch.${appName}"
  41                  targetDir="${javaSourceDir}" project="${appName}" version="${version}" 
  42                  buildNumFile="buildnum.properties" buildNumProperty="buildnum">
  43              <parameter name="VersionNum" type="int" value="1" />
  44              <parameter name="RevisionNum" type="Integer" value="0" />
  45          </jreleaseinfo>
  46      </target>
  47  
  48      <target name="compile" depends="init">
  49          <javac srcdir="${javaSourceDir}" destdir="${classDir}" debug="true" deprecation="true">
  50              <classpath refid="compileClasspath"/>
  51          </javac>
  52  
  53          <!-- Copy files needed to run the software to destinations in the 
  54           build directory. I do this because I usually pull all binary files like
  55           this from inside the Jar files that make up my application rather than
  56           having them loose. So they need to be copied to the class dir so they
  57           get included in the Jar file for the application. -->
  58          <copy todir="${classDir}" >
  59              <fileset dir="${javaSourceDir}">
  60                  <include name="**/*.gif"/>
  61                  <include name="**/*.jpg"/>
  62                  <include name="**/*.png"/>
  63                  <include name="**/*.wav"/>
  64                  <include name="**/*.dtd"/>
  65                  <include name="**/*.properties"/>
  66              </fileset>
  67          </copy>
  68      </target>
  69  
  70      <target name="jar" depends="init,compile">
  71          <jar jarfile="${distDir}/${appName}.jar" compress="true" 
  72              basedir="${classDir}"/>
  73      </target>
  74  
  75      <target name="war" depends="jar">
  76          <war destfile="${distDir}/${appName}.war" webxml="${etcDir}/WEB-INF/web.xml">
  77              <fileset dir="web"/>
  78              <lib dir="${libDir}">
  79                  <include name="jstl.jar"/>
  80                  <include name="standard.jar"/>
  81              </lib>
  82              <lib dir="${libDir}">
  83                  <include name="*.jar"/>
  84              </lib>
  85              <lib dir="${distDir}">
  86                  <include name="${appName}.jar"/>
  87              </lib>
  88          </war>
  89      </target>
  90  
  91      <target name="all" depends="war" description="Build everything.">
  92          <echo message="Application built."/>
  93      </target>
  94  
  95      <target name="javadoc" depends="init" description="Javadoc for the code.">
  96          <javadoc packagenames="*" sourcepath="${javaSourceDir}" 
  97  	        destdir="${javadocDir}"/>
  98      </target>
  99  
 100      <target name="clean" depends="init" description="Clean all build products.">
 101          <delete dir="${tempDir}"/>
 102          <delete dir="${javadocDir}"/>
 103          <delete dir="${junitDir}"/>
 104          <delete dir="${distDir}"/>
 105          <delete dir="${classDir}"/>
 106          <delete dir="${buildDir}"/>
 107      </target>
 108  
 109      <target name="junit" depends="jar" description="Performs unit tests.">
 110          <javac srcdir="test" destdir="${classDir}" debug="true" deprecation="true">
 111              <classpath refid="compileClasspath"/>
 112          </javac>
 113  
 114          <mkdir dir="${tempDir}"/>
 115  
 116          <junit failureproperty="junit.failed">
 117              <formatter type="xml"/>
 118              <batchtest todir="${tempDir}">
 119                  <fileset dir="${classDir}">
 120                     <include name="**/*Test.class"/>
 121                     <include name="**/Test*.class"/>
 122                  </fileset>
 123              </batchtest>
 124              <classpath>
 125                  <path refid="compileClasspath"/>
 126                  <pathelement location="${classDir}"/>
 127              </classpath>
 128          </junit>
 129  
 130          <mkdir dir="${buildDir}/JUnit"/>
 131  
 132          <junitreport tofile="TESTS-TestSuites.xml" todir="${tempDir}">
 133              <fileset dir="${tempDir}">
 134                  <include name="TEST-*.xml"/>
 135              </fileset>
 136              <report todir="${junitDir}"/>
 137          </junitreport>
 138    	
 139          <fail message="JUnit test failure." if="junit.failed"/>  	
 140      </target>
 141  </project>

You need to create an account or log in to post comments to this site.


Click here to browse all 5828 code snippets

Related Posts