??xml version="1.0" encoding="utf-8" standalone="yes"?> 目的准备: 我们希望从不同的来源所产生的文Ӟ因此我们的Java源文件将?/tt>文g夹中?所有生成的文g应根?tt> Qƈ分裂成数个子目录个h步骤Q??/tt>我们汇编文g?/tt> JAR为我们自q白佳文g?br />
在完成仅用JAVA完成后我们必考虑我们的构E。我们必ȝ译我们的代码Q否则我们不能启动这个项目。啊-开?是的Q我们能提供一个目标。我们应该封装我们的应用。现在仅仅只有一个class文gQ不是如果你惌提供一个下载,没有人想下蝲几百文gQ想象一下一个复杂的Swing GUI --所以我们必dZ个JAR文gQ类gEXEq种直接点击可启动E序Q,一个启动JAR文g会很不错。。。而这是一个很好的实践为有一个干净的目标,q会删除所有生成的东西。许多失败将可以解决只因Z?“clean build”Q?br />
默认的情况下Ant使用 build.xml 作ؓ构徏文g的名Uͼ所以我们的.\build.xml 会是:
教程: Hello World with Ant
本文提供了一步一步用ANT开始JAVA目构徏的教E?不包括更深入了解JAVA与ANTQ本教程的目的是Z让你看,使用ANT最单的步骤?/h1>
我们必须创徏一个唯一的src目录Q?br />
md src
接着是创Z个打印固定信息的标准输出的JAVA代码Q所以仅需要写下这些代码到 src\oata\HelloWorld.java. 目录Q?br />
package oata;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
现在我们开始编译和q行Q?br />
md build\classes
javac -sourcepath src -d build\classes src\oata\HelloWorld.java
java -cp build\classes oata.HelloWorld
(q里我想说明下,可能有些新手不知道怎么L译和q行上面的程序,我在q里单演CZQ?br />
首先q行--cmd--q入你项目所在的路径Qcd +目录路径卛_Q:
然后在上面代码拷贝运?br />
)
q行?在控制台打?Hello WorldQƈ且你现在在的工E目录下多了一个build文g?build 下有一个classes文g夹,classes文g夹下正是~译后的HELLOWORLD的class文g,在通过观察Q?br />
md build\classes --在当前项目\径下创徏了build\classes子\?br />
javac -sourcepath src -d build\classes src\oata\HelloWorld.java --~译JAVA文gq将其放到classes目录?br />
java -cp build\classes oata.HelloWorld --q行classes目录下的HelloWorld.java字节码文件?br />
创徏一?jar-file 不是很困难,但是创徏一个启动的 jar-file 需要更多的步骤Q创Z个包含启动的类文gQ创Z个目标目录和归档文g:
echo Main-Class: oata.HelloWorld>myManifest
md build\jar
jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
java -jar build\jar\HelloWorld.jar
注意Q不能有MI格q行?br />
q是成功q行后的目录?br />
四步执行应用Q?/strong>
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="oata.HelloWorld"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
</project>
现在你可以编译打包运行应用了
ant compile
ant jar
ant run
或者更短的命o
ant compile jar run
While having a look at the buildfile, we will see some similar steps between Ant and the java-only commands:
同时我们可以看一下这个构建文Ӟ我们可以看见许多cM的步骤Ant和java命o之间
java-only
Ant
md build\classes
javac
-sourcepath src
-d build\classes
src\oata\HelloWorld.java
echo Main-Class: oata.HelloWorld>mf
md build\jar
jar cfm
build\jar\HelloWorld.jar
mf
-C build\classes
.
java -jar build\jar\HelloWorld.jar
<mkdir dir="build/classes"/>
<javac
srcdir="src"
destdir="build/classes"/>
<!-- automatically detected -->
<!-- obsolete; done via manifest tag -->
<mkdir dir="build/jar"/>
<jar
destfile="build/jar/HelloWorld.jar"
basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="oata.HelloWorld"/>
</manifest>
</jar>
<java jar="build/jar/HelloWorld.jar" fork="true"/>
增强构徏文g
...(待箋。。今天就暂时译也学习到q里Q对ANT也有了一个初步的认识Q也知道了我们现在项目中的build.xml是用来作甚么的了Q说白了ANT是一个基于XML文g的构建工P可以类gclass?jar啊,通过~译目Q设定相应目标\径,q行一个文件的整理~译发布。。呵?q当然是很肤的Q但是对于项目的应用_了。。不会就对着实例Q在看API了解。。?
英文部䆾没有译的部?/span>
文原始地址Q?/span>http://ant.apache.org/manual/index.html
参考资料:孙鑫老师的JAVA WEB开发详?/span>
ant的构建文件是xml格式的,每一个构建文件包含一个project和至一?~省)targetQ目标包含Q?task)元素Q构建文仉的每一个Q务元素(taskQ可以指定一个id元素Q可以用它的值来引用q个taskQ这个值是唯一的,
Q更多说明,L下面?TasksQ?/p>
Project 有三个属?/p>
属?/strong> | 描述 | 必须 |
name | 工程?/td> | No |
default | 没有指定target时缺省用的target名字 | No; 然而在Ant 1.6.0后,每个工程包含一个即使?-projecthelp 选项也要执行的Q? |
basedir |
要\径,q个属性可以被先前讄?#8220;basedir"q个属性覆盖,计算其它路径的其路径。如果没有设|这个属性或Ҏ(PropertyQ,用构建文?build.xml)的父目录作ؓ基目录?/p> |
No |
对一个工E的描述QdescriptionQ可以用的元?lt;description>Q请?a >description 元素Q?/p>
每个工程都定义了一个或多个目标(target)Q目标就是一pd你要执行的Q务,当执?span class="hilite1">ant的时候,你可以选择执行你想要执行的目标Q当没有指定目标Ӟ使用<project>元素中default指定的目标?/p>
一个目标可以依赖其它的目标Q例如,你可以有一个编译的目标Q一个发布的目标Q只有编译之后才可以发布Q所以以布目标要依赖于编译,Ant解决q种依赖关系?/p>
应该说明的是Q?span class="hilite1">Ant的这U依赖只是指定的目标的执行顺序,qƈ不媄响执行那些没有必要执行的依赖目标?/p>
Ant执行目标的顺序是按照它们从左到右的出现顺序,一个目标比它依赖的目标更早执行是有可能的?/p>
<target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/>
我们要执行目标D.从依赖属?depends)看,你可能认为应该先执行目标C,然后是BQ最后是AQ错?C依赖BQB依赖AQ因此,先执行AQ再是BQ接着是CQ最后是D
上面从给定目标D延C赖的A目标的这个依赖链中,每一个目标只执行一ơ,即多个目标依赖一个目标,因此Q执行目标D首先会调用C执行QC又会调用B执行QB会DAW一ơ被执行Q然后执行BQ接着是CQ一直执行到依赖链中的DQ这个过E中Q不会再执行B和AQ因Z们已l执行过了,如果B和C没有依赖关系Q那么,B和A在C处理D的依赖时p执行?/p>
可以讄某个Ҏ?Property)执行目标Q例如,q可以依赖系l环?java 版本,操作pȝQ命令行Ҏ定义,{?更好的控制构造过E?q性来d目标Q你应该d属性if(或?unless属?Q属性gؓ你要作用于此目标上的Ҏ。注意:Ant仅检查这个特性是否被讄Q这个特性的值ƈ不重要,一个空字符串的Ҏ仍然是一个存在的Ҏ。比如:
<target name="build-module-A" if="module-A-present"/><target name="build-own-fake-module-A" unless="module-A-present"/>
在第一个例子中Q如果设|了module-A-presentҎ(可以是Q何|比如QfalseQ?目标都会执行?/p>
在第二个例子中,如果讄了module-A-presentQ也是Q意|Q目标都不会被运行?/p>
在if/unless属性中只能指定一个特性,如果x定多U情况,可以通过依赖目标来计这U检查结果?/p>
<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>
如果不存在if和unless属性,目标L会被执行?/p>
Important: if和unless属性只是让使用了这二个属性的目标执行或不执行Q而不能控制目标依赖的目标的执行?/p>
可选的description属性能对目标进行描qͼ可以?projecthelp选项查看Q没有Description的目标不会被选项令列出来Q除非?verbose?debug选项?/p>
在其它目标依赖的初始化目?initialization target)中放|?tstamp 是一个好的实践,保q个目标LW一个被执行的,在这本手册中Q大多数的初始化目标都命名叫“init”?/p>
如果讄了依赖目标和if/unless属性,依赖的属性会先执行?/p>
一个目标(targetQ有以下属性?/p>
属?/strong> | 描述 | 必须 |
name | target 的名?/td> | Yes |
depends |
一pd依赖的目标名字,用逗号分隔 |
No |
if |
执行此目标一定要讄的特性(propertyQ名 |
No |
unless |
执行些目标一定不要设|的Ҏ(propertyQ名?/p> |
No |
description | 对目标的一个简短描q?/td> | No |
A target name can be any alphanumeric string valid in the encoding of the XML file. The empty string "" is in this set, as is comma "," and space " ". Please avoid using these, as they will not be supported in future Ant versions because of all the confusion they cause. IDE support of unusual target names, or any target name containing spaces, varies with the IDE.
Targets beginning with a hyphen such as "-restart"
are valid, and can be used to name targets that should not be called directly from the command line.
A task is a piece of code that can be executed.
A task can have multiple attributes (or arguments, if you prefer). The value of an attribute might contain references to a property. These references will be resolved before the task is executed.
Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />
where name is the name of the task, attributeN is the attribute name, and valueN is the value for this attribute.
There is a set of built-in tasks, along with a number of optional tasks, but it is also very easy to write your own.
All tasks share a task name attribute. The value of this attribute will be used in the logging messages generated by Ant.
Tasks can be assigned an id
attribute:
<taskname id="taskID" ... />
where taskname is the name of the task, and taskID is a unique identifier for this task. You can refer to the corresponding task object in scripts or other tasks via this name. For example, in scripts you could do:
<script ... > task1.setFoo("bar"); </script>
to set the foo
attribute of this particular task instance. In another task (written in Java), you can access the instance via project.getReference("task1")
.
Note1: If "task1" has not been run yet, then it has not been configured (ie., no attributes have been set), and if it is going to be configured later, anything you've done to the instance may be overwritten.
Note2: Future versions of Ant will most likely not be backward-compatible with this behaviour, since there will likely be no task instances at all, only proxies.
A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant. A property has a name and a value; the name is case-sensitive. 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
In order to maintain backward compatibility with older Ant releases, a single '$' character encountered apart from a property-like construct (including a matched pair of french braces) will be interpreted literally; that is, as '$'. The "correct" way to specify this literal character, however, is by using the escaping mechanism unconditionally, so that "$$" is obtained by specifying "$$$$". Mixing the two approaches yields unpredictable results, as "$$$" results in "$$".
Built-in Properties
Ant provides access to all system properties as if they had been defined using a
<property>
task. For example,${os.name}
expands to the name of the operating system.For a list of system properties see the Javadoc of System.getProperties.
In addition, Ant has some built-in properties:
basedir the absolute path of the project's basedir (as set with the basedir attribute of <project>). ant.file the absolute path of the buildfile. ant.version the version of Ant ant.project.name the name of the project that is currently executing; it is set in the name attribute of <project>. ant.java.version the JVM version Ant detected; currently it can hold the values "1.2", "1.3", "1.4" and "1.5".
There is also another property, but this is set by the launcher script and therefore maybe not set inside IDEs:
ant.home home directory of Ant
<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>
Notice that we are declaring properties outside any target. As of Ant 1.6 all tasks can be declared outside targets (earlier version only allowed <property>,<typedef> and <taskdef>). When you do this they are evaluated before any targets are executed. Some tasks will generate build failures if they are used outside of targets as they may cause infinite loops otherwise (
<antcall>
for example).We have given some targets descriptions; this causes the projecthelp invocation option to list them as public targets with the descriptions; the other target is internal and not listed.
Finally, for this target to work the source in the src subdirectory should be stored in a directory tree which matches the package names. Check the <javac> task for details.
A project can have a set of tokens that might be automatically expanded if found when a file is copied, when the filtering-copy behavior is selected in the tasks that support this. These might be set in the buildfile by the filter task.
Since this can potentially be a very harmful behavior, the tokens in the files must be of the form
@
token@
, where token is the token name that is set in the<filter>
task. This token syntax matches the syntax of other build systems that perform such filtering and remains sufficiently orthogonal to most programming and scripting languages, as well as with documentation systems.Note: If a token with the format
@
token@
is found in a file, but no filter is associated with that token, no changes take place; therefore, no escaping method is available - but as long as you choose appropriate names for your tokens, this should not cause problems.Warning: If you copy binary files with filtering turned on, you can corrupt the files. This feature should be used with text files only.
Path-like Structures
You can specify
PATH
- andCLASSPATH
-type references using both ":
" and ";
" as separator characters. Ant will convert the separator to the correct character of the current operating system.Wherever path-like values need to be specified, a nested element can be used. This takes the general form of:
<classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath>
The
location
attribute specifies a single file or directory relative to the project's base directory (or an absolute filename), while thepath
attribute accepts colon- or semicolon-separated lists of locations. Thepath
attribute is intended to be used with predefined paths - in any other case, multiple elements withlocation
attributes should be preferred.As a shortcut, the
<classpath>
tag supportspath
andlocation
attributes of its own, so:<classpath> <pathelement path="${classpath}"/> </classpath>
can be abbreviated to:
<classpath path="${classpath}"/>
In addition, 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>
This builds a path that holds the value of
${classpath}
, followed by all jar files in thelib
directory, theclasses
directory, all directories namedclasses
under theapps
subdirectory of${build.dir}
, except those that have the textTest
in their name, and the files specified in the referenced FileList.If you want to use the same path-like structure for several tasks, you can define them with a
<path>
element at the same level as targets, and reference them via their id attribute--see References for an example.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>
The shortcuts previously mentioned for
<classpath>
are also valid for<path>
.For example:<path id="base.path"> <pathelement path="${classpath}"/> </path>
can be written as:
<path id="base.path" path="${classpath}"/>
Command-line Arguments
Several tasks take arguments that will be passed to another process on the command line. To make it easier to specify arguments that contain space characters, nested
arg
elements can be used.
Attribute | Description | Required |
value | a single command-line argument; can contain space characters. | Exactly one of these. |
file | The name of a file as a single command-line argument; will be replaced with the absolute filename of the file. | |
path | A string that will be treated as a path-like string as a single command-line argument; you can use ; or : as path separators and Ant will convert it to the platform's local conventions. |
|
pathref | Reference to a path defined elsewhere. Ant will convert it to the platform's local conventions. | |
line | a space-delimited list of command-line arguments. |
It is highly recommended to avoid the line
version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances.
<arg value="-l -a"/>
is a single command-line argument containing a space character, not separate commands "-l" and "-a".
<arg line="-l -a"/>
This is a command line with two separate arguments, "-l" and "-a".
<arg path="/dir;/dir2:\dir3"/>
is a single command-line argument with the value \dir;\dir2;\dir3
on DOS-based systems and /dir:/dir2:/dir3
on Unix-like systems.
Any project element can be assigned an identifier using its id
attribute. In most cases the element can subsequently be referenced by specifying the refid
attribute on an element of the same type. This can be useful if you are going to replicate the same snippet of XML over and over again--using a <classpath>
structure more than once, for example.
The following example:
<project ... > <target ... > <rmic ...> <classpath> <pathelement location="lib/"/> <pathelement path="${java.class.path}/"/> <pathelement path="${additional.path}"/> </classpath> </rmic> </target> <target ... > <javac ...> <classpath> <pathelement location="lib/"/> <pathelement path="${java.class.path}/"/> <pathelement path="${additional.path}"/> </classpath> </javac> </target> </project>
could be rewritten as:
<project ... > <path id="project.class.path"> <pathelement location="lib/"/> <pathelement path="${java.class.path}/"/> <pathelement path="${additional.path}"/> </path> <target ... > <rmic ...> <classpath refid="project.class.path"/> </rmic> </target> <target ... > <javac ...> <classpath refid="project.class.path"/> </javac> </target> </project>
All tasks that use nested elements for PatternSets, FileSets, ZipFileSets or path-like structures accept references to these structures as shown in the examples. Using refid
on a task will ordinarily have the same effect (referencing a task already declared), but the user should be aware that the interpretation of this attribute is dependent on the implementation of the element upon which it is specified. Some tasks (the property task is a handy example) deliberately assign a different meaning to refid
.
Ant supports a plugin mechanism for using third party tasks. For using them you have to do two steps:
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:
-lib
${user.home}/.ant/lib
${ant.home}/lib
For the declaration there are several ways:
<taskdef name="taskname" classname="ImplementationClass"/>
<taskdef name="for" classname="net.sf.antcontrib.logic.For" /> <for ... />
<taskdef>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" /> <for ... />
<taskdef>
<taskdef resource="net/sf/antcontrib/antlib.xml" /> <for ... />
antlib:
protocoll handler <project xmlns:ac="antlib:net.sf.antconrib"/> <ac:for ... />
If you need a special function, you should
</project>