Getting started with ant
Remarks#
This section provides an overview of what Ant is, and why a developer might want to use it.
It should also mention any large subjects within Ant, and link out to the related topics. Since the Documentation for Ant is new, you may need to create initial versions of those related topics.
Minimum Java Versions
Various versions of Ant require different versions of the Java runtime (the JRE) in order to run.
Ant Version | Minimum Java Version |
---|---|
1.1 up to 1.5.4 | 1.1 |
1.6.x releases | 1.2 |
1.7.x releases | 1.3 |
1.8.x releases | 1.4 |
1.9.x releases | 1.5 |
1.10.x releases | 1.8 |
Versions#
Version | Release Date |
---|---|
1.4.1 | 2001-10-11 |
1.5.0 | 2002-07-10 |
1.5.1 | 2002-10-03 |
1.5.2 | 2003-03-03 |
1.5.3 | 2003-04-09 |
1.5.4 | 2003-08-12 |
1.6.0 | 2003-12-18 |
1.6.1 | 2004-02-12 |
1.6.2 | 2004-07-16 |
1.6.3 | 2005-04-28 |
1.6.4 | 2005-05-19 |
1.6.5 | 2005-06-02 |
1.7.0 | 2006-12-13 |
1.7.1 | 2008-07-09 |
1.8.0 | 2010-02-02 |
1.8.1 | 2010-04-30 |
1.8.2 | 2010-12-20 |
1.8.3 | 2012-03-13 |
1.8.4 | 2012-05-23 |
1.9.0 | 2013-03-10 |
1.9.1 | 2013-05-22 |
1.9.2 | 2013-07-12 |
1.9.3 | 2013-12-29 |
1.9.4 | 2014-04-30 |
1.9.5 | 2015-06-03 |
1.9.6 | 2015-07-02 |
1.9.7 | 2016-04-12 |
1.9.8 | 2016-12-31 |
1.9.9 | 2017-02-06 |
1.10.0 | 2016-12-31 |
1.10.1 | 2017-02-06 |
Hello World
Add the following to a file named build.xml
in your project directory:
<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld" default="main">
<target name="main" description="this is target main">
<echo message="Hello World" />
</target>
</project>
From a command prompt on a computer running Windows, executing ant main
will display similar to the following:
$ ant main
Buildfile: C:\Users\<me>\Projects\HelloWorld\build.xml
main:
[echo] Hello World
BUILD SUCCESSFUL
Also, user can now run the command ant
as default
target name added to the project. When ant
command is run, it looks for project’s default
target and execute it.
$ ant
Buildfile: C:\Users\<me>\Projects\HelloWorld\build.xml
main:
[echo] Hello World
BUILD SUCCESSFUL
If the build script is written by some one else and the end user like to see what target he can run, run the command which will show all the targets which has descriptions.
$ ant -p
Bootstrap Apache Ivy
Print environment information before build
Run JUnit
Create jar package
The following will create dist/output.jar
from the source code in src
and the libraries in lib
, and will use src/Main.java
as the main class.
<project name="Project" default="main" basedir=".">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="dist.dir" value="dist"/>
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
<pathelement path="${build.dir}"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"/>
<copy todir="${build.dir}">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>
<target name="jar" depends="compile">
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/${ant.project.name}.jar" basedir="${build.dir}">
<fileset dir="${build.dir}"/>
<restrict>
<archives>
<zips>
<fileset dir="lib" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<target name="main" depends="clean,jar"/>
</project>
Installation or Setup
Installing Ant is very simple. Follow the steps given below to install Ant on windows platform:
-
Download latest ant version from Apache website
-
Unzip the file on your machine.
-
Set ANT_HOME in environment variables
-
Add %ANT_HOME%\bin to your PATH environment variable.
-
Set CLASSPATH=%ANT_HOME%\lib;%CLASSPATH%
-
Now open command prompt and enter
ant
command. You should see below:Buildfile: build.xml does not exist! Build failed
Alternatively, using Homebrew on macOS or Linuxbrew on Linux you can simply run:
brew install ant
When using brew it isn’t necessary to set up the environment variables.
Several Linux distributions also support installing Ant from their respective package managers.
To test Ant is installed properly navigate to the command prompt and execute
ant -version
This command will print the Ant version and also shows that Ant was successfully installed.
Ant’s own installation instructions page is available on the Apache Ant website.