Ant : As Build Tool :: build.xml File
Understanding build.xml |
Guidelines for build.xml
- The Begin and End tags for project (<project> and </project>) must start and end the file.
- The Begin <project> tag must have an attribute called default which is the name of one of the targets.
- Each build file must have at least one target.
- The begin and end tags for <target> and </target> must also match Exactly.
- Each target must have a name.
- Target depends are optional
- Anything between <echo> tag will display on console.
Sample of build.xml
<?xml version="1.0"?>
<project name="myfirstAntProject" default="mytarget2">
<target name="init">
<echo>Running Target init</echo>
</target>
<target name="mytarget1" depends="init">
<echo>Runnig target mytarget1</echo>
</target>
<target name="mytarget2" depends="mytarget1">
<echo>Runnig target mytarget2</echo>
</target>
</project>
All buildfiles require the project element and at least one target element.
The XML element project has three attributes :
Attributes | Details |
---|---|
name | The Name of the project. (Optional) |
default | The default target for the build script. A project may contain any number of targets. This attribute specifies which target should be considered as the default. (Mandatory) |
basedir | The base directory (or) the root folder for the project. (Optional) |
Create above code with name build.xml, and then run it as below
Output of above sample code
F:\antExample>ant
Buildfile: F:\antExample\build.xml
init:
[echo] Running Target init
mytarget1:
[echo] Runnig target mytarget1
mytarget2:
[echo] Runnig target mytarget2
BUILD SUCCESSFUL
Total time: 5 seconds
F:\antExample>
So, first target “mytarget2” is getting executed since it is default in build.xml. and this target is having dependency on target “mytarget1” and further target “mytarget1” having dependency on target “init”. That’s Why “init” target is running first and then “mytarget1” and then “mytarget2”.
Sample Code for build.xml |
Another Sample code for build.xml
<?xml version="1.0"?>
<project name="myAntProject" default="compile" basedir=".">
<property name="src" value=".\src\"/>
<property name="build" value=".\classes\"/>
<target name="init">
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}"
destdir="${build}"
optimize="on"
debug="false">
<classpath>
<pathelement location="${build}"/>
</classpath>
</javac>
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
<target name="jar" depends="compile">
<jar destfile="myAntProject-1.0.jar" basedir="classes">
<manifest>
<attribute name="Main-Class" value="com.javaskool.TestDrive"/>
</manifest>
</jar>
</target>
</project>
As you can see, we name each target; for one of the targets, you’ll notice the depends attribute. Each target represents a logical block of tasks that are treated as a single unit. The depends attribute controls the order in which these blocks are executed. For example, before you compile the source code, you might want to check that the output directory actually exists, and create it if it doesn’t.
Near the top of the file, you’ll notice the <property> elements, which define some variables that will be used throughout the build file. Here you’re defining the source and output directories. This is a particularly powerful feature of Ant because you can easily change the output directory without manually editing the whole file for every occurrence.
MainClass.java
Create this java file in src folder and give name class TestDrive.java, even you can store all java file here.
package com.javaskool;
class TestDrive
{
public static void main(String[] args)
{
System.out.println("Hi I am James Bond!");
}
}
Output of above sample code with ant command
F:\antExample>ant
Buildfile: F:\antExample\build.xml
init:
[mkdir] Created dir: F:\antExample\classes
compile:
[javac] F:\antExample\build.xml:15: warning: 'includeantruntime' was not set
, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to F:\antExample\classes
BUILD SUCCESSFUL
Total time: 3 seconds
F:\antExample>
The first thing Ant printed was the name of the build file it was processing. Second, it started running through the targets. But how did it know which one to run first? Looking back at the build.xml file, you’ll notice that in the top-level tag, <project>, there was an attribute, default, that defined which target was to be run first. default had the value compile, but init was the first target that was actually run.
However, what if you want to force a compilation. How do you signal that? That is where the final target in the build file-the clean target-comes into play. As you can probably guess from its name, this target deletes the files in the directory defined by the property ${build}. But how do you run it? It is very simple. You can pass in the name of the target that you want to trigger to the ant script as below.
This will override the default target defined in the project element and run the clean target. After you’ve done this, you can run the ant script again to compile. Alternatively, you could stack up the trigger targets in one call at the command line:
Ant command with target clean, compile and jar
Output
F:\antExample>ant clean compile jar
Buildfile: F:\antExample\build.xml
clean:
[delete] Deleting directory F:\antExample\classes
init:
[mkdir] Created dir: F:\antExample\classes
compile:
[javac] F:\antExample\build.xml:15: warning: 'includeantruntime' was not set
, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to F:\antExample\classes
init:
compile:
[javac] F:\antExample\build.xml:15: warning: 'includeantruntime' was not set
, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to F:\antExample\classes
jar:
[jar] Building jar: F:\antExample\myAntProject-1.0.jar
BUILD SUCCESSFUL
Total time: 2 seconds
F:\antExample>java -jar myAntProject-1.0.jar
'java' is not recognized as an internal or external command,
operable program or batch file.
F:\antExample>set PATH=C:\Program Files\Java\jdk1.7.0_03\bin;.;
F:\antExample>java -jar myAntProject-1.0.jar
Hi I am James Bond!
F:\antExample>
This will run the clean , compile target and then the jar target immediately afterward.
Recent Comments