PROJECT
A project has three attributes:
TARGET
A target is a container of tasks that cooperate to reach a desired state during the build process.
Targets can depend on other targets and Apache Ant ensures that these other targets have been executed before the current target. For example you might have a target for compiling and a target for creating a distributable. You can only build a distributable when you have compiled first, so the distribute target depends on the compile target.
Ant tries to execute the targets in the
A project has three attributes:
| Attribute | Description | Required |
| name | the name of the project. | No |
| default | the default target to use when no target is supplied. | No; however, since Ant 1.6.0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option. |
| basedir | the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used. |
TARGET
A target is a container of tasks that cooperate to reach a desired state during the build process.
Targets can depend on other targets and Apache Ant ensures that these other targets have been executed before the current target. For example you might have a target for compiling and a target for creating a distributable. You can only build a distributable when you have compiled first, so the distribute target depends on the compile target.
Ant tries to execute the targets in the
depends attribute
in the order they appear (from left to right). Keep in mind that it is
possible that a target can get executed earlier when an earlier target
depends on it:Suppose we want to execute target D. From its<target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/>
depends attribute,
you might think that first target C, then B and then A is executed.
Wrong! C depends on B, and B depends on A, so first A is executed, then
B, then C, and finally D.Call-Graph: A --> B --> C --> D
SAMPLE ANT FILE
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
mkdir/copy/delete-
mkdir dir="${webapp}"/><mkdir dir="${web-inf}"/><mkdir dir="${classes}"/><mkdir dir="${resources}"/><delete file="${res}/beans.xml"/><copy file="${res}/beans.xml.template" tofile="${res}/beans.xml"/><replace file="${res}/beans.xml" replacefilterfile="build.properties"/>UPDATE SVN REVISION<target name="updateSVNRevision">
<description>This task adds svn info in output files</description><typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath"/><echo message="Subversion repository url: ${repository.url}"/><echo message="Highest revision number : ${revision.max}"/><echo message="Highest last commited revision number : ${committed.max}"/><echo message="Revision range if workingCopy is mixed : ${revision.range}"/><echo message="Build version is : ${build.version}"/><echo message="Build Date ( mm/dd/yyyy hh:mm:ss aa ) is : ${current.timestamp}"/>
</target>
USAGE OF FILESET COMMAND Groups all files in directory${server.src}that are Java source files and don't have the textTestin their name.<fileset dir="${server.src}" casesensitive="yes"> <include name="**/*.java"/> <exclude name="**/*Test*"/> </fileset>Create war file example:<target name="war" depends="copy-resources, compileOnly"> <war destfile="${output}/${war.file}.war" webxml="${web}/WEB-INF/web.xml"> <fileset dir="${webapp}"> <include name="**/*.*" /> <exclude name="**/servlet-api.jar"/> </fileset> </war> </target>Copy-resource example<target name="copy-resources" depends="clean,init"> <mkdir dir="${towebserver}"/> <mkdir dir="${webapp}"> <copy todir="${webapp}"> <fileset dir="${web}"> <exclude name="*.jsp"> <exclude name="*.jspf"> <exclude name="**/WEB-INF/**"/> <fileset> </copy>Creating Jar file using ANT ScriptHow to start and stop Tomcat from ANT
MARCH 25, 2006 113 COMMENTSThis approach is not based on the Tomcat Ant Tasks or a brute force <exec dir=”${tomcat.home}/bin” executable=”startup.bat”/> kind of approach.After wading through startup.bat and catalina.bat I figured that they boil down to the following:
123456789101112<targetname="tomcat-start"><javajar="${tomcat.home}/bin/bootstrap.jar"fork="true"><jvmargvalue="-Dcatalina.home=${tomcat.home}"/></java></target><targetname="tomcat-stop"><javajar="${tomcat.home}/bin/bootstrap.jar"fork="true"><jvmargvalue="-Dcatalina.home=${tomcat.home}"/><argline="stop"/></java></target>You can have in build.properties, something like the following entry:tomcat.home=E:/peter/opt/apache-tomcat-5.5.12To execute the jar task, wrap it inside a target, most commonly, the build or package target, and execute them.<target name="build-jar"> <jar destfile="${web.dir}/lib/util.jar" basedir="${build.dir}/classes" includes="faxapp/util/**" excludes="**/Test.class"> <manifest> <attribute name="Main-Class" value="com.tutorialspoint.util.FaxUtil"/> </manifest> </jar> </target>Running Ant on this file creates the util.jar file for us.The following outcome is the result of running the Ant file:C:\>ant build-jar Buildfile: C:\build.xml BUILD SUCCESSFUL Total time: 1.3 seconds