- How to write build.xml and run build in Apache ANT? Example
- Apache Ant — Tutorial
- 1. Build tools
- 2. Apache Ant Overview
- 2.1. What is Apache Ant?
- 2.2. Configuring Apache Ant
- 3. Installation
- 3.1. Prerequisites
- 3.2. Ubuntu
- 3.3. Windows
- 4. Tutorial: Using Apache Ant
- 4.1. Using Ant for Java development
- 4.2. Create Java project
- 4.3. Create build.xml
- 4.4. Run your Ant build from Eclipse
- 4.5. Run your Ant build from the command line
- 5. Apache Ant classpath
- 5.1. Setting the Ant classpath
- 5.2. Print
How to write build.xml and run build in Apache ANT? Example
This is the second article on Apache ANT tutorials for beginners series As I have always said that I like the short, clear, and concise tutorial which tells about few concepts but in a clear and concise manner and puts weight on fundamentals. I try to adopt the same theory while writing my blog post while writing my experience coupled with the concept which is important for a software developer’s point of view. Here I am answering some of the basic questions related to installing the ANT tool, running ANT build, creating a build.XML file, debugging build.xml in the case of any issue.
These questions have been asked by my students while teaching them JAVA and related technology in my early career.
How do I run ant?
To run you need to download ant and install it on your machine, then create environment variable ANT_HOME and include ANT_HOME/bin into your PATH like below.
In Windows path=%path%;%ANT_HOME%/bin
In Linux PATH =$:$/bin
Now you can open a command prompt and type ant.
If you get this output, means ant binaries is not in your path
C:\Documents and Settings>ant
‘ant’ is not recognized as an internal or external command, operable program, or batch file.
Otherwise, you will get output that will complain about the build file if it doesn’t exist.
C:\Documents and Settings>ant
Buildfile: build.xml does not exist!
Build failed
How do I write build.xml file?
here is a sample build.xml you just need to know important element e.g. project ,target ,property and task and the order in which different target gets executed to start with basic build procedure.
This line defines our project; every build file must have this line. The project name is “test” defined by attribute “name”; default target is “all” while running “ant” command from command prompt if we don’t specify any target than ant executed this default target.
basedir tells which is the top level directory for creating the build in this case its current directory (from where you run ant command) , denoted by dot “.” .
Here we are declaring and specifying property ,you can say variable every property has at least two attributes “name” and “value” , though you can define your all properties in a separate properties file and load from there as well . denotes ant’s property task, which do have some other attribute e.g. location to specify location of any properties file. I recommend that you always use property in your build.xml instead of using hard coded values in target for directory name etc , this will give you flexibility to change the value anytime without changing at many places (in case you have hard coded it).
Here we are defining a target since we have already called target “all” as default in project tag, so if we don’t specify this target our build will fail to say “target not found”.
”name” attribute specified name of target. “depends ontarget” says that before executing this target executed first “clean” and then “compile”
This will print message in console as “doing all”
This is target “Clean” which will delete old build before building new one , you can have as many target you want based on your need.
«/>
delete task or tag is used to delete directory, file etc, verbose=true makes it to print message while deleting in cosole, is an important tag and I recommend you to read in detail somewhere in ant manual or may be I will explain in detail sometime because it include “patternset” which supports pattern matching of directory/files via its includes and excludes attribute which is extremely useful to filter unwanted files (generally meta data files form CVS, SVN etc).
Here we are deleting build directory by using value of property “build”, $ denotes value of any property.
This is our compile target ,which compiles our code and also copies resources e.g. images, we can also create jar file using ant, which we are not doing here just for simplicity.
Imporant thing here is property $ this is builtin propety provided by ant and its’s value is name of project defined by attribute “name” of proejct tag.
tag is used to copy files and tag is used to compile java code .
How do I debug build.xml?
if you see the problem on your build or you are getting exceptions related to finding files/directory or anything then you would like to know what’s going behind there are two option , run ant on the verbose option, it will print lots of detail (I don’t prefer this) because of so much unwanted information but can be useful in certain situation.
Second and my preferred way is the good old “echo” way. use echo task to print values of properties, variables or printing simple messages to check the work flow.
here are some examples of using echo
How do I enforce ant to use file other than build.xml?
Normally when you run ant from any directory from command prompt it will look for file called build.xml in current directory; if it doesn’t find the file it will give error. If you have file named “custom_build.xml” you can instruct ant to use this file for building your application by using option “-f” e.g. ant –f custom_build.xml
Hope this would be useful let me know if you have any questions, doubt etc will be happy to answer.
Apache Ant — Tutorial
Apache Ant Tutorial. This tutorial describes the usage of Ant as a build tool to compile Java code, pack this code into an executable jar and how to create Javadoc. The usage of Ant is demonstrated within Eclipse and from the command line. This tutorial is based on Apache Ant 1.8.x.
1. Build tools
In software development the term building usually means the conversion of source code and other artifacts, like images or configuration files, into another artifact. For example source code might be compiled into a JAR file, or you may create a new standalone application. The build result can be shared with users of the software or used only internally.
A build tool is used to automate repetitive tasks during this process. This can be, for example, compiling source code, running software tests and creating files and documentation for the software deployment.
Build tools typically run without a graphical user interface directly from the command line. As a user interface is not required for such builds, these builds are called headless.
Popular build tools in the Java space are Maven, Gradle and Apache Ant.
2. Apache Ant Overview
2.1. What is Apache Ant?
Apache Ant (Ant) is a general purpose build tool. Ant is an abbreviation for Another Neat Tool.
Ant is primarily used for building and deploying Java projects but can be used for every possible repetitive tasks, e.g. generating documentation.
2.2. Configuring Apache Ant
A Java build process typically includes:
- the compilation of the Java source code into Java bytecode
- creation of the .jar file for the distribution of the code
- creation of the Javadoc documentation
Ant uses an xml file for its configuration. The default file name is build.xml . Ant builds are based on three blocks: tasks, targets and extension points.
A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets.
A target can be directly invoked via Ant. Targets can specify their dependencies. Ant will automatically execute all dependent targets.
For example if target A depends on B and Ant is instructed to run A, it first runs B before running A.
In your build.xml file you can specify the default target. Ant executes this target, if no explicit target is specified.
3. Installation
3.1. Prerequisites
Ant requires the installation of the Java Development Kit (JDK). See Java introduction for a description how to install Java.
3.2. Ubuntu
On Ubuntu use the apt-get install ant command to install Ant. For other distributions please check the documentation of your vendor.
3.3. Windows
Extract the zip file into a directory structure of your choice. Set the ANT_HOME environment variable to this location and include the ANT_HOME/bin directory in your path.
Make also sure that the JAVA_HOME environment variable is set to the JDK. This is required for running Ant.
Check your installation by opening a command line and typing ant -version into the commend line. The system should find the command ant and show the version number of your installed Ant version.
4. Tutorial: Using Apache Ant
4.1. Using Ant for Java development
The following describes how you compile Java classes, create an executable JAR file and create Javadoc for your project with Apache Ant. The following example assumes that you are using the Eclipse IDE to manage your Java project and your Ant build.
4.2. Create Java project
Create a Java project called de.vogella.build.ant.first in Eclipse. Create a package called math and the following class.
package math; public class MyMath public int multi(int number1, int number2) return number1 * number2; > >
Create the test package and the following class.
package test; import math.MyMath; public class Main public static void main(String[] args) MyMath math = new MyMath(); System.out.println("Result is: " + math.multi(5, 10)); > >
4.3. Create build.xml
Create a new file through the File New File menu and create the build.xml file. Implement the following code to this file.
name="Ant-Test" default="main" basedir="."> --> name="src.dir" location="src" /> name="build.dir" location="bin" /> name="dist.dir" location="dist" /> name="docs.dir" location="docs" /> name="clean"> dir="$" /> dir="$" /> dir="$" /> name="makedir"> dir="$" /> dir="$" /> dir="$" /> name="compile" depends="clean, makedir"> srcdir="$" destdir="$"> name="docs" depends="compile"> packagenames="src" sourcepath="$" destdir="$"> dir="$"> name="**" /> name="jar" depends="compile"> destfile="$\de.vogella.build.test.ant.jar" basedir="$"> name="Main-Class" value="test.Main" /> name="main" depends="compile, jar, docs"> Main target
The code is documented, you should be able to determine the purpose of the different ant tasks via the documentation in the coding.
4.4. Run your Ant build from Eclipse
Run the build.xml file as an Ant Build in Eclipse.
After this process your data structure should look like this:
4.5. Run your Ant build from the command line
Open a command line and switch to your project directory. Type the following commands.
# run the build ant -f build.xml # run ant with defaults (build.xml file and the default target) ant
The build should finish successfully and generate the build artifacts.
5. Apache Ant classpath
5.1. Setting the Ant classpath
Ant allows to create classpath containers and use them in tasks. The following build.xml from a project called de.vogella.build.ant.classpath demonstrates this.
name="Ant-Test" default="Main" basedir="."> --> name="src.dir" location="src" /> name="lib.dir" location="lib" /> name="build.dir" location="bin" /> id="build.classpath"> dir="$"> name="**/*.jar" /> name="clean"> dir="$" /> name="makedir"> dir="$" /> name="compile" depends="clean, makedir"> srcdir="$" destdir="$" classpathref="build.classpath" /> name="Main" depends="compile"> Main target
5.2. Print
You can use the following statements to write the full classpath to the console. You can use this to easily verify if the classpath is correct.
" />