Week 10: build automation
2013-03-26
Large software projects can require a very complex process to compile the code
and package the resulting executable to be distributed. This process can
involve many steps, such as testing and packaging resources and configuration
files. For this reason, it's useful to automate as much of the
process as possible, even for small projects.
You might have been using an IDE like NetBeans or Eclipse to provide some of
this functionality: those IDEs will automatically compile your project, and
possible perform other tasks such as testing or generating documentation.
We're going to use Apache Ant to illustrate how to generalize this
process.
XML
Ant uses XML format for its configuration file. XML is a markup language
that can be used to model hierarchical data. There are tons of tools available
for parsing, transforming, and displaying data described using XML.
Ant
To install (on Windows; other operating systems are easier),
download Ant unzip it to any desired location, and set the following
environment variables:
ANT_HOME
should be set to the full path of the directory you just unzipped,
for example something like C:\Users\jlepak\programs\apache-ant-1.9.0
- Update
PATH
to contain the bin
folder where ant.bat
is saved.
More information on setting PATH.
- If you get an error about
JAVA_HOME
being incorrect, you'll need
to define a JAVA_HOME
environment variable that points to the location
of your JDK. This is usually something like C:\Program Files\Java\jdk1.7.0
.
The course materials contain a very simple example in
examples/ant-example/
that we'll build on in class (see the
examples/ant-example directory). Here's the Ant setup file, that we'll improve
and add to:
<project>
<property name="build.dir" location="build" />
<property name="src.dir" location="src" />
<target name="hello">
<echo message="Hello!!!" />
</target>
<target name="init" description="Initialize build">
<mkdir dir="${build.dir}" />
</target>
<target name="compile" depends="init" description="Compile all source files">
<javac srcdir="${src.dir}" destdir="${build.dir}" />
</target>
<target name="dist" depends="compile" description="Build jar file">
<jar destfile="Hello.jar" basedir="${build.dir}">
<manifest>
<!-- This is XML comment syntax -->
<!-- This attribute defines which class runs when running the jar
(or with the way Java is commonly set up in Windows, when the
jar is double-clicked). -->
<attribute name="Main-Class" value="HelloGUI" />
</manifest>
</jar>
</target>
</project>
The dist
target will package the program and allow it to run like a normal
executable (on Windows at least - you can see the setup by typing ftype
jarfile
).
Readings
For XML information, check out this XML tutorial. For tons more detail, check
out the introductory chapters of Beginning XML.
For Ant, start with this Ant tutorial, which is part of the extensive
Ant manual. See the intro chapters in Ant in Action
for lots more information.