Apache ANT Tutorial
Apache ANT Tutorial
Why do you need a build tool?
Do you spend your day doing the following manually?
1. Compile code
2. Package the binaries
3. Deploy the binaries to the test server
4. Test your changes
5. Copy code from one location to another
If you have answered yes to any of the above, then it is time to automate the process and take away that burden from you.
Installing Apache Ant
a) Ensure that the JAVA_HOME environment variable is set to the folder where your JDK is installed.
b) Download the binaries from http://ant.apache.org
c) Unzip the zip file to a convenient location using Winzip, winRAR, 7-zip or similar tools, say c:\ folder.
d) Create a new environment variable called ANT_HOME that points to the Ant installation folder, in this case c:\apache-ant-1.8.2-bin folder.
e) Append the path to the Apache Ant batch file to the PATH environment variable. In our case this would be the c:\apache-ant-1.8.2-bin\bin folder.
Or
f) Use eclipse
Ant - Build Files
Ant's build file, build.xml should live in the project's base directory. Although you are free to use other file names or place the build file in some other location.
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant tutorial test" default="create">
<target name="create">
<echo>This is the test file for ant tutorial using ${ant.version}</echo>
</target>
</project>
|
All buildfiles require the project element and at least one target element.
Attributes
|
Description
|
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)
|
Targets can have dependencies on other targets. For example, a deploy target may have a dependency on the package target, and the package target may have a dependency on the compile target and so forth. Dependencies are denoted using the depends attribute. For example:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant tutorial test" default="create">
<target name="create" depends="dependent">
<echo>This is the test file for ant tutorial using ${ant.version}</echo>
</target>
<target name="dependent">
<echo>dependent test target</echo>
</target>
</project>
|
Output:
Buildfile: D:\projects\eclipse workspace\XXX\build\build.xml
dependent:
[echo] dependent test target
create:
[echo] This is the test file for ant tutorial using Apache Ant(TM) version 1.8.2 compiled on December 20 2010
BUILD SUCCESSFUL
Total time: 270 milliseconds
|
The target element has the following attributes:
Attributes
|
Description
|
name
|
The name of the target (Required)
|
depends
|
Comma separated list of all targets that this target depends on. (Optional)
|
description
|
A short description of the target. (optional)
|
if
|
Allows the execution of a target based on the trueness of a conditional attribute. (optional)
|
unless
|
Adds the target to the dependency list of the specified Extension Point. An Extension Point is similar to a target, but it does not have any tasks. (Optional)
|
Ant - Property Task
Ant uses the property element which allows you to specify properties. This allows the properties to be changed from one build to another. or from one environment to another.
By default, Ant provides the following pre-defined properties that can be used in the build file
Properties
|
Description
|
ant.file
|
The full location of the build file.
|
ant.version
|
The version of the Apache Ant installation.
|
basedir
|
The basedir of the build, as specified in the basedir attribute of the project element.
|
ant.java.version
|
The version of the JDK that is used by Ant.
|
ant.project.name
|
The name of the project, as specified in the name atrribute of the project element
|
ant.project.default-target
|
The default target of the current project
|
ant.project.invoked-targets
|
Comma separated list of the targets that were invoked in the current project
|
ant.core.lib
|
The full location of the ant jar file
|
ant.home
|
The home directory of Ant installation
|
ant.library.dir
|
The home directory for Ant library files - typically ANT_HOME/lib folder.
|
In addition to the above, the user can define additional properties using the property element. An example is presented below which shows how to define a property called username:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant tutorial test" default="create">
<property name="username" value="anand"/>
<target name="create" depends="dependent">
<echo>This is the test file for ant tutorial using ${ant.version} run by ${username}</echo>
</target>
<target name="dependent">
<echo>dependent test target</echo>
</target>
</project>
|
Output:
Buildfile: D:\projects\eclipse workspace\fasttrack\build\build.xml
dependent:
[echo] dependent test target
create:
[echo] This is the test file for ant tutorial using Apache Ant(TM) version 1.8.2 compiled on December 20 2010 run by anand
BUILD SUCCESSFUL
Total time: 278 milliseconds
|
Ant - Property Files
Storing the properties in a separate file allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST and PROD environments.
There is no hard and fast rule, but typically the property file is named build.properties and is placed along side the build.xml file. You could create multiple build properties file based on the deployment environments - such as build.properties.dev and build.properties.test
Build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant tutorial test" default="create">
<property file="build.properties"/>
<target name="create" depends="dependent">
<echo>This is the test file for ant tutorial using ${ant.version} run by ${username}</echo>
</target>
<target name="dependent">
<echo>dependent test target. project name= ${project.name}</echo>
</target>
</project>
|
Build.properties
username=annad
project.name=test ant project
|
File Set
The Fileset data types represents a collection of files. The Fileset data type is usually used as a filter to include and exclude files that match a particular pattern.
<fileset dir=${src} casesensitive="yes">
<include name="**/*.java"/>
<exclude name="**/*test*"/>
</fileset>
|
In the above example, the fileset selects all java files in the source folder except those that contain the word test in them. The case sensitive filter is applied to the fileset which means that a file with the name SampleTest.java will not be excluded from the fileset
Pattern Set
A pattern set is a pattern that allows to easily filter files or folders based on certain patterns. Patterns can be created using the following meta characters.
1. ? - Matches one character only
2. * - Matches zero or many characters
3. ** - Matches zero or many directories recursively
<patternset id="files_without_dummy_classes">
<include name="**/*.java"/>
<exclude name="**/*test*"/>
</patternset>
<target name="build">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" failonerror="true">
<patternset refid="files_without_dummy_classes"/>
<src path="${src}"/>
</javac>
</target>
|
Ant - Building Projects
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant tutorial test" default="start">
<property file="build.properties"/>
<property name="src" location="..\src"/>
<target name="start" depends="build">
<echo>This is the test file for ant tutorial using ${ant.version} run by ${username}</echo>
<echo>${web.dir}</echo>
</target>
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<patternset id="files_without_dummy_classes">
<include name="**/*.java"/>
<exclude name="**/*test*"/>
</patternset>
<target name="build">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" failonerror="true" includeantruntime="false">
<src path="${src}"/>
<patternset refid="files_without_dummy_classes"/>
<classpath refid="master-classpath"/>
</javac>
</target>
<!-- <target name="clean" description="Clean output directories">
<delete>
<fileset dir="${build.dir}">
<patternset refid="files_without_dummy_classes"/>
<include name="**/*.class"/>
</fileset>
</delete>
</target> -->
</project>
|
Ant - Creating JAR files
Attributes
|
Description
|
basedir
|
The base directory for the output JAR file. By default, this is set to the base directory of the project.
|
compress
|
Advises ant to compress the file as it creates the JAR file.
|
keepcompression
|
While the compress attribute is applicable to the individual files, the keepcompression attribute does the same thing, but it applies to the entire archive.
|
destfile
|
The name of the output JAR file
|
duplicate
|
Advises Ant on what to do when duplicate files are found. You could add, preserve or fail the duplicate files.
|
excludes
|
Advises Ant to not include these comma seperated list of files in the package.
|
excludesfile
|
Same as above, except the exclude files are specified using a pattern.
|
inlcudes
|
Inverse of excludes
|
includesfile
|
Inverse of excludesfile.
|
update
|
Advises ant to overwrite files in the already built JAR file.
|
<jar destfile="${basedir}/jar/test.jar" basedir="${build.dir}"/>
|
If we want to make the util.jar an executable jar file we need to add the manifest with the Main-Class meta attribute.
Therefore the above example will be updated as:
<jar destfile="${basedir}/jar/test.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
|
Ant - Creating WAR files
Attributes
|
Description
|
webxml
|
Path to the web.xml file
|
lib
|
A grouping to specify what goes into the WEB-INF\lib folder.
|
classes
|
A grouping to specify what goes into the WEB-INF\classes folder.
|
metainf
|
Specifies the instructions for generating the MANIFEST.MF file.
|
<target name="war" description="create war">
<war destfile="${basedir}/war/test.war" webxml="${web.xml}/web.xml">
<classes dir="${build.dir}"/>
<!-- <lib dir="${web.xml}/lib">
</lib> -->
<fileset dir="${web.condent}">
<include name="**/*.*"/>
<!-- <include name="**/*.jar"/> -->
</fileset>
</war>
</target>
|
Final File:
Build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant tutorial test" default="start">
<property file="build.properties"/>
<property name="src" location="..\src"/>
<target name="start" depends="build,jar,war,clean">
<echo>This is the test file for ant tutorial using ${ant.version} run by ${username}</echo>
<echo>${build.dir}</echo>
</target>
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<patternset id="files_without_dummy_classes">
<include name="**/*.java"/>
<exclude name="**/*test*"/>
</patternset>
<target name="build">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" failonerror="true" includeantruntime="false">
<src path="${src}"/>
<patternset refid="files_without_dummy_classes"/>
<classpath refid="master-classpath"/>
</javac>
</target>
<target name="jar" description="create jar">
<jar destfile="${basedir}/jar/test.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<target name="war" description="create war">
<war destfile="${basedir}/war/test.war" webxml="${web.xml}/web.xml">
<classes dir="${build.dir}"/>
<!-- <lib dir="${web.xml}/lib">
</lib> -->
<fileset dir="${web.condent}">
<include name="**/*.*"/>
<!-- <include name="**/*.jar"/> -->
</fileset>
</war>
</target>
<target name="clean" description="Clean output directories">
<delete>
<fileset dir="${build.dir}">
<include name="**/*.class"/>
</fileset>
</delete>
</target>
</project>
|
Build.properties
username=annad
project.name=test ant project
base=${basedir}
build.dir=${basedir}/classes
web.dir=war
web.condent=../WebContent
web.xml=${web.condent}/WEB-INF/
|
Comments
Post a Comment