Using Ant
A
project has three attributes:
name
default
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.
Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used.
A target has the following attributes:
name
depends
a comma-separated list of names of targets on which this target depends.
if
the name of the property that must be set in order for this target to execute.
unless
the name of the property that must not be set in order for this target to execute.
description
a short description of this target's function.
A target can depend on other targets.
eg,
<
target
name
="A"
/>
< target name ="B" depends ="A" />
< target name ="C" depends ="B" />
< target name ="D" depends ="C,B,A" />
< target name ="B" depends ="A" />
< target name ="C" depends ="B" />
< target name ="D" depends ="C,B,A" />
Explain:
Suppose we want to execute target D. From its
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.
Note: Ant will only check whether the property has been set, the value doesn't matter. A property set to the empty string is still an existing property.
eg
<
target
name
="build-module-A"
if
="module-A-present"
/>
< target name ="build-own-fake-module-A" unless ="module-A-present" />
< target name ="build-own-fake-module-A" unless ="module-A-present" />
In the first example, if the
module-A-present
property is set (to any value, e.g. false), the target will be run. In the second example, if the module-A-present
property is set (again, to any value), the target will not be run.
If you want to check multiple conditions, you can use a dependend target for computing the result for the check:
<
target
name
="myTarget"
depends
="myTarget.check"
if
="myTarget.run"
>
< echo >Files foo.txt and bar.txt are present. </ echo >
</ target >
< target name ="myTarget.check" >
< condition property ="myTarget.run" >
< and >
< available file ="foo.txt" />
< available file ="bar.txt" />
</ and >
</ condition >
</ target >
< echo >Files foo.txt and bar.txt are present. </ echo >
</ target >
< target name ="myTarget.check" >
< condition property ="myTarget.run" >
< and >
< available file ="foo.txt" />
< available file ="bar.txt" />
</ and >
</ condition >
</ target >
A task is a piece of code that can be executed.
Tasks have a common structure:
<
name
attribute1
="value1"
attribute2
="value2" ...
/>
A project can have a set of properties.
Properties may be used in the value of task attributes. This is done by placing the property name between "
${
" and "}
" in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes
. This is resolved at run-time as build/classes
.
In the event you should need to include this construct literally (i.e. without property substitutions), simply "escape" the '$' character by doubling it. To continue the previous example:
<
echo
>$${builddir}=${builddir}
</
echo
>
will echo this message:
${builddir}=build/classes
Example Buildfile
<
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 >
< 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 >
one or more Resource Collections can be specified as nested elements (these must consist of file-type resources only). Additionally, it should be noted that although resource collections are processed in the order encountered, certain resource collection types such as fileset, dirset and files are undefined in terms of order.
<
classpath
>
< pathelement path ="${classpath}" />
< fileset dir ="lib" >
< include name ="**/*.jar" />
</ fileset >
< pathelement location ="classes" />
< dirset dir ="${build.dir}" >
< include name ="apps/**/classes" />
< exclude name ="apps/**/*Test*" />
</ dirset >
< filelist refid ="third-party_jars" />
</ classpath >
< pathelement path ="${classpath}" />
< fileset dir ="lib" >
< include name ="**/*.jar" />
</ fileset >
< pathelement location ="classes" />
< dirset dir ="${build.dir}" >
< include name ="apps/**/classes" />
< exclude name ="apps/**/*Test*" />
</ dirset >
< filelist refid ="third-party_jars" />
</ classpath >
This builds a path that holds the value of
${classpath}
, followed by all jar files in the lib
directory, the classes
directory, all directories named classes
under the apps
subdirectory of ${build.dir}
, except those that have the text Test
in their name, and the files specified in the referenced FileList.
Note:
Fileset
A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors.
A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors.
A path-like structure can include a reference to another path-like structure (a path being itself a resource collection) via nested
<path>
elements:
<
path
id
="base.path"
>
< pathelement path ="${classpath}" />
< fileset dir ="lib" >
< include name ="**/*.jar" />
</ fileset >
< pathelement location ="classes" />
</ path >
< path id ="tests.path" >
< path refid ="base.path" />
< pathelement location ="testclasses" />
</ path >
< pathelement path ="${classpath}" />
< fileset dir ="lib" >
< include name ="**/*.jar" />
</ fileset >
< pathelement location ="classes" />
</ path >
< path id ="tests.path" >
< path refid ="base.path" />
< pathelement location ="testclasses" />
</ path >
The shortcuts previously mentioned for
<classpath>
are also valid for <path>
.For example:
<
path
id
="base.path"
>
< pathelement path ="${classpath}" />
</ path >
< pathelement path ="${classpath}" />
</ path >
can be written as:
<
path
id
="base.path"
path
="${classpath}"
/>
Typic Builder.xml of JSF
<
project
name
="JSFDemo"
basedir
="../"
default
="deploy"
>
<!-- Project settings -->
< property name ="project.distname" value ="JSFDemo" />
<!-- Local system paths -->
< property file ="${basedir}/ant/build.properties" />
< property name ="webroot.dir" value ="${basedir}/WebContent" />
< property name ="webinf.dir" value ="${webroot.dir}/WEB-INF" />
< property name ="build.dir" value ="build" />
<!-- classpath for JSF 1.1.01 -->
< path id ="compile.classpath" >
< pathelement path ="${webinf.dir}/lib/commons-beanutils.jar" />
< pathelement path ="${webinf.dir}/lib/commons-collections.jar" />
< pathelement path ="${webinf.dir}/lib/commons-digester.jar" />
< pathelement path ="${webinf.dir}/lib/commons-logging.jar" />
< pathelement path ="${webinf.dir}/lib/jsf-api.jar" />
< pathelement path ="${webinf.dir}/lib/jsf-impl.jar" />
< pathelement path ="${webinf.dir}/lib/jstl.jar" />
< pathelement path ="${webinf.dir}/lib/standard.jar" />
< pathelement path ="${webinf.dir}/classes" />
< pathelement path ="${classpath.external}" />
< pathelement path ="${classpath}" />
</ path >
<!-- define your folder for deployment -->
< property name ="deploy.dir" value ="deploy" />
<!-- Check timestamp on files -->
< target name ="prepare" >
< tstamp />
</ target >
<!-- Copy any resource or configuration files -->
< target name ="resources" >
< copy todir ="${webinf.dir}/classes" includeEmptyDirs ="no" >
< fileset dir ="JavaSource" >
< patternset >
< include name ="**/*.conf" />
< include name ="**/*.properties" />
< include name ="**/*.xml" />
</ patternset >
</ fileset >
</ copy >
</ target >
<!-- Normal build of application -->
< target name ="compile" depends ="prepare,resources" >
< javac srcdir ="JavaSource" destdir ="${webinf.dir}/classes" >
< classpath refid ="compile.classpath" />
</ javac >
</ target >
<!-- Remove classes directory for clean build -->
< target name ="clean"
description ="Prepare for clean build" >
< delete dir ="${webinf.dir}/classes" />
< mkdir dir ="${webinf.dir}/classes" />
</ target >
<!-- Build entire project -->
< target name ="build" depends ="prepare,compile" />
< target name ="rebuild" depends ="clean,prepare,compile" />
<!-- Create binary distribution -->
< target name ="war" depends ="build" >
< mkdir dir ="${build.dir}" />
< war
basedir ="${webroot.dir}"
warfile ="${build.dir}/${project.distname}.war"
webxml ="${webinf.dir}/web.xml" >
< exclude name ="WEB-INF/${build.dir}/**" />
< exclude name ="WEB-INF/src/**" />
< exclude name ="WEB-INF/web.xml" />
</ war >
</ target >
< target name ="deploy" depends ="war" >
< delete file ="${deploy.dir}/${project.distname}.war" />
< delete dir ="${deploy.dir}/${project.distname}" />
< copy file ="${build.dir}/${project.distname}.war" todir ="${deploy.dir}" />
</ target >
</ project >
<!-- Project settings -->
< property name ="project.distname" value ="JSFDemo" />
<!-- Local system paths -->
< property file ="${basedir}/ant/build.properties" />
< property name ="webroot.dir" value ="${basedir}/WebContent" />
< property name ="webinf.dir" value ="${webroot.dir}/WEB-INF" />
< property name ="build.dir" value ="build" />
<!-- classpath for JSF 1.1.01 -->
< path id ="compile.classpath" >
< pathelement path ="${webinf.dir}/lib/commons-beanutils.jar" />
< pathelement path ="${webinf.dir}/lib/commons-collections.jar" />
< pathelement path ="${webinf.dir}/lib/commons-digester.jar" />
< pathelement path ="${webinf.dir}/lib/commons-logging.jar" />
< pathelement path ="${webinf.dir}/lib/jsf-api.jar" />
< pathelement path ="${webinf.dir}/lib/jsf-impl.jar" />
< pathelement path ="${webinf.dir}/lib/jstl.jar" />
< pathelement path ="${webinf.dir}/lib/standard.jar" />
< pathelement path ="${webinf.dir}/classes" />
< pathelement path ="${classpath.external}" />
< pathelement path ="${classpath}" />
</ path >
<!-- define your folder for deployment -->
< property name ="deploy.dir" value ="deploy" />
<!-- Check timestamp on files -->
< target name ="prepare" >
< tstamp />
</ target >
<!-- Copy any resource or configuration files -->
< target name ="resources" >
< copy todir ="${webinf.dir}/classes" includeEmptyDirs ="no" >
< fileset dir ="JavaSource" >
< patternset >
< include name ="**/*.conf" />
< include name ="**/*.properties" />
< include name ="**/*.xml" />
</ patternset >
</ fileset >
</ copy >
</ target >
<!-- Normal build of application -->
< target name ="compile" depends ="prepare,resources" >
< javac srcdir ="JavaSource" destdir ="${webinf.dir}/classes" >
< classpath refid ="compile.classpath" />
</ javac >
</ target >
<!-- Remove classes directory for clean build -->
< target name ="clean"
description ="Prepare for clean build" >
< delete dir ="${webinf.dir}/classes" />
< mkdir dir ="${webinf.dir}/classes" />
</ target >
<!-- Build entire project -->
< target name ="build" depends ="prepare,compile" />
< target name ="rebuild" depends ="clean,prepare,compile" />
<!-- Create binary distribution -->
< target name ="war" depends ="build" >
< mkdir dir ="${build.dir}" />
< war
basedir ="${webroot.dir}"
warfile ="${build.dir}/${project.distname}.war"
webxml ="${webinf.dir}/web.xml" >
< exclude name ="WEB-INF/${build.dir}/**" />
< exclude name ="WEB-INF/src/**" />
< exclude name ="WEB-INF/web.xml" />
</ war >
</ target >
< target name ="deploy" depends ="war" >
< delete file ="${deploy.dir}/${project.distname}.war" />
< delete dir ="${deploy.dir}/${project.distname}" />
< copy file ="${build.dir}/${project.distname}.war" todir ="${deploy.dir}" />
</ target >
</ project >
本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/172597,如需转载请自行联系原作者