Now that we've learned how to define properties, dependencies, and how to run ant, we'll learn how to use ant to compile Java source code and generate jar files.
Compiling source code
Because Ant's primary goal is to build Java applications, it has built-in Javac tasks to invoke the Java compiler. This task is generally defined as follows
<javac srcdir= "src" destdir= "Build"/>
Ant will look for all the files in the SRC directory with the. java extension, and call the Javac compiler on them to generate the class file in the build directory. The JAVAC task compiles not only the source files that need to be compiled, but if a class file already exists, but the corresponding source code file has not been changed since the class file was generated, the source file will not be recompiled.
Create a JAR file
After compiling the Java source files, it is common to package their archives as a jar file. The jar task is usually defined as follows
<jar destfile= "Wufengtinghai.alm.jar" basedir= "Build"/>
This task creates a Wufengtinghai.alm.jar file and packages all the files under build into this jar file. If you do not specify a manifest file, Ant generates a basic manifest file. The Manifest property allows you to create a manifest file, and you can also use the manifest task to define a manifest file.
<JarDestFile= "Wufengtinghai.alm.jar"Basedir= "Classes"><Manifest><attributename= "Built-by"value= "${user.name}"/><attributename= "Project"value= "Wufengtinghai.alm"/></Manifest></Jar>
View Code
Generate Timestamp
Use the current time and date in the build environment to mark when a build generates output in this way. Ant provides a simple and easy-to-use Tstamp task to accomplish this function. This task is usually called at the beginning of the build process. Tstamp does not produce any output; instead, it sets ant's related properties based on the current system time and date.
Dstamp: Set to current date, default format is YYYMMDD, for example 20150606
Tstamp: Set to current time, default format is HHMM, for example 1020
Today: set to current date with full month, e.g. June 6, 2015
Then we can define our jar file as follows
<jar destfile= "Lucene-${dstamp}.jar" basedir= "Classes"/>
Eventually our build file Autobuildjava.xml as follows
<?XML version= "1.0"?><Projectdefault= "Dist"name= "Autobuildjava"Basedir=".. /"> <Description>A Simple Auto Build Java project</Description> < Propertyname= "Srcdir" Location= "src" /> < Propertyname= "BuildDir" Location= "Build" /> < Propertyname= "Distdir" Location= "Dist" /> <Targetname= "Init"> <Tstamp/> <mkdirdir= "${builddir}" /> <mkdirdir= "${distdir}" /> </Target> <Targetname= "Compile"depends= "Init"> <JavacSrcdir= "${srcdir}"Destdir= "${builddir}" /> </Target> <Targetname= "Dist"depends= "Compile"> <JarDestFile= "${distdir}/wufengtinghai.alm-${dstamp}.jar"Basedir= "${builddir}"> <Manifest> <attributename= "Built-by"value= "${user.name}" /> <attributename= "Project"value= "Wufengtinghai.alm" /> </Manifest> </Jar> </Target> <Targetname= "clean"> <Deletedir= "${builddir}" /> <Deletedir= "${distdir}" /> </Target></Project>
View Code
Perform the build console output as follows
The generated jar files and manifest files are as follows
Using ant automation to build our Java project