编译工具 之 ant
一、概述
需要设置的环境变量:
JAVA_HOME="D:\JDK",
ANT_HOME="D:\ant",
PATH=".,%JAVA_HOME%\bin,%ANT_HOME%bin"
运行:ant -buildfile test.xml -Dbuild=build/classes dist
(含义为:执行test.xml的编译脚本,参数build的值为build/classes,要执行的target为dist)
默认的编译脚本为build.xml,默认的target为编译脚本中的project的default属性值所对应的target
二、常用task
编译脚本是xml文档,元素有project->target->task,property,filter。
(右击前面查看图片)
task有内置的和可选的之分,可选task需要下载额外的包来支持,此外可以编写自己的task。
1、Echo
Examples
basedir="${build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class" />
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"
excludes="**/Test.class" />
<fileset dir="${src}/resources"/>
</jar>
4、javadoc
需要设置的环境变量:
JAVA_HOME="D:\JDK",
ANT_HOME="D:\ant",
PATH=".,%JAVA_HOME%\bin,%ANT_HOME%bin"
运行:ant -buildfile test.xml -Dbuild=build/classes dist
(含义为:执行test.xml的编译脚本,参数build的值为build/classes,要执行的target为dist)
默认的编译脚本为build.xml,默认的target为编译脚本中的project的default属性值所对应的target
二、常用task
编译脚本是xml文档,元素有project->target->task,property,filter。
(右击前面查看图片)
task有内置的和可选的之分,可选task需要下载额外的包来支持,此外可以编写自己的task。
1、Echo
Parameters
Attribute | Description | Required |
message | the message to echo. | Yes, unless data is included in a character section within this element. |
file | the file to write the message to. | No |
append | Append to an existing file? | No - default is false. |
level | Control the level at which this message is reported. One of "error", "warning", "info", "verbose", "debug" | No - default is "warning". |
Examples
<echo message="Hello, world"/>
<echo message="Embed a line break:${line.separator}"/>
<echo>Embed another:${line.separator}</echo>
<echo>This is a longer message stretching over
two lines.
</echo>
2、Javac
Parameters(not all)
Attribute | Description | Required |
srcdir | Location of the java files. (See the note below.) | Yes, unless nested <src> elements
are present. |
destdir | Location to store the class files. | No |
includes | Comma- or space-separated list of files (may be specified using
wildcard patterns) that must be included; all .java files are
included when omitted. |
No |
includesfile | The name of a file that contains a list of files to include (may be specified using wildcard patterns). | No |
excludes | Comma- or space-separated list of files (may be specified using wildcard patterns) that must be excluded; no files (except default excludes) are excluded when omitted. | No |
excludesfile | The name of a file that contains a list of files to exclude (may be specified using wildcard patterns). | No |
classpath | The classpath to use. | No |
sourcepath | The sourcepath to use; defaults to the value of the srcdir
attribute (or nested <src> elements). To suppress the
sourcepath switch, use sourcepath="" . |
No |
Examples
<javac srcdir="${src}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
3、jar
Parameters(not all)
Attribute | Description | Required |
destfile | the JAR file to create. | Yes |
basedir | the directory from which to jar the files. | No |
compress | Not only store data but also compress them, defaults to true. Unless you set the keepcompression attribute to false, this will apply to the entire archive, not only the files you've added while updating. | No |
keepcompression | For entries coming from existing archives (like nested zipfilesets or while updating the archive), keep the compression as it has been originally instead of using the compress attribute. Defaults false. Since Ant 1.6 | No |
encoding | The character encoding to use for filenames inside the archive. Defaults to UTF8. It is not recommended to change this value as the created archive will most likely be unreadable for Java otherwise. | No |
filesonly | Store only file entries, defaults to false | No |
includes | comma- or space-separated list of patterns of files that must be included. All files are included when omitted. | No |
includesfile | the name of a file. Each line of this file is taken to be an include pattern | No |
excludes | comma- or space-separated list of patterns of files that must be excluded. No files (except default excludes) are excluded when omitted. | No |
excludesfile | the name of a file. Each line of this file is taken to be an exclude pattern | No |
defaultexcludes | indicates whether default excludes should be used or not ("yes"/"no"). Default excludes are used when omitted. | No |
manifest | the manifest file to use. This can be either the location of a manifest, or the name of a jar added through a fileset. If its the name of an added jar, the task expects the manifest to be in the jar at META-INF/MANIFEST.MF | No |
Examples
<jar destfile="${dist}/lib/app.jar"basedir="${build}/classes"
includes="mypackage/test/**"
excludes="**/Test.class" />
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"
excludes="**/Test.class" />
<fileset dir="${src}/resources"/>
</jar>
4、javadoc
Parameters(not all)
Attribute | Description | Availability | Required |
sourcepath | Specify where to find source files | all | At least one of the three or nested
<sourcepath> , <fileset> or
<packageset> |
sourcepathref | Specify where to find source files by reference to a PATH defined elsewhere. | all | |
sourcefiles | Comma separated list of source files -- see also the nested
source element. |
all | |
destdir | Destination directory for output files | all | Yes, unless a doclet has been specified. |
maxmemory | Max amount of memory to allocate to the javadoc VM | all | No |
packagenames | Comma separated list of package files (with terminating wildcard)
-- see also the nested package element. |
all | No |
packageList | The name of a file containing the packages to process | 1.2+ | No |
classpath | Specify where to find user class files | all | No |
Example
<javadoc packagenames="com.dummy.test.*"
sourcepath="src"
excludepackagenames="com.dummy.test.doc-files.*"
defaultexcludes="yes"
destdir="docs/api"
author="true"
version="true"
use="true"
windowtitle="Test API">
<doctitle><![CDATA[<h1>Test</h1>]]></doctitle>
<bottom><![CDATA[<i>Copyright © 2000 Dummy Corp. All Rights Reserved.</i>]]></bottom>
<tag name="todo" scope="all" description="To do:" />
<group title="Group 1 Packages" packages="com.dummy.test.a*"/>
<group title="Group 2 Packages" packages="com.dummy.test.b*:com.dummy.test.c*"/>
<link offline="true" href="http://java.sun.com/products/jdk/1.2/docs/api/" packagelistLoc="C:\tmp"/>
<link href="http://developer.java.sun.com/developer/products/xml/docs/api/"/>
</javadoc>