ant学习笔记:
ant:一款针对JAVA的代码编译、管理、处理其依赖关系的工具。因其灵活、易用,故被大量使用。
*ant安装:
1.下载最新ant软件包:
http://ant.apache.org/ 官方网站上下载最新的对应版本,liunx下载tar.gz,window下载zip。
2.下载JDK
3.安装JDK
4.设置JDK环境变量
JAVA_HOME: JDK安装更目录
CLASSPATH: 定义类加载的路径。定位到JDK安装根目录下的lib目录,将当前目录添加到类路径下。
PATH: 定义JAVA,JAVAC命令的路径,将JDK/JRE的bin目录添加到path中。
5.设置ANT环境变量
ANT_HOME: ant包的根目录。
PATH: 将ant包的bin目录添加到path中。
* ant安装测试:
进入命令行,输入ant -verion 、ant -h 显示ant 的版本信息,帮助信息表示ant安装成功。
* ant是基于XML文件的配置,默认配置文件为build.xml,先看一个例子:
<?xml version="1.0" ?>
<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>
简单的讲,build.xml文件有多个target组成。
* 下面解析build.xml文件的构成,XML文件配置基本格式如下:
1.project 元素: 用于定义一个工程,通常一个文件仅包含一个工程的配置
name: 工程名
basedir: 相对路径,可以被basedir属性覆盖,如果basedir设置,此路径将失效。如果两者都没有设置,就以build.xml文件的目录为basedir。
default: 默认执行的target
例子:
<project name="MyProject" default="dist" basedir=".">
2.target 元素: 用于定义一个执行任务,ant的核心内容
name: target名字
depends: 依赖的target名字,依赖性确定了target执行顺序
if: 其值为某个属性值,进行依赖性检查,当某个属性值设置,才执行该target
unless: 其值为某个属性值,进行依赖性检查,当某个属性值没有设置,才执行该target
description: target的描述信息,当你使用-projecthelp命令行选项时,该信息将会被打印到控制台,用于表示build.xml的执行顺序
重点:target依赖关系,用于定义target执行的先后顺序,如:
<target name="clean" />
<target name="init" depends="clean" />
<target name="compile" depends="init" />
<target name="package" depends="complie" />
通过depends控制执行的顺序为 clean -> init -> compiler -> package
if、unless用于检查属性是否设置,来控制该target是否执行。
例子:
<target name="clean" description="clean old file">
<delete dir="${build}" />
<delete dir="${dest}" />
</target>
<target name="init" depends="clean" description="init process">
<tstamp/>
<mkdir dir="${build}" />
<mkdir dir="${dest}" />
<copy todir="${build}">
<fileset dir="${src}" exclude="**\*.java" /> <!-- ** 表示所有目录 , *.java表示java源文件-->
</copy>
</target>
<target name="compile" depends="init" description="complie the source file" >
<javac basedir="${src}" destdir="${build}" />
</target>
3.properties 元素: 定义一个属性,用于给target元素使用。
该元素可以通过六中方式加载属性:
3.1 name: 属性名
value: 属性值,可以是一个字符串,也可以是其他属性的的name,通过${name}引用其他属性的value
3.2 name: 属性名
location: 属性值,通常用于定义路径,只要是用于解决不同平台下路径分隔符的异同。
3.3 url: 通过读取网络资源加载在其中定义的property
3.4 resource:通过读取资源文件的方式加载在其中定义的property
3.5 file: 通过读取文件方式的加载在其中定义的property
3.6 env :
例子:
设置foo.dist到dist值
<property name="foo.dist" value="dist"/>
读取foo.properties,加载其中定义的属性值
<property file="foo.properties"/>
读取URL指定的资源,从其中加载属性值
<property url="http://www.mysite.com/bla/props/foo.properties"/>
reads a set of properties from a resource called "foo.properties".
<property resource="foo.properties"/>
<property file="${user.home}/.ant-global.properties"/>
since the "user.home" property is defined by the Java virtual machine to be your home directory. Where the "user.home" property resolves to in the file system depends on the operating system version and the JVM implementation. On Unix based systems, this will map to the user's home directory. On modern Windows variants, this will most likely resolve to the user's directory in the "Documents and Settings" folder. Older windows variants such as Windows 98/ME are less predictable, as are other operating system/JVM combinations.
<property environment="env"/>
<echo message="Number of Processors = ${env.NUMBER_OF_PROCESSORS}"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>
* 其他元素:
<echo>helloword</echo> 输出内容到控制台
<description>this is a project description</description> 工程的描述信息
* 文件操作命令:
1. copy、copydir、copyfile
1.1 copy
copy当个文件 <copy file="myfile.txt" tofile="mycopy.txt"/>
copy文件到目录 <copy file="myfile.txt" todir="../some/other/dir"/>
copy整个路径到另一个路径,借助fileset 的 dir属性
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
copy某个路径的部分文件到另一路径,通过exclude排除部分文件,也可以通过include
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
等同于
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
给copy过去的文件添加一个后缀名.bak
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<globmapper from="*" to="*.bak"/>
</copy>
delete
删除某个文件 <delete file="/lib/ant.jar"/>
删除一个目录 <delete dir="lib"/>
删除所有名字以.bak结尾的文件。模糊匹配
<delete>
<fileset dir="." includes="**/*.bak"/>
</delete>
删除所有的子文件夹和子文件。包括build自身。
<delete includeemptydirs="true">
<fileset dir="build" includes="**/*"/>
</delete>
执行系统命令
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
jar:
<jar destfile="${dist}/lib/app.jar" basedir="${build}/classes"/>
jars all files in the ${build}/classes directory into a file called app.jar in the ${dist}/lib directory.
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
excludes="**/Test.class"
/>
jars all files in the ${build}/classes directory into a file called app.jar in the ${dist}/lib directory. Files with the name Test.class are excluded.
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class"
/>
jars all files in the ${build}/classes directory into a file called app.jar in the ${dist}/lib directory. Only files under the directory mypackage/test are used, and files with the name Test.class are excluded.
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"
excludes="**/Test.class"/>
<fileset dir="${src}/resources"/>
</jar>
jars all files in the ${build}/classes directory and also in the ${src}/resources directory together into a file called app.jar in the ${dist}/lib directory. Files with the name Test.class are excluded. If there are files such as ${build}/classes/mypackage/MyClass.class and ${src}/resources/mypackage/image.gif, they will appear in the same directory in the JAR (and thus be considered in the same package by Java).
<jar destfile="test.jar" basedir=".">
<include name="build"/>
<manifest>
<!-- Who is building this jar? -->
<attribute name="Built-By" value="kev"/>
<!-- Information about the program itself -->
<attribute name="Implementation-Vendor" value="ACME inc."/>
<attribute name="Implementation-Title" value="GreatProduct"/>
<attribute name="Implementation-Version" value="1.0.0beta2"/>
<!-- details -->
<section name="common/class1.class">
<attribute name="Sealed" value="false"/>
</section>
</manifest>
</jar>
This is an example of an inline manifest specification including the version of the build program (Implementation-Version). Note that the Built-By attribute will take the value of the Ant property kev. The manifest produced by the above would look like this:
Manifest-Version: 1.0
Built-By: conor
Implementation-Vendor: ACME inc.
Implementation-Title: GreatProduct
Implementation-Version: 1.0.0beta2
Created-By: Apache Ant 1.7.0
Name: common/MyClass.class
Sealed: false
The following shows how to create a jar file specifing a service with an implementation of the JDK6 scripting interface:
<jar jarfile="pinky.jar">
<fileset dir="build/classes"/>
<service type="javax.script.ScriptEngineFactory"
provider="org.acme.PinkyLanguage"/>
</jar>
The following shows how to create a jar file specifing a service with two implementations of the JDK6 scripting interface:
<jar jarfile="pinkyandbrain.jar">
<fileset dir="classes"/>
<service type="javax.script.ScriptEngineFactory">
<provider classname="org.acme.PinkyLanguage"/>
<provider classname="org.acme.BrainLanguage"/>
</service>
</jar>
javac:
<javac srcdir="${src}"
destdir="${build}"
classpath="xyz.jar"
debug="on"
source="1.4"
/>
compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. The classpath used includes xyz.jar, and compiling with debug information is on. The source level is 1.4, so you can use assert statements.
<javac srcdir="${src}"
destdir="${build}"
fork="true"
source="1.2"
target="1.2"
/>
compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. This will fork off the javac compiler using the default javac executable. The source level is 1.2 (similar to 1.1 or 1.3) and the class files should be runnable under JDK 1.2+ as well.
<javac srcdir="${src}"
destdir="${build}"
fork="java$javac.exe"
source="1.5"
/>
compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. This will fork off the javac compiler, using the executable named java$javac.exe. Note that the $ sign needs to be escaped by a second one. The source level is 1.5, so you can use generics.
<javac srcdir="${src}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
compiles .java files under the ${src} directory, and stores the .class files in the ${build} directory. The classpath used includes xyz.jar, and debug information is on. Only files under mypackage/p1 and mypackage/p2 are used. All files in and below the mypackage/p1/testpackage directory are excluded from compilation. You didn't specify a source or target level, so the actual values used will depend on which JDK you ran Ant with.
<javac srcdir="${src}:${src2}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
is the same as the previous example, with the addition of a second source path, defined by the property src2. This can also be represented using nested <src> elements as follows:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>
If you want to run the javac compiler of a different JDK, you should tell Ant, where to find the compiler and which version of JDK you will be using so it can choose the correct command line switches. The following example executes a JDK 1.1 javac in a new process and uses the correct command line switches even when Ant is running in a Java VM of a different version:
<javac srcdir="${src}"
destdir="${build}"
fork="yes"
executable="/opt/java/jdk1.1/bin/javac"
compiler="javac1.1"
/>
Note: If you wish to compile only source files located in certain packages below a common root, use the include/exclude attributes or <include>/<exclude> nested elements to filter for these packages. Do not include part of your package structure in the srcdir attribute (or nested <src> elements), or Ant will recompile your source files every time you run your compile target. See the Ant FAQ for additional information.
If you wish to compile only files explicitly specified and disable javac's default searching mechanism then you can unset the sourcepath attribute:
<javac sourcepath="" srcdir="${src}"
destdir="${build}" >
<include name="**/*.java"/>
<exclude name="**/Example.java"/>
</javac>
That way the javac will compile all java source files under "${src}" directory but skip the examples. The compiler will even produce errors if some of the non-example files refers to them.
If you wish to compile with a special JDK (another than the one Ant is currently using), set the executable and fork attribute. Using taskname could show in the log, that these settings are fix.
<javac srcdir=""
destdir=""
executable="path-to-java14-home/bin/javac"
fork="true"
taskname="javac1.4" />
Note: If you are using Ant on Windows and a new DOS window pops up for every use of an external compiler, this may be a problem of the JDK you are using. This problem may occur with all JDKs < 1.2.
If you want to activate other compiler options like lint you could use the <compilerarg> element:
<javac srcdir="src"
destdir="${classes.dir}"
classpathref="libraries">
<compilerarg value="-Xlint"/>
</javac>
ZIP命令:
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
/>
zips all files in the htdocs/manual directory into a file called manual.zip in the ${dist} directory.
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
update="true"
/>
zips all files in the htdocs/manual directory into a file called manual.zip in the ${dist} directory. If manual.zip doesn't exist, it is created; otherwise it is updated with the new/changed files.
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
excludes="mydocs/**, **/todo.html"
/>
zips all files in the htdocs/manual directory. Files in the directory mydocs, or files with the name todo.html are excluded.
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
includes="api/**/*.html"
excludes="**/todo.html"
/>
zips all files in the htdocs/manual directory. Only html files under the directory api are zipped, and files with the name todo.html are excluded.
<zip destfile="${dist}/manual.zip">
<fileset dir="htdocs/manual"/>
<fileset dir="." includes="ChangeLog.txt"/>
</zip>
zips all files in the htdocs/manual directory, and also adds the file ChangeLog.txt in the current directory. ChangeLog.txt will be added to the top of the ZIP file, just as if it had been located at htdocs/manual/ChangeLog.txt.
<zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipfileset dir="." includes="ChangeLog27.txt" fullpath="docs/ChangeLog.txt"/>
<zipfileset src="examples.zip" includes="**/*.html" prefix="docs/examples"/>
</zip>
zips all files in the htdocs/manual directory into the docs/user-guide directory in the archive, adds the file ChangeLog27.txt in the current directory as docs/ChangeLog.txt, and includes all the html files in examples.zip under docs/examples. The archive might end up containing the files:
docs/user-guide/html/index.html
docs/ChangeLog.txt
docs/examples/index.html
The code
<zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipgroupfileset dir="." includes="examples*.zip"/>
</zip>
zips all files in the htdocs/manual directory into the docs/user-guide directory in the archive and includes all the files in any file that maches examples*.zip, such as all files within examples1.zip or examples_for_brian.zip.
<zip dest="release.zip">
<tarfileset src="release.tar"/>
</zip>
UNZIP:
<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
UNZIP:
unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
UNwar:
<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
Unjar:
<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
War:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
using Ant's default manifest file. The content of WEB-INF/web.xml is identical to src/metadata/myapp.xml.
We regulary receive
ant:一款针对JAVA的代码编译、管理、处理其依赖关系的工具。因其灵活、易用,故被大量使用。
*ant安装:
1.下载最新ant软件包:
http://ant.apache.org/ 官方网站上下载最新的对应版本,liunx下载tar.gz,window下载zip。
2.下载JDK
3.安装JDK
4.设置JDK环境变量
JAVA_HOME: JDK安装更目录
CLASSPATH: 定义类加载的路径。定位到JDK安装根目录下的lib目录,将当前目录添加到类路径下。
PATH: 定义JAVA,JAVAC命令的路径,将JDK/JRE的bin目录添加到path中。
5.设置ANT环境变量
ANT_HOME: ant包的根目录。
PATH: 将ant包的bin目录添加到path中。
* ant安装测试:
进入命令行,输入ant -verion 、ant -h 显示ant 的版本信息,帮助信息表示ant安装成功。
* ant是基于XML文件的配置,默认配置文件为build.xml,先看一个例子:
<?xml version="1.0" ?>
<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>
简单的讲,build.xml文件有多个target组成。
* 下面解析build.xml文件的构成,XML文件配置基本格式如下:
1.project 元素: 用于定义一个工程,通常一个文件仅包含一个工程的配置
name: 工程名
basedir: 相对路径,可以被basedir属性覆盖,如果basedir设置,此路径将失效。如果两者都没有设置,就以build.xml文件的目录为basedir。
default: 默认执行的target
例子:
<project name="MyProject" default="dist" basedir=".">
2.target 元素: 用于定义一个执行任务,ant的核心内容
name: target名字
depends: 依赖的target名字,依赖性确定了target执行顺序
if: 其值为某个属性值,进行依赖性检查,当某个属性值设置,才执行该target
unless: 其值为某个属性值,进行依赖性检查,当某个属性值没有设置,才执行该target
description: target的描述信息,当你使用-projecthelp命令行选项时,该信息将会被打印到控制台,用于表示build.xml的执行顺序
重点:target依赖关系,用于定义target执行的先后顺序,如:
<target name="clean" />
<target name="init" depends="clean" />
<target name="compile" depends="init" />
<target name="package" depends="complie" />
通过depends控制执行的顺序为 clean -> init -> compiler -> package
if、unless用于检查属性是否设置,来控制该target是否执行。
例子:
<target name="clean" description="clean old file">
<delete dir="${build}" />
<delete dir="${dest}" />
</target>
<target name="init" depends="clean" description="init process">
<tstamp/>
<mkdir dir="${build}" />
<mkdir dir="${dest}" />
<copy todir="${build}">
<fileset dir="${src}" exclude="**\*.java" /> <!-- ** 表示所有目录 , *.java表示java源文件-->
</copy>
</target>
<target name="compile" depends="init" description="complie the source file" >
<javac basedir="${src}" destdir="${build}" />
</target>
3.properties 元素: 定义一个属性,用于给target元素使用。
该元素可以通过六中方式加载属性:
3.1 name: 属性名
value: 属性值,可以是一个字符串,也可以是其他属性的的name,通过${name}引用其他属性的value
3.2 name: 属性名
location: 属性值,通常用于定义路径,只要是用于解决不同平台下路径分隔符的异同。
3.3 url: 通过读取网络资源加载在其中定义的property
3.4 resource:通过读取资源文件的方式加载在其中定义的property
3.5 file: 通过读取文件方式的加载在其中定义的property
3.6 env :
例子:
设置foo.dist到dist值
<property name="foo.dist" value="dist"/>
读取foo.properties,加载其中定义的属性值
<property file="foo.properties"/>
读取URL指定的资源,从其中加载属性值
<property url="http://www.mysite.com/bla/props/foo.properties"/>
reads a set of properties from a resource called "foo.properties".
<property resource="foo.properties"/>
<property file="${user.home}/.ant-global.properties"/>
since the "user.home" property is defined by the Java virtual machine to be your home directory. Where the "user.home" property resolves to in the file system depends on the operating system version and the JVM implementation. On Unix based systems, this will map to the user's home directory. On modern Windows variants, this will most likely resolve to the user's directory in the "Documents and Settings" folder. Older windows variants such as Windows 98/ME are less predictable, as are other operating system/JVM combinations.
<property environment="env"/>
<echo message="Number of Processors = ${env.NUMBER_OF_PROCESSORS}"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>
* 其他元素:
<echo>helloword</echo> 输出内容到控制台
<description>this is a project description</description> 工程的描述信息
* 文件操作命令:
1. copy、copydir、copyfile
1.1 copy
copy当个文件 <copy file="myfile.txt" tofile="mycopy.txt"/>
copy文件到目录 <copy file="myfile.txt" todir="../some/other/dir"/>
copy整个路径到另一个路径,借助fileset 的 dir属性
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
copy某个路径的部分文件到另一路径,通过exclude排除部分文件,也可以通过include
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
等同于
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
给copy过去的文件添加一个后缀名.bak
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<globmapper from="*" to="*.bak"/>
</copy>
delete
删除某个文件 <delete file="/lib/ant.jar"/>
删除一个目录 <delete dir="lib"/>
删除所有名字以.bak结尾的文件。模糊匹配
<delete>
<fileset dir="." includes="**/*.bak"/>
</delete>
删除所有的子文件夹和子文件。包括build自身。
<delete includeemptydirs="true">
<fileset dir="build" includes="**/*"/>
</delete>
执行系统命令
<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
jar:
<jar destfile="${dist}/lib/app.jar" basedir="${build}/classes"/>
jars all files in the ${build}/classes directory into a file called app.jar in the ${dist}/lib directory.
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
excludes="**/Test.class"
/>
jars all files in the ${build}/classes directory into a file called app.jar in the ${dist}/lib directory. Files with the name Test.class are excluded.
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class"
/>
jars all files in the ${build}/classes directory into a file called app.jar in the ${dist}/lib directory. Only files under the directory mypackage/test are used, and files with the name Test.class are excluded.
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"
excludes="**/Test.class"/>
<fileset dir="${src}/resources"/>
</jar>
jars all files in the ${build}/classes directory and also in the ${src}/resources directory together into a file called app.jar in the ${dist}/lib directory. Files with the name Test.class are excluded. If there are files such as ${build}/classes/mypackage/MyClass.class and ${src}/resources/mypackage/image.gif, they will appear in the same directory in the JAR (and thus be considered in the same package by Java).
<jar destfile="test.jar" basedir=".">
<include name="build"/>
<manifest>
<!-- Who is building this jar? -->
<attribute name="Built-By" value="kev"/>
<!-- Information about the program itself -->
<attribute name="Implementation-Vendor" value="ACME inc."/>
<attribute name="Implementation-Title" value="GreatProduct"/>
<attribute name="Implementation-Version" value="1.0.0beta2"/>
<!-- details -->
<section name="common/class1.class">
<attribute name="Sealed" value="false"/>
</section>
</manifest>
</jar>
This is an example of an inline manifest specification including the version of the build program (Implementation-Version). Note that the Built-By attribute will take the value of the Ant property kev. The manifest produced by the above would look like this:
Manifest-Version: 1.0
Built-By: conor
Implementation-Vendor: ACME inc.
Implementation-Title: GreatProduct
Implementation-Version: 1.0.0beta2
Created-By: Apache Ant 1.7.0
Name: common/MyClass.class
Sealed: false
The following shows how to create a jar file specifing a service with an implementation of the JDK6 scripting interface:
<jar jarfile="pinky.jar">
<fileset dir="build/classes"/>
<service type="javax.script.ScriptEngineFactory"
provider="org.acme.PinkyLanguage"/>
</jar>
The following shows how to create a jar file specifing a service with two implementations of the JDK6 scripting interface:
<jar jarfile="pinkyandbrain.jar">
<fileset dir="classes"/>
<service type="javax.script.ScriptEngineFactory">
<provider classname="org.acme.PinkyLanguage"/>
<provider classname="org.acme.BrainLanguage"/>
</service>
</jar>
javac:
<javac srcdir="${src}"
destdir="${build}"
classpath="xyz.jar"
debug="on"
source="1.4"
/>
compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. The classpath used includes xyz.jar, and compiling with debug information is on. The source level is 1.4, so you can use assert statements.
<javac srcdir="${src}"
destdir="${build}"
fork="true"
source="1.2"
target="1.2"
/>
compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. This will fork off the javac compiler using the default javac executable. The source level is 1.2 (similar to 1.1 or 1.3) and the class files should be runnable under JDK 1.2+ as well.
<javac srcdir="${src}"
destdir="${build}"
fork="java$javac.exe"
source="1.5"
/>
compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. This will fork off the javac compiler, using the executable named java$javac.exe. Note that the $ sign needs to be escaped by a second one. The source level is 1.5, so you can use generics.
<javac srcdir="${src}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
compiles .java files under the ${src} directory, and stores the .class files in the ${build} directory. The classpath used includes xyz.jar, and debug information is on. Only files under mypackage/p1 and mypackage/p2 are used. All files in and below the mypackage/p1/testpackage directory are excluded from compilation. You didn't specify a source or target level, so the actual values used will depend on which JDK you ran Ant with.
<javac srcdir="${src}:${src2}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
is the same as the previous example, with the addition of a second source path, defined by the property src2. This can also be represented using nested <src> elements as follows:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>
If you want to run the javac compiler of a different JDK, you should tell Ant, where to find the compiler and which version of JDK you will be using so it can choose the correct command line switches. The following example executes a JDK 1.1 javac in a new process and uses the correct command line switches even when Ant is running in a Java VM of a different version:
<javac srcdir="${src}"
destdir="${build}"
fork="yes"
executable="/opt/java/jdk1.1/bin/javac"
compiler="javac1.1"
/>
Note: If you wish to compile only source files located in certain packages below a common root, use the include/exclude attributes or <include>/<exclude> nested elements to filter for these packages. Do not include part of your package structure in the srcdir attribute (or nested <src> elements), or Ant will recompile your source files every time you run your compile target. See the Ant FAQ for additional information.
If you wish to compile only files explicitly specified and disable javac's default searching mechanism then you can unset the sourcepath attribute:
<javac sourcepath="" srcdir="${src}"
destdir="${build}" >
<include name="**/*.java"/>
<exclude name="**/Example.java"/>
</javac>
That way the javac will compile all java source files under "${src}" directory but skip the examples. The compiler will even produce errors if some of the non-example files refers to them.
If you wish to compile with a special JDK (another than the one Ant is currently using), set the executable and fork attribute. Using taskname could show in the log, that these settings are fix.
<javac srcdir=""
destdir=""
executable="path-to-java14-home/bin/javac"
fork="true"
taskname="javac1.4" />
Note: If you are using Ant on Windows and a new DOS window pops up for every use of an external compiler, this may be a problem of the JDK you are using. This problem may occur with all JDKs < 1.2.
If you want to activate other compiler options like lint you could use the <compilerarg> element:
<javac srcdir="src"
destdir="${classes.dir}"
classpathref="libraries">
<compilerarg value="-Xlint"/>
</javac>
ZIP命令:
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
/>
zips all files in the htdocs/manual directory into a file called manual.zip in the ${dist} directory.
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
update="true"
/>
zips all files in the htdocs/manual directory into a file called manual.zip in the ${dist} directory. If manual.zip doesn't exist, it is created; otherwise it is updated with the new/changed files.
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
excludes="mydocs/**, **/todo.html"
/>
zips all files in the htdocs/manual directory. Files in the directory mydocs, or files with the name todo.html are excluded.
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
includes="api/**/*.html"
excludes="**/todo.html"
/>
zips all files in the htdocs/manual directory. Only html files under the directory api are zipped, and files with the name todo.html are excluded.
<zip destfile="${dist}/manual.zip">
<fileset dir="htdocs/manual"/>
<fileset dir="." includes="ChangeLog.txt"/>
</zip>
zips all files in the htdocs/manual directory, and also adds the file ChangeLog.txt in the current directory. ChangeLog.txt will be added to the top of the ZIP file, just as if it had been located at htdocs/manual/ChangeLog.txt.
<zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipfileset dir="." includes="ChangeLog27.txt" fullpath="docs/ChangeLog.txt"/>
<zipfileset src="examples.zip" includes="**/*.html" prefix="docs/examples"/>
</zip>
zips all files in the htdocs/manual directory into the docs/user-guide directory in the archive, adds the file ChangeLog27.txt in the current directory as docs/ChangeLog.txt, and includes all the html files in examples.zip under docs/examples. The archive might end up containing the files:
docs/user-guide/html/index.html
docs/ChangeLog.txt
docs/examples/index.html
The code
<zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipgroupfileset dir="." includes="examples*.zip"/>
</zip>
zips all files in the htdocs/manual directory into the docs/user-guide directory in the archive and includes all the files in any file that maches examples*.zip, such as all files within examples1.zip or examples_for_brian.zip.
<zip dest="release.zip">
<tarfileset src="release.tar"/>
</zip>
UNZIP:
<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
UNZIP:
unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
UNwar:
<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
Unjar:
<unzip src="${tomcat_src}/tools-src.zip" dest="${tools.home}"/>
<gunzip src="tools.tar.gz"/>
<untar src="tools.tar" dest="${tools.home}"/>
<unzip src="${tomcat_src}/tools-src.zip"
dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
</unzip>
<unzip dest="${tools.home}">
<patternset>
<include name="**/*.java"/>
<exclude name="**/Test*.java"/>
</patternset>
<fileset dir=".">
<include name="**/*.zip"/>
<exclude name="**/tmp*.zip"/>
</fileset>
</unzip>
<unzip src="apache-ant-bin.zip" dest="${tools.home}">
<patternset>
<include name="apache-ant/lib/ant.jar"/>
</patternset>
<mapper type="flatten"/>
</unzip>
Related tasks
<unzip src="some-archive" dest="some-dir">
<patternset>
<include name="some-pattern"/>
</patternset>
<mapper type="some-mapper"/>
</unzip>
is identical to
<copy todir="some-dir" preservelastmodified="true">
<zipfileset src="some-archive">
<patternset>
<include name="some-pattern"/>
</patternset>
</zipfileset>
<mapper type="some-mapper"/>
</copy>
The same is also true for <untar> and <tarfileset>. <copy> offers additional features like filtering files on the fly, allowing a file to be mapped to multiple destinations or a configurable file system timestamp granularity.
<zip destfile="new.jar">
<zipfileset src="old.jar">
<exclude name="do/not/include/this/class"/>
</zipfileset>
</zip>
War:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
using Ant's default manifest file. The content of WEB-INF/web.xml is identical to src/metadata/myapp.xml.
We regulary receive