ANT
Set-up
1. Download ANT from here
2. Set ANT_HOME
using the same process as that of setting TESTNG_HOME, but pointing to
ANT jar.
3. Validate ANT version by running ‘ant - version’
in the command prompt
Understanding
build.xml
Build.xml is the most important component of Ant build
tool. For a Java project, all cleaning, setup, compilation and deployment
related task are mentioned in this file in XML format. When we execute this XML
file using command line or any IDE plugin, all instructions written into this
file will get executed in sequential manner.
Let's understand the code within
a sample build.XML
- Project tag is used to mention a project name and basedir
attribute. The basedir is the root directory of an application. Project
is the starting point of the Ant configuration file and consists of the entire
configuration with respect to building a project
1
| <project name="YourProjectName" basedir=".">
|
- Property tags are used as variables in build.XML file to be used
in further steps. They are an important way to customize a build process or to
provides way to define shortcuts to reuse repeatedly inside a build file
1
2
3
4
| <property name="build.dir" value="${basedir}/build"/>
<property name="external.jars" value=".\resources"/>
<property name="YourProjectName.dir" value="${external.jars}/YourProjectName"/>
<property name="src.dir"value="${basedir}/src"/>
|
- A target is a set of tasks that that you want to execute during
the build process. You can have multiple targets in a single build.xml
1
| <target name="setClassPath">
|
- path tag is used to bundle all files logically which are in the
common location
1
| <path id="classpath_jars">
|
- pathelement tag will set the path to the root of common location
where all files are stored
1
| <pathelement path="${basedir}/"/>
|
- pathconvert tag used to convert paths of all common file inside
path tag to system's classpath format
1
| <pathconvert pathsep=";" property="test.classpath" refid="classpath_jars"/>
|
- fileset tag used to set classpath for different third party jar
in our project
1
| <fileset dir="${YourProjectName.dir}" includes="*.jar"/>
|
- Echo tag is used to print text on the console
1
| <echo message="deleting existing build directory"/>
|
- Delete tag will clean data from given folder
1
| <delete dir="${build.dir}"/>
|
- mkdir tag will create a new directory
1
| <mkdir dir="${build.dir}"/>
|
- javac tag used to compile java source code and move .class files
to a new folder
1
2
3
| <javac destdir="${build.dir}" srcdir="${src.dir}">
<classpath refid="classpath_jars"/>
</javac>
|
- jar tag will create jar file from .class files
1
| <jar destfile="${YourProjectName.dir}/YourProjectName.jar" basedir="${build.dir}">
|
- manifest tag will set your main class for execution
1
2
3
| <manifest>
<attribute name="Main-Class" value="test.Main"/>
</manifest>
|
- 'depends' attribute used to make one target to depend on another
target
1
| <target name="run" depends="compile">
|
- java tag will execute main function from the jar created in
compile target section
1
| <java jar="${ytoperation.dir}/YourProjectName.jar" fork="true"/>
|
Generate build file in IDE:
Select File -> click on Export -> General -> Ant Buildfiles
and specify the file name, project, etc as per your needs.
When executing, you may encounter an
issue as 'tools.jar' does not exists. This is because, You have installed Java
Runtime Environment (JRE) instead of the Java Development Kit (JDK). JRE
doesn't have a tools.jar, We need to set JAVA_HOME and PATH variables and point
to JDK, not to JRE.
When you run
an Ant build from Eclipse by right clicking and selecting Ant Build, you should
see a message as ''BUILD SUCCESSFUL" as below:
Buildfile:
D:\Ant\SampleAntProject\build.xml
BUILD SUCCESSFUL
Total time: 903 milliseconds
No comments:
Post a Comment