Build Tools

Build Tools
 Creating a product from source may take several steps:  Compile  Link  Copy files to various directories  Remove intermediate files  Generate documentation  It becomes problematic to do all these steps manually, first of all because it’s boring, second because it is error-prone.  The objective should be an automated tool that does all the work for you. Type or click one command and create a final product.

Build Tools
 There are a couple ways this can be done:
 Write a batch file or script
 The scripts tend to be hard to maintain

 Use a tool designed for the task
 Make  Ant

 Make is the original tool for this sort of thing. Roots in the Unix world  The stuff to the left of the colon is the “target”, the stuff to the right the “dependents”, and the lines below that the actions to take  If a target is newer than its dependents, the actions are performed
lib.a: x.o y.o z.o ar rvu lib.a x.o y.o z.o ranlib lib.a

 Make has some well-known pathologies
 The action lines MUST start with a tab, which is impossible to see  The action lines have platformdependent scripting dependencies

 The platform restrictions weren’t so bad in the Unix world, but Java was intended to be cross-platform. A build file using Make couldn’t transition from a Unix to a windows box to a Mac box. Make tends to be C- and Unix-centric.  So ant was developed as a crossplatform build tool

What File Format to use?
 If you’re developing a new tool, what should the syntax of the file be?  The world needs fewer file formats. If you write your own unique syntax, like Make files, you wind up having to parse that syntax. That usually means a trip to yacc, lex, and friends.  Ant choose to use XML rather than develop its own syntax. This lets people leverage their existing knowledge of XML rather than learn a bunch of new rules

 Ant is a cross-platform, XML-based system for creating software products from source code.  It is NOT a scripting language. But that doesn’t stop some people.  Open source, available at

Installing Ant
 Get the binary release and set ANT_HOME and JAVA_HOME environment variables. Put the ant bin directory on the PATH environment variable.

Example Fragment
 Creates some directories to hold the output of the build process.
 Matching <target> and </target> tags  Solo tags ended with “/>”  Uses XML tag attributes
<target description="Preparatory actions for compilation and documentation targets" name="init"> <mkdir dir="build"/> <mkdir dir=“build/lib"/> <mkdir dir= “build/classes"/> <mkdir dir=“build/doc"/> </target>

 A “target” is something that needs to be done—create initial directories, compile source, create javadoc, create jar files, etc.  Targets can depend on other targets
 The “compile” task can depend on an “init” task that creates directories for the products to land in

Target Dependencies
 You can add a comma-delimited list of other targets that depend on this target. If the dependent target depends on other things, those will run, too.
<target name=“init“ description=“Create directories”> … </target> <target name="compile“ depends="init“ description=“Compile Java sources”> … </target> <target name=“jar“ depends=“compile“ description=“Create jar files”> … </target>

 You can define variables so things like directory names aren’t hardcoded through your ant file.

<property description="Source directory" location="${basedir}/source" name="dir.src"/>

A Simple Compile Task
 Compile the source directory, put the results in the build/classes directory, and use the specified jar file
<target depends="init" name="compile"> <!--Compile everything in the source directory -> <javac destdir="${}"> <classpath> <pathelement location=“${lib.dir}/somelib.jar” </classpath> <src path="${dir.src}"/> </javac> </target>

Ant Files
 Often you need to specify lists of files (as in classpaths). It is brittle to do this by using a long quoted series of files
 Path=“foo.jar,baz.jar,xml.jar,…”

 Ant can handle this by using lists of XML tags  <Classpath> can have multiple <pathelement> tags embedded.  This is somewhat more maintainable than a single quoted list.

 Often you want to specify a whole series of files, as with the classpath example, when many jar files needed to be specified  Rather than individually name every jar file, you can specify a set of files, in this case in the lib directory and all subdirectories of the lib directory. You can also use regexps to select files in the fileset.
<classpath> <fileset dir="${dir.lib}" includes="**/*.jar"/> </classpath>

Popular Ant Tasks
        Javac Javadoc jar Mkdir Copy Delete Junit (automated tests) FTP (FTP a result to a server)

User-Written tasks
 You can also write your own tasks with fairly low quantities of drama  This has been used to do things like automatically deploy servlets to a tomcat server as part of the build process

Ant files
 Ant files are usually named “build.xml” and usually reside in the root of your project directory.  To run, type “ant <targetname>”.

Simple Example
 Write an ant file for a simple project  Source directory, compile to a build directory, do jar and javadoc  Tasks for init, compile, jar, javadoc, clean
Foo build.xml source build classes doc project.jar

Simple Example

 See for the example with a simple project

Parting thoughts
 Don’t depend on manual build steps if at all possible  The objective should be typing or clicking “ant” which leads to a functioning product from source  It’s possible, but probably a bad idea, to use ant for C++/C