| Benefits | Overview | Walkthrough | Exercise | References |
- Automatic scripting tool
- Open source
- Cross platform and portable
- Provides a means for automating tasks
- Can automatically compile source code
- Ability to run Junit tests
- Can export compiled code such as to a JAR file
- ANT builds automatically document themselves
Brief Overview
In this tutorial you will be building a simple ANT procedure for a Hello World Java program.
Walkthrough
First, lets get familiar with three of the basic components of an Ant procedure.
Project
- <project> is the top level element in an Ant script
- <project> has three optional attributes
- name- the name of the project
- default- the default target to use when no
target is supplied
- basedir- the base
directory from which all path calculations are done
- Each project has zero or more targets
- A target is a set of tasks to be executed
- When starting Ant, you can select which target(s) you want
to have executed
- When no target is given, the project's default is used
- Targets can be conditionally executed
- A target can have dependencies
- A task is a piece of code that can be executed
- A task can have multiple attributes
- Ant comes with over 80 core tasks, and 60 optional tasks
- There are over 100 third-party tools and tasks written for Ant
Exercise
In order to separate the source and automatically generated files, we must put the java source files in a "src" folder. All generated files should be under a "build" folder. The build folder will be split into subdirectories for the individual steps: "classes" for compiled files and "jar" for the generated JAR file.
First create the "src" directory inside a parent directory for this tutorial. Now create a "build" directory in the same manner, also within the parent directory.
The following Java class prints "Hello World". Place
this code into "src/HelloWorld.java"
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Make a "build\classes"
directory. Now compile and run the code:
javac -sourcepath src -d build\classes src\HelloWorld.javathis should result in a message being displayed on the screen:
java -cp build\classes HelloWorld
Hello World
Create a manifest-file containing the start class, creating the target directory and archiving the files. Create a file named "myManifest". This file should contain the text "Main-Class: HelloWorld". Now create the directory "build\jar". Next run the following commands in order to build the JAR file.
jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
java -jar build\jar\HelloWorld.jar
Now think about that build process. We need to manually compile our code and then manually call the command to make our JAR file. Let's now look out how we can automate this process using Ant.
By default Ant uses build.xml as the name for a buildfile, so our .\build.xml would be:
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="HelloWorld"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
</project>
Now we are able to compile, package and run the application via one command
ant compile jar run
References
- Apache Ant manual
- Automate your build process using Java and Ant
- The excercise was modified from Apache, the original can be found at Hello World With Ant
