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 likeC:\Users\jlepak\programs\apache-ant-1.8.3
- Update
PATH
to contain thebin
folder whereant.bat
is saved. More information on setting PATH. - If you get an error about
JAVA_HOME
being incorrect, you'll need to define aJAVA_HOME
environment variable that points to the location of your JDK. This is usually something likeC:\Program Files\Java\jdk1.7.0
.
The course materials contain a very simple 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" description="Print a greeting">
<echo message="Hello!" />
</target>
<target name="init">
<mkdir dir="${build.dir}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}" />
</target>
<target name="dist" depends="compile">
<jar destfile="Hello.jar" basedir="${build.dir}">
<manifest>
<!-- This is XML comment syntax -->
<!-- Switched to graphical main class to work
on default Windows setup -->
<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). However, the default Windows setup is to run
it expecting a graphical interface. You can see the setup by typing ftype
jarfile
. I edited the project so that it now uses a graphical program as the
default main class in the jar file.
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.
Homework
Write a description of your favorite recipe using XML. To do this, decide what tags to use to describe things like ingredients, measurements, and step descriptions.
Find examples of 2 other file formats that are based on XML, and describe what they're for. Show a small example, if possible. For example, SVG is a format for vector graphics, and a simple example looks like:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <circle cx="100" cy="50" r="50" fill="black" /> <circle cx="100" cy="50" r="40" fill="orange" /> <circle cx="100" cy="50" r="30" fill="black" /> <circle cx="100" cy="50" r="20" fill="orange" /> <circle cx="100" cy="50" r="10" fill="black" /> </svg>
And the processed file looks like:
Find a previous homework assignment in which you used Java, and create an Ant build setup for it. Include a target that will run one of the programs in your assignment (see the java task type).
To submit, either do your work in your 185-hw
repository in a folder called
hw7
(and post to your BitBucket account when you're done), or email your
answers in a zip file to jal2016@email.vccs.edu with subject CSC 185 HW7.
Due Tuesday, Mar 20.