Apache Ant™
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.
The latest version Ant1.8.2.
1. Ant安装(Windows)
- 下载ant最新版本并解压到文件目录
- 设置ANT_HOME环境变量
- JAVA_HOME目录
- 添加%ANT_HOME%\bin 到path
- 检查是否设置成功:ant -version
2. Ant构建文件(build.xml)
Build.xml由xml编写, ant构建文件对大小写敏感
3. 运行ant
ant命令:
ant [options] [target [target2 [target3] ...]]
Options:
-help, -h print this message
-projecthelp, -p print project help information
-version print the version information and exit
-diagnostics print information that might be helpful to
diagnose or report problems.
-quiet, -q be extra quiet
-verbose, -v be extra verbose
-debug, -d print debugging information
-emacs, -e produce logging information without adornments
-lib <path> specifies a path to search for jars and classes
-logfile <file> use given file for log
-l <file> ''
-logger <classname> the class which is to perform logging
-listener <classname> add an instance of class as a project listener
-noinput do not allow interactive input
-buildfile <file> use given buildfile
-file <file> ''
-f <file> ''
-D<property>=<value> use value for given property
-keep-going, -k execute all targets that do not depend
on failed target(s)
-propertyfile <name> load all properties from file with -D
properties taking precedence
-inputhandler <class> the class which will handle input requests
-find <file> (s)earch for buildfile towards the root of
-s <file> the filesystem and use it
-nice number A niceness value for the main thread:
1 (lowest) to 10 (highest); 5 is the default
-nouserlib Run ant without using the jar files from ${user.home}/.ant/lib
-noclasspath Run ant without using CLASSPATH
-autoproxy Java 1.5+ : use the OS proxies
-main <class> override Ant's normal entry point
Examples
ant
Ant将打开默认的构建文件,并执行默认的目标compile. 默认情况下,ant命令会在当前目录下寻找build.xml。
ant –buildfile test.xml
runs Ant using the test.xml file in the current directory, on the default target.
ant -buildfile test.xml dist
runs Ant using the test.xml file in the current directory, on the target called dist.
ant -buildfile test.xml -Dbuild=build/classes dist
runs Ant using the test.xml file in the current directory, on the target called dist, setting the build property to the value build/classes.
ant -lib /home/ant/extras
runs Ant picking up additional task and support jars from the /home/ant/extras location
ant -lib one.jar;another.jar
ant -lib one.jar -lib another.jar
adds two jars to Ants classpath.
4. Using Apache Ant
Structure of Buildfile
Task1 |
Task2 |
Property |
Path |
Taskn |
taskref |
Projects
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. |
e.g:
<project name="CopsTestComp" default="show property" basedir=”.”>
Targets
A target can depend on other targets. You might have a target for compiling, for example, 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 resolves these dependencies.
For example:
<target name="jar" depends="clean,build">
……
</target>
Tasks
A task is a piece of code that can be executed.
<target name="build">
<mkdir dir="${path.build}" /> //task1
<javac classpath="${path.build}" debug="true" srcdir="src" destdir="${path.build}"> //task2
<include name="**/*.java" />
<classpath refid= "compile.classpath "/>
</javac>
</target>
Properties
Properties are an important way to customize a build process or to just provide shortcuts for strings that are used repeatedly inside a build file.
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
Use of external tasks
Ant supports a plugin mechanism for using third party tasks. For using them you have to do two steps:
- place their implementation somewhere where Ant can find them
- declare them.
Don't add anything to the CLASSPATH environment variable - this is often the reason for very obscure errors. Use Ant's own mechanisms for adding libraries:
- via command line argument
-lib
- adding to
${user.home}/.ant/lib
- adding to
${ant.home}/lib
For the declaration there are several ways:
- declare a single task per using instruction using
<taskdef name="jag_connect" classname="com.sybase.jaguar.management.jagtool.ant.ConnectTask" />
- declare a bundle of tasks using a properties-file holding these taskname-ImplementationClass-pairs and
<taskdef>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" /> <for ... />
- declare a bundle of tasks using a xml-file holding these taskname-ImplementationClass-pairs and
<taskdef>
<taskdef resource="net/sf/antcontrib/antlib.xml" /> <for ... />
- declare a bundle of tasks using a xml-file named antlib.xml, XML-namespace and
antlib:
protocoll handler<project xmlns:ac="antlib:net.sf.antconrib"/> <ac:for ... />
Example Buildfile
<?xml version="1.0"?>
<project name="Test" default="show property" basedir=”.”>
<property name="jar.file" value="${ant.project.name}.jar" />
<property name="entity.name" value="Package:${ant.project.name}" />
<property name="list.type" value="Package" />
<property name="path.build" value="${basedir}/bin" />
<property name="path.jar" value="D:/jar/jar_hksg/" />
<property name="webs.jar" value="D:/jar/jar_hksg/" />
<!-- EAServer properties -->
<property name="jaguar.host" value="192.168.0.11" />
<property name="jaguar.port" value="9091" />
<property name="jaguar.user" value="addddd" />
<property name="jaguar.password" value="123456" />
<!-- import EAServer task -->
<taskdef name="jag_connect" classname="com.sybase.jaguar.management.jagtool.ant.ConnectTask" />
<taskdef name="jag_deploy" classname="com.sybase.jaguar.management.jagtool.ant.DeployTask" />
<taskdef name="jag_delete" classname="com.sybase.jaguar.management.jagtool.ant.DeleteTask" />
<taskdef name="jag_refresh" classname="com.sybase.jaguar.management.jagtool.ant.RefreshTask" />
<taskdef name="jag_restart" classname="com.sybase.jaguar.management.jagtool.ant.RestartTask" />
<taskdef name="jag_shutdown" classname="com.sybase.jaguar.management.jagtool.ant.ShutdownTask" />
<taskdef name="jag_list" classname="com.sybase.jaguar.management.jagtool.ant.ListTask" />
<target name="jar" depends="clean,build">
<delete file="${jar.file}"/>
<jar destfile="${jar.file}">
<fileset dir="${path.build}" includes="**/*.*" />
<fileset dir="${basedir}" includes="META-INF/**" />
</jar>
</target>
<target name="connect">
<jag_connect host="${jaguar.host}" port="${jaguar.port}" user="${jaguar.user}" password="${jaguar.password}" />
</target>
<target name="restart" depends="connect">
<jag_restart />
<waitfor checkevery="1" checkeveryunit="second">
<socket server="${jaguar.host}" port="${jaguar.port}" />
</waitfor>
</target>
<target name="shutdown" depends="connect">
<jag_shutdown />
</target>
<target name="deploy" depends="jar,connect">
<jag_deploy type="ejbjar" install="true" strategy="full" file="${basedir}/${jar.file}" verbose="true" />
<jag_refresh entity="${entity.name}" />
</target>
<target name="undeploy" depends="connect">
<jag_delete entity="${entity.name}" />
</target>
<target name="list" depends="connect">
<jag_list type="${list.type}" />
</target>
<target name="refresh" depends="connect">
<jag_refresh entity="${entity.name}" />
</target>
<target name="clean">
<delete includeEmptyDirs="true">
<fileset dir="${path.build}" />
</delete>
</target>
<target name="build">
<mkdir dir="${path.build}" />
<javac classpath="${path.build}" debug="true" srcdir="src" destdir="${path.build}">
<include name="**/*.java" />
<classpath refid= "compile.classpath "/>
</javac>
</target>
<path id= "compile.classpath ">
<pathelement path = "${path.jar}easj2ee.jar"/>
<pathelement path = "${path.jar}log4j-1.2.15.jar "/>
<pathelement path = "${path.jar}easserver.jar"/>
<pathelement path = "${path.jar}easclient.jar"/>
<pathelement path = "${path.jar}hksps.jar"/>
<pathelement path = "${path.jar}common.jar"/>
<pathelement path = "${path.jar}mbclnt50.jar"/>
</path>
<target name="show property">
<echo message="Host:${jaguar.host}"/>
<echo message="Port:${jaguar.port}"/>
</target>
</project>
The structure of the build.xml:
Stucture of build.xml
参考文档