Build engineering involves compiling source code and packaging the release for distribution. I have worked with many Java build systems written in Maven 1.0.2, Maven 2 and Ant 1.6.5. Maven is very popular with a lot of java developers because it enforces standard directory names and also does a lot of the work automatically for you. Maven enthusiasts often refer to this as maven "doing the heavy lifting for you". That said, there are also a lot of good reasons to use Ant either instead of Maven or together with Maven. Read on to join us for a quick intro to Ant 1.6.5.
Well maybe we should talk about Maven 2 first
The beauty of Maven is that the developer does not have to write all the XML to do all the work manually to setup the build details. Maven's implicit assumptions are not without problems though. Many developers look back at large Maven 2 build XMLs and scratch their heads as they try to figure out what is going on. Now obviously, there are a lot of smart developers who know Maven 2 well and understand exactly what is happening. I have personally puzzled over many a Maven 2 script and had to ask for help when the scripts were more complicated (pretty painful embarrassment for a build engineer). I have also put Maven 2 into a debug mode and studied the Maven 2 cycle closely in an attempt to see exactly what was going on in each step. Often I would hear my colleagues utter the words "maven bug". We'll talk more about Maven 2 and go through some examples in future, but for now we will take a quick look at Ant.
What I like about Ant
I like Ant a lot because I always understand what Ant is doing. It's true that I also have to write all of the XML myself. Many Maven enthusiasts also have to use Ant to create features in Maven scripts that cannot currently be done using Maven's existing features. With some developers this starts to sound like a religious discussion so I will try to fairly point out the pros and cons of each (Maven and Ant) in this and subsequent build engineering articles.
Cygwin is a nice platform to use when learning Ant
My examples are going to shown using my CYGWIN console which allows me to work in a nifty Unix like interface from my PC. Download Cygwin from www.cygwin.com. I also installed my Ant and Maven environments on my PC and then set an environment variable as follows - ANT_HOME=d:\ant-1.6.5. I also modified my path to include
%ANT_HOME%/bin.
I tested my installation of ant from cygwin as follows:
$ ant -version
Apache Ant version 1.6.5 compiled on June 2 2005
Then I created a little test hello.java program in a subdirectory called src.
Here is the Ant build.xml for this simple build.
$ cat build.xml
<!-- Our project is called "Test" and the default target -->
<!-- is set to "dist" which depends upon the "compile" -->
<!-- target. The "compile" target depends upon the "init" -->
<!-- target. -->
<project name="Test" default="dist" basedir=".">
<description>
Test Ant 1.6.5 build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Setup a time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} à
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory à
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the Test-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/Test-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees à
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
The build.xml is invoked as follows:
$ ant
Buildfile: build.xml
init:
compile:
dist:
BUILD SUCCESSFUL
Total time: 1 second
This shows that the init step is run first, followed by the compile and dist steps which results in a jar as shown here:
$ ls -lt dist/lib
total 4
-rwxr-xr-x 1 student user 671 Jun 29 22:42 Test-20080630.jar
You can find out lots more information on Ant by visiting http://ant.apache.org.
In future articles we will show more advanced features of Ant as well as how to imbed ant directives in your Maven 2 scripts.
Build engineering is a lot of fun and learning more about Maven and Ant will put you in a good position to setup your builds in a reliable and repeatable way!
Bob Aiello is the Editor-in-Chief for CM Crossroads and an independent consultant specializing in Software Process Improvement including Software Configuration and Release Management. Mr. Aiello has over 25 years experience as a technical manager in several top NYC Financial Services firms where he had had company-wide responsibility for CM, often providing hands-on technical support for enterprise Source Code Management tools, SOX/Cobit compliance, build engineering, continuous integration and automated application deployment. Bob is a long standing member of the Steering Committee of the NYC Software Process Improvement Network (CitySPIN), where he serves as the chair of the CM SIG. Mr. Aiello holds a Masters in Industrial Psychology from NYU and a B.S. in Computer Science and Math from Hofstra University. You may contact Mr. Aiello at raiello@acm.org or link with him at http://www.linkedin.com/in/bobaiello
Trackback(0)
Comments 
Write comment
 |