﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-小菜毛毛技术分享-文章分类-ANT</title><link>http://www.blogjava.net/caizh2009/category/39893.html</link><description>与大家共同成长</description><language>zh-cn</language><lastBuildDate>Wed, 03 Jun 2009 07:38:23 GMT</lastBuildDate><pubDate>Wed, 03 Jun 2009 07:38:23 GMT</pubDate><ttl>60</ttl><item><title>ant入门教程（一）Hello World with Ant（英文教程文档翻译）（连载）</title><link>http://www.blogjava.net/caizh2009/articles/279775.html</link><dc:creator>小菜毛毛</dc:creator><author>小菜毛毛</author><pubDate>Wed, 03 Jun 2009 04:01:00 GMT</pubDate><guid>http://www.blogjava.net/caizh2009/articles/279775.html</guid><wfw:comment>http://www.blogjava.net/caizh2009/comments/279775.html</wfw:comment><comments>http://www.blogjava.net/caizh2009/articles/279775.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/caizh2009/comments/commentRss/279775.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/caizh2009/services/trackbacks/279775.html</trackback:ping><description><![CDATA[ant入门教程（一）Hello World with Ant（英文教程文档翻译）（连载）<br />
只能够翻译个大概意思，希望朋友们别见怪...<br />
<h1>教程: Hello World with Ant<br />
<br />
本文档提供了一步一步用ANT开始JAVA项目构建的教程,不包括更深入了解JAVA与ANT，本教程的目的是为了让你看，使用ANT最简单的步骤。</h1>
<p><strong style="font-size: 14pt">项目的准备：</strong></p>
<p>&nbsp;我们希望从不同的来源所产生的文件，因此我们的Java源文件将<tt>源</tt>文件夹中。 <span onmouseover="_tipon(this)" onmouseout="_tipoff()">所有生成的文件应根据<tt>建设</tt> ，并分裂成数个子目录个人步骤： <tt>班</tt>我们汇编文件<tt>和</tt> JAR为我们自己的白佳文件。<br />
</span>&nbsp;</p>
我们希望从所产生的文件来分离源文件，因此我们的JAVA源文件将在<tt>src</tt> folder。所有生成的文件应根据构建方式，并为单独的步骤分成数个子目录：classes是我们编译的文件，jar是我们JAR包文件。<br />
<br />
我们必须创建一个唯一的src目录：<br />
<pre class="code">md src
</pre>
<p><br />
接着是创建一个打印固定信息的标准输出的JAVA代码，所以仅需要写下这些代码到 <tt>src\oata\HelloWorld.java</tt>. 目录：<br />
<br />
package oata;<br />
<br />
public class HelloWorld {<br />
&nbsp;&nbsp;&nbsp; public static void main(String[] args) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Hello World");<br />
&nbsp;&nbsp;&nbsp; }<br />
}<br />
<br />
现在我们开始编译和运行：<br />
md build\classes<br />
javac -sourcepath src -d build\classes src\oata\HelloWorld.java<br />
java -cp build\classes oata.HelloWorld<br />
(这里我想说明下，可能有些新手不知道怎么样编译和运行上面的程序，我在这里简单演示下：<br />
首先运行--cmd--进入你项目所在的路径（cd +目录路径即可）：<br />
然后在上面代码拷贝运行<br />
)<br />
运行后 将在控制台打印 Hello World，并且你将现在在的工程目录下多了一个build文件夹,build 下有一个classes文件夹，classes文件夹下正是编译后的HELLOWORLD的class文件,在通过观察：<br />
md build\classes&nbsp; --在当前项目路径下创建了build\classes子路径<br />
javac -sourcepath src -d build\classes src\oata\HelloWorld.java --编译JAVA文件并将其放到classes目录下<br />
java -cp build\classes oata.HelloWorld&nbsp; --运行classes目录下的HelloWorld.java字节码文件。<br />
<br />
创建一个 jar-file 不是很困难，但是创建一个启动的&nbsp;jar-file&nbsp; 需要更多的步骤：创建一个包含启动级的类文件，创建一个目标目录和归档文件:<br />
echo Main-Class: oata.HelloWorld&gt;myManifest<br />
md build\jar<br />
jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .<br />
java -jar build\jar\HelloWorld.jar<br />
注意：不能有任何空格运行时<br />
<br />
<img height="208" alt="" src="http://www.blogjava.net/images/blogjava_net/caizh2009/2.jpg" width="239" border="0" /><br />
这是成功运行后的目录。<br />
<br />
<strong style="font-size: 18pt">四步执行应用：</strong><br />
<br />
</p>
<p>在完成仅用JAVA完成后我们必须考虑我们的构建过程。我们必须编译我们的代码，否则我们不能启动这个项目。啊-开始-是的，我们能提供一个目标。我们应该封装我们的应用。现在仅仅只有一个class文件，不是如果你想要提供一个下载，没有人想下载几百文件（想象一下一个复杂的Swing GUI --所以我们必须创建一个JAR文件（类似于EXE这种直接点击就可启动程序），一个启动JAR文件将会很不错。。。而这是一个很好的实践为有一个干净的目标，这会删除所有生成的东西。许多失败将可以解决只因为一个 &#8220;clean build&#8221;）<br />
</p>
<p>默认的情况下Ant使用 build.xml 作为构建文件的名称，所以我们的<tt>.\build.xml</tt> 将会是：<br />
<br />
&lt;project&gt;<br />
<br />
&nbsp;&nbsp;&nbsp; &lt;target name="clean"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;delete dir="build"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/target&gt;<br />
<br />
&nbsp;&nbsp;&nbsp; &lt;target name="compile"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;mkdir dir="build/classes"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;javac srcdir="src" destdir="build/classes"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/target&gt;<br />
<br />
&nbsp;&nbsp;&nbsp; &lt;target name="jar"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;mkdir dir="build/jar"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;jar destfile="build/jar/HelloWorld.jar" basedir="build/classes"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;manifest&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;attribute name="Main-Class" value="oata.HelloWorld"/&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/manifest&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/jar&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/target&gt;<br />
<br />
&nbsp;&nbsp;&nbsp; &lt;target name="run"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;java jar="build/jar/HelloWorld.jar" fork="true"/&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;/target&gt;<br />
<br />
&lt;/project&gt;<br />
<br />
现在你可以编译打包运行应用了<br />
ant compile<br />
ant jar<br />
ant run<br />
或者更简短的命令<br />
ant compile jar run<br />
<br />
While having a look at the buildfile, we will see some similar steps between Ant and the java-only commands: <br />
同时我们可以看一下这个构建文件，我们可以看见许多类似的步骤Ant和java命令之间<br />
<table>
    <tbody>
        <tr>
            <th>java-only</th>
            <th>Ant</th>
        </tr>
        <tr>
            <td valign="top">
            <pre class="code">md build\classes
            javac
            -sourcepath src
            -d build\classes
            src\oata\HelloWorld.java
            echo Main-Class: oata.HelloWorld&gt;mf
            md build\jar
            jar cfm
            build\jar\HelloWorld.jar
            mf
            -C build\classes
            .
            java -jar build\jar\HelloWorld.jar
            </pre>
            </td>
            <td valign="top">
            <pre class="code">&lt;mkdir dir="build/classes"/&gt;
            &lt;javac
            srcdir="src"
            destdir="build/classes"/&gt;
            <em>&lt;!-- automatically detected --&gt;</em>
            <em>&lt;!-- obsolete; done via manifest tag --&gt;</em>
            &lt;mkdir dir="build/jar"/&gt;
            &lt;jar
            destfile="build/jar/HelloWorld.jar"
            basedir="build/classes"&gt;
            &lt;manifest&gt;
            &lt;attribute name="Main-Class" value="oata.HelloWorld"/&gt;
            &lt;/manifest&gt;
            &lt;/jar&gt;
            &lt;java jar="build/jar/HelloWorld.jar" fork="true"/&gt;</pre>
            </td>
        </tr>
    </tbody>
</table>
<br />
<span style="font-size: 18pt"><br />
<strong>增强构建文件</strong></span></p>
<p>...(待续。。今天就暂时翻译也学习到这里，对ANT也有了一个初步的认识，也知道了我们现在项目中的build.xml是用来作甚么的了，说白了ANT就是一个基于XML文件的构建工具，可以将类似于class啊 jar啊，通过编译项目，设定相应目标路径，进行一个文件的整理编译发布。。呵呵 这当然是很肤浅的，但是对于项目的应用足够了。。不会就对着实例，在看API了解。。。)</p>
<img src ="http://www.blogjava.net/caizh2009/aggbug/279775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/caizh2009/" target="_blank">小菜毛毛</a> 2009-06-03 12:01 <a href="http://www.blogjava.net/caizh2009/articles/279775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ant文档翻译(连载)</title><link>http://www.blogjava.net/caizh2009/articles/279701.html</link><dc:creator>小菜毛毛</dc:creator><author>小菜毛毛</author><pubDate>Tue, 02 Jun 2009 13:41:00 GMT</pubDate><guid>http://www.blogjava.net/caizh2009/articles/279701.html</guid><wfw:comment>http://www.blogjava.net/caizh2009/comments/279701.html</wfw:comment><comments>http://www.blogjava.net/caizh2009/articles/279701.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/caizh2009/comments/commentRss/279701.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/caizh2009/services/trackbacks/279701.html</trackback:ping><description><![CDATA[<p><span style="font-size: small; color: #ff0000"><strong>很想把<span class="hilite1">Ant</span>好好的学习一下，最好的学习方法就是看官方文档，所以决定把原版英文文档翻译一下，如果有翻译不对的地方，请大家指出来，谢谢</strong></span></p>
<p><strong><span style="font-size: small; color: #ff0000">英文部份为还没有翻译的部份</span></strong></p>
<p><strong><span style="color: #ff0000"><span style="font-size: small">文档原始地址：</span><a href="http://ant.apache.org/manual/index.html"><span style="font-size: small">http://<span class="hilite1">ant</span>.apache.org/manual/index.html</span></a></span></strong></p>
<p><strong><span style="color: #ff0000">参考资料：孙鑫老师的JAVA WEB开发详解</span></strong>&nbsp;</p>
<h1>Using <span class="hilite1">Ant</span></h1>
<h1><a name="buildfile">简单的构建文件：</a></h1>
<p><strong><span style="font-size: medium; color: #ff0000"><span class="hilite1">ant</span>的构建文件是xml格式的，每一个构建文件包含一个project和至少一个(缺省)target，目标包含任务(task)元素，构建文件里的每一个任务元素（task）可以指定一个id元素，可以用它的值来引用这个task，这个值是唯一的，</span></strong></p>
<p>（更多说明，请看下面的 <a href="http://coolbi.javaeye.com/blog/192600#tasks">Tasks</a>）</p>
<h3><a name="projects">Projects</a>（工程）</h3>
<p>Project　有三个属性</p>
<p>&nbsp;</p>
<table cellspacing="0" cellpadding="2" border="1">
    <tbody>
        <tr>
            <td valign="top"><strong>属性</strong></td>
            <td valign="top"><strong>描述</strong></td>
            <td valign="top" align="center"><strong>必须</strong></td>
        </tr>
        <tr>
            <td valign="top">name</td>
            <td valign="top">工程名</td>
            <td valign="top" align="center">No</td>
        </tr>
        <tr>
            <td valign="top">default</td>
            <td valign="top">没有指定target时缺省使用的target名字</td>
            <td valign="top" align="center">No;&nbsp;然而在<strong><span class="hilite1">Ant</span> 1.6.0后，</strong>每个工程包含一个即使使用 <a href="http://coolbi.javaeye.com/blog/running.html#options">-projecthelp</a> 选项也要执行的任务. </td>
        </tr>
        <tr>
            <td valign="top">basedir</td>
            <td valign="top">
            <p>要路径，这个属性可以被先前设置的&#8220;basedir"这个属性覆盖，计算其它路径的其路径。如果没有设置这个属性或特性（Property），将使用构建文件(build.xml)的父目录作为基目录。</p>
            </td>
            <td valign="top" align="center">No</td>
        </tr>
    </tbody>
</table>
<p>
<p>对一个工程的描述（description）可以用顶级的元素&lt;description&gt;（请看<a href="http://coolbi.javaeye.com/blog/CoreTypes/description.html">description</a> 元素）</p>
<p>每个工程都定义了一个或多个目标(target)，目标就是一系列你要执行的任务，当执行<span class="hilite1">ant</span>的时候，你可以选择执行你想要执行的目标，当没有指定目标时，使用&lt;project&gt;元素中default指定的目标。</p>
<h3><a name="targets">Targets</a></h3>
<p>一个目标可以依赖其它的目标，例如，你可以有一个编译的目标，一个发布的目标，只有编译之后才可以发布，所以以布目标要依赖于编译，<span class="hilite1">Ant</span>解决这种依赖关系。</p>
<p>应该说明的是，<span class="hilite1">Ant</span>的这种依赖只是指定的目标的执行顺序，这并不影响执行那些没有必要执行的依赖目标。</p>
<p><span class="hilite1">Ant</span>执行目标的顺序是按照它们从左到右的出现顺序，一个目标比它依赖的目标更早执行是有可能的。</p>
<pre>&lt;target name="A"/&gt;
&lt;target name="B" depends="A"/&gt;
&lt;target name="C" depends="B"/&gt;
&lt;target name="D" depends="C,B,A"/&gt;</pre>
<p>我们要执行目标D.从依赖属性(depends)看，你可能认为应该先执行目标C,然后是B，最后是A，错了!C依赖B，B依赖A，因此，先执行A，再是B，接着是C，最后是D</p>
<p>上面从给定目标D延伸到依赖的A目标的这个依赖链中，每一个目标只执行一次，即使多个目标依赖一个目标，因此，执行目标D首先会调用C执行，C又会调用B执行，B将会导致A第一次被执行，然后执行B，接着是C，一直执行到依赖链中的D，这个过程中，不会再执行B和A，因为他们已经执行过了，如果B和C没有依赖关系，那么，B和A将在C处理D的依赖时就被执行。</p>
<p>可以设置某个特性(Property)执行目标，例如，这可以依赖系统环境(java 版本,操作系统，命令行特性定义，等)更好的控制构造过程,由特性来添加目标，你应该添加属性if(或者 unless属性)，属性值为你要作用于此目标上的特性。注意：<span class="hilite1">Ant</span>仅检查这个特性是否被设置，这个特性的值并不重要，一个空字符串的特性仍然是一个存在的特性。比如：</p>
<p>&nbsp;</p>
<blockquote>
<blockquote>
<pre>&lt;target name="build-module-A" if="module-A-present"/&gt;</pre>
<pre>&lt;target name="build-own-fake-module-A" unless="module-A-present"/&gt;</pre>
</blockquote></blockquote>
<p>在第一个例子中，如果设置了module-A-present特性（可以是任何值，比如：false）,目标都会执行。</p>
<p>在第二个例子中，如果设置了module-A-present（也是任意值），目标都不会被运行。</p>
<p>在if/unless属性中只能指定一个特性，如果想指定多种情况，可以通过依赖目标来计算这种检查结果。</p>
<pre>&lt;target name="myTarget" depends="myTarget.check" if="myTarget.run"&gt;
&lt;echo&gt;Files foo.txt and bar.txt are present.&lt;/echo&gt;
&lt;/target&gt;
&lt;target name="myTarget.check"&gt;
&lt;condition property="myTarget.run"&gt;
&lt;and&gt;
&lt;available file="foo.txt"/&gt;
&lt;available file="bar.txt"/&gt;
&lt;/and&gt;
&lt;/condition&gt;
&lt;/target&gt;
</pre>
<p>如果不存在if和unless属性，目标总是会被执行。</p>
<p><strong>Important:</strong> if和unless属性只是让使用了这二个属性的目标执行或不执行，而不能控制目标依赖的目标的执行。</p>
<p>可选的description属性能对目标进行描述，可以用-projecthelp选项查看，没有Description的目标不会被选项令列出来，除非使用-verbose或-debug选项。</p>
<p>在其它目标依赖的初始化目标(initialization target)中放置 tstamp 是一个好的实践，确保这个目标总是第一个被执行的，在这本手册中，大多数的初始化目标都命名叫&#8220;init&#8221;。</p>
<p>如果设置了依赖目标和if/unless属性，依赖的属性会先执行。</p>
<p>一个目标（target）有以下属性。</p>
<p>&nbsp;</p>
<table cellspacing="0" cellpadding="2" border="1">
    <tbody>
        <tr>
            <td valign="top"><strong>属性</strong></td>
            <td valign="top"><strong>描述</strong></td>
            <td valign="top" align="center"><strong>必须</strong></td>
        </tr>
        <tr>
            <td valign="top">name</td>
            <td valign="top">target　的名字</td>
            <td valign="top" align="center">Yes</td>
        </tr>
        <tr>
            <td valign="top">depends</td>
            <td valign="top">
            <p>一系列依赖的目标名字，用逗号分隔</p>
            </td>
            <td valign="top" align="center">No</td>
        </tr>
        <tr>
            <td valign="top">if</td>
            <td valign="top">
            <p>执行此目标一定要设置的特性（property）名</p>
            </td>
            <td valign="top" align="center">No</td>
        </tr>
        <tr>
            <td valign="top">unless</td>
            <td valign="top">
            <p>执行些目标一定不要设置的特性（property）名。</p>
            </td>
            <td valign="top" align="center">No</td>
        </tr>
        <tr>
            <td valign="top">description</td>
            <td valign="top">对目标的一个简短描述</td>
            <td valign="top" align="center">No</td>
        </tr>
    </tbody>
</table>
<p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>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 <span class="hilite1">Ant</span> versions because of all the confusion they cause. IDE support of unusual target names, or any target name containing spaces, varies with the IDE.</p>
<p>Targets beginning with a hyphen such as <code>"-restart"</code> are valid, and can be used to name targets that should not be called directly from the command line.</p>
<h3><a name="tasks">Tasks</a></h3>
<p>A task is a piece of code that can be executed.</p>
<p>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.</p>
<p>Tasks have a common structure:</p>
<blockquote>&lt;<em>name</em> <em>attribute1</em>="<em>value1</em>" <em>attribute2</em>="<em>value2</em>" ... /&gt;
<pre></pre>
</blockquote>
<p>where <em>name</em> is the name of the task, <em>attributeN</em> is the attribute name, and <em>valueN</em> is the value for this attribute.</p>
<p>There is a set of <a href="http://coolbi.javaeye.com/blog/coretasklist.html" target="navFrame">built-in tasks</a>, along with a number of <a href="http://coolbi.javaeye.com/blog/optionaltasklist.html" target="navFrame">optional tasks</a>, but it is also very easy to <a href="http://coolbi.javaeye.com/blog/develop.html#writingowntask">write your own</a>.</p>
<p>All tasks share a task name attribute. The value of this attribute will be used in the logging messages generated by <span class="hilite1">Ant</span>.</p>
<p>Tasks can be assigned an <code>id</code> attribute: </p>
<blockquote>&lt;<em>taskname</em> id="<em>taskID</em>" ... /&gt;
<pre></pre>
</blockquote>
<p>where <em>taskname</em> is the name of the task, and <em>taskID</em> 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: </p>
<blockquote>&lt;script ... &gt; task1.setFoo("bar"); &lt;/script&gt;
<pre></pre>
</blockquote>
<p>to set the <code>foo</code> attribute of this particular task instance. In another task (written in Java), you can access the instance via <code>project.getReference("task1")</code>. </p>
<p>Note<sup>1</sup>: 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. </p>
<p>Note<sup>2</sup>: Future versions of <span class="hilite1">Ant</span> will most likely <em>not</em> be backward-compatible with this behaviour, since there will likely be no task instances at all, only proxies. </p>
<h3><a name="properties">Properties</a></h3>
<p>A project can have a set of properties. These might be set in the buildfile by the <a href="http://coolbi.javaeye.com/blog/CoreTasks/property.html">property</a> task, or might be set outside <span class="hilite1">Ant</span>. 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 "<code>${</code>" and "<code>}</code>" 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: <code>${builddir}/classes</code>. This is resolved at run-time as <code>build/classes</code>.</p>
<p>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: &lt;echo&gt;$${builddir}=${builddir}&lt;/echo&gt;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre>
<p>will echo this message: </p>
<p>${builddir}=build/classes</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre>
<p>&nbsp;</p>
<p>In order to maintain backward compatibility with older <span class="hilite1">Ant</span> 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 "$$".</p>
<h3><a name="built-in-props">Built-in Properties</a></h3>
<p><span class="hilite1">Ant</span> provides access to all system properties as if they had been defined using a <code>&lt;property&gt;</code> task. For example, <code>${os.name}</code> expands to the name of the operating system.</p>
<p>For a list of system properties see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#getProperties()">the Javadoc of System.getProperties</a>. </p>
<p>In addition, <span class="hilite1">Ant</span> has some built-in properties:</p>
<p>basedir the absolute path of the project's basedir (as set with the basedir attribute of <a href="http://coolbi.javaeye.com/blog/192600#projects">&lt;project&gt;)</a>. <span class="hilite1">ant</span>.file the absolute path of the buildfile. <span class="hilite1">ant</span>.version the version of <span class="hilite1">Ant</span> <span class="hilite1">ant</span>.project.name the name of the project that is currently executing; it is set in the name attribute of &lt;project&gt;. <span class="hilite1">ant</span>.java.version the JVM version <span class="hilite1">Ant</span> detected; currently it can hold the values "1.2", "1.3", "1.4" and "1.5". </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre>
<p>There is also another property, but this is set by the launcher script and therefore maybe not set inside IDEs:</p>
<p><span class="hilite1">ant</span>.home home directory of <span class="hilite1">Ant</span> </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre>
<p><a name="example"></p>
<p>&nbsp;</p>
<p>&lt;project name="MyProject" default="dist" basedir="."&gt; &lt;description&gt; simple example build file &lt;/description&gt; &lt;!-- set global properties for this build --&gt; &lt;property name="src" location="src"/&gt; &lt;property name="build" location="build"/&gt; &lt;property name="dist" location="dist"/&gt; &lt;target name="init"&gt; &lt;!-- Create the time stamp --&gt; &lt;tstamp/&gt; &lt;!-- Create the build directory structure used by compile --&gt; &lt;mkdir dir="${build}"/&gt; &lt;/target&gt; &lt;target name="compile" depends="init" description="compile the source " &gt; &lt;!-- Compile the java code from ${src} into ${build} --&gt; &lt;javac srcdir="${src}" destdir="${build}"/&gt; &lt;/target&gt; &lt;target name="dist" depends="compile" description="generate the distribution" &gt; &lt;!-- Create the distribution directory --&gt; &lt;mkdir dir="${dist}/lib"/&gt; &lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt; &lt;jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/&gt; &lt;/target&gt; &lt;target name="clean" description="clean up" &gt; &lt;!-- Delete the ${build} and ${dist} directory trees --&gt; &lt;delete dir="${build}"/&gt; &lt;delete dir="${dist}"/&gt; &lt;/target&gt; &lt;/project&gt; </p>
<p>&nbsp;</p>
<pre>
<p>Notice that we are declaring properties outside any target. As of <span class="hilite1">Ant</span> 1.6 all tasks can be declared outside targets (earlier version only allowed <tt>&lt;property&gt;</tt>,<tt>&lt;typedef&gt;</tt> and <tt>&lt;taskdef&gt;</tt>). 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 (<code>&lt;antcall&gt;</code> for example).</p>
<p>We have given some targets descriptions; this causes the <tt>projecthelp</tt> invocation option to list them as public targets with the descriptions; the other target is internal and not listed. </p>
<p>Finally, for this target to work the source in the <tt>src</tt> subdirectory should be stored in a directory tree which matches the package names. Check the <tt>&lt;javac&gt;</tt> task for details. <a name="filters"></p>
<p>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 <a href="http://coolbi.javaeye.com/blog/CoreTasks/filter.html">filter</a> task.</p>
<p>Since this can potentially be a very harmful behavior, the tokens in the files <strong>must</strong> be of the form <code>@</code><em>token</em><code>@</code>, where <em>token</em> is the token name that is set in the <code>&lt;filter&gt;</code> 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.</p>
<p>Note: If a token with the format <code>@</code><em>token</em><code>@</code> 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.</p>
<p><strong>Warning:</strong> If you copy binary files with filtering turned on, you can corrupt the files. This feature should be used with text files <em>only</em>.</p>
<h3><a name="path">Path-like Structures</a></h3>
<p>You can specify <code>PATH</code>- and <code>CLASSPATH</code>-type references using both "<code>:</code>" and "<code>;</code>" as separator characters. <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> will convert the separator to the correct character of the current operating system.</p>
<p>Wherever path-like values need to be specified, a nested element can be used. This takes the general form of:</p>
<p>&lt;classpath&gt; &lt;pathelement path="${classpath}"/&gt; &lt;pathelement location="lib/helper.jar"/&gt; &lt;/classpath&gt; </p>
<p>&nbsp;</p>
<pre>
<p>The <code>location</code> attribute specifies a single file or directory relative to the project's base directory (or an absolute filename), while the <code>path</code> attribute accepts colon- or semicolon-separated lists of locations. The <code>path</code> attribute is intended to be used with predefined paths - in any other case, multiple elements with <code>location</code> attributes should be preferred.</p>
<p>As a shortcut, the <code>&lt;classpath&gt;</code> tag supports <code>path</code> and <code>location</code> attributes of its own, so:</p>
<p>&lt;classpath&gt; &lt;pathelement path="${classpath}"/&gt; &lt;/classpath&gt; </p>
<p>&nbsp;</p>
<pre>
<p>can be abbreviated to:</p>
<p>&lt;classpath path="${classpath}"/&gt; </p>
<p>&nbsp;</p>
<pre>
<p>In addition, one or more <a href="http://coolbi.javaeye.com/blog/CoreTypes/resources.html#collection">Resource Collection</a>s can be specified as nested elements (these must consist of <a href="http://coolbi.javaeye.com/blog/CoreTypes/resources.html#file">file</a>-type resources only). Additionally, it should be noted that although resource collections are processed in the order encountered, certain resource collection types such as <a href="http://coolbi.javaeye.com/blog/CoreTypes/fileset.html">fileset</a>, <a href="http://coolbi.javaeye.com/blog/CoreTypes/dirset.html">dirset</a> and <a href="http://coolbi.javaeye.com/blog/CoreTypes/resources.html#files">files</a> are undefined in terms of order.</p>
<p>&lt;classpath&gt; &lt;pathelement path="${classpath}"/&gt; &lt;fileset dir="lib"&gt; &lt;include name="**/*.jar"/&gt; &lt;/fileset&gt; &lt;pathelement location="classes"/&gt; &lt;dirset dir="${build.dir}"&gt; &lt;include name="apps/**/classes"/&gt; &lt;exclude name="apps/**/*Test*"/&gt; &lt;/dirset&gt; &lt;filelist refid="third-party_jars"/&gt; &lt;/classpath&gt; </p>
<p>&nbsp;</p>
<pre>
<p>This builds a path that holds the value of <code>${classpath}</code>, followed by all jar files in the <code>lib</code> directory, the <code>classes</code> directory, all directories named <code>classes</code> under the <code>apps</code> subdirectory of <code>${build.dir}</code>, except those that have the text <code>Test</code> in their name, and the files specified in the referenced FileList.</p>
<p>If you want to use the same path-like structure for several tasks, you can define them with a <code>&lt;path&gt;</code> element at the same level as <em>target</em>s, and reference them via their <em>id</em> attribute--see <a href="http://coolbi.javaeye.com/blog/192600#references">References</a> for an example.</p>
<p>A path-like structure can include a reference to another path-like structure (a path being itself a resource collection) via nested <code>&lt;path&gt;</code> elements:</p>
<p>&lt;path id="base.path"&gt; &lt;pathelement path="${classpath}"/&gt; &lt;fileset dir="lib"&gt; &lt;include name="**/*.jar"/&gt; &lt;/fileset&gt; &lt;pathelement location="classes"/&gt; &lt;/path&gt; &lt;path id="tests.path"&gt; &lt;path refid="base.path"/&gt; &lt;pathelement location="testclasses"/&gt; &lt;/path&gt; </p>
<p>&nbsp;</p>
<pre>
<p>The shortcuts previously mentioned for <code>&lt;classpath&gt;</code> are also valid for <code>&lt;path&gt;</code>.For example: </p>
<p>&lt;path id="base.path"&gt; &lt;pathelement path="${classpath}"/&gt; &lt;/path&gt; </p>
<p>&nbsp;</p>
<pre>
<p>can be written as: </p>
<p>&lt;path id="base.path" path="${classpath}"/&gt; </p>
<p>&nbsp;</p>
<pre>
<h3><a name="arg">Command-line Arguments</a></h3>
<p>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 <code>arg</code> elements can be used.</p>
<p>
</p>
<table cellspacing="0" cellpadding="2" border="1">
    <tbody>
        <tr>
            <td valign="top" width="12%"><strong>Attribute</strong></td>
            <td valign="top" width="78%"><strong>Description</strong></td>
            <td valign="top" width="10%"><strong>Required</strong></td>
        </tr>
        <tr>
            <td valign="top">value</td>
            <td valign="top">a single command-line argument; can contain space characters.</td>
            <td align="center" rowspan="5">Exactly one of these.</td>
        </tr>
        <tr>
            <td valign="top">file</td>
            <td valign="top">The name of a file as a single command-line argument; will be replaced with the absolute filename of the file.</td>
        </tr>
        <tr>
            <td valign="top">path</td>
            <td valign="top">A string that will be treated as a path-like string as a single command-line argument; you can use <code>;</code>or <code>:</code>as path separators and <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> will convert it to the platform's local conventions.</td>
        </tr>
        <tr>
            <td valign="top">pathref</td>
            <td valign="top"><a href="http://coolbi.javaeye.com/blog/192600#references">Reference</a> to a path defined elsewhere. <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> will convert it to the platform's local conventions.</td>
        </tr>
        <tr>
            <td valign="top">line</td>
            <td valign="top">a space-delimited list of command-line arguments.</td>
        </tr>
    </tbody>
</table>
<p>
<p>It is highly recommended to avoid the <code>line</code> version when possible. <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> 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.</p>
<h4>Examples</h4>
<blockquote>&lt;arg value="-l -a"/&gt;
<pre></pre>
</blockquote>
<p>is a single command-line argument containing a space character, <em>not</em> separate commands "-l" and "-a".</p>
<blockquote>&lt;arg line="-l -a"/&gt;
<pre></pre>
</blockquote>
<p>This is a command line with two separate arguments, "-l" and "-a".</p>
<blockquote>&lt;arg path="/dir;/dir2:\dir3"/&gt;
<pre></pre>
</blockquote>
<p>is a single command-line argument with the value <code>\dir;\dir2;\dir3</code> on DOS-based systems and <code>/dir:/dir2:/dir3</code> on Unix-like systems.</p>
<h3><a name="references">References</a></h3>
<p>Any project element can be assigned an identifier using its <code>id</code> attribute. In most cases the element can subsequently be referenced by specifying the <code>refid</code> 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 <code>&lt;classpath&gt;</code> structure more than once, for example.</p>
<p>The following example:</p>
<blockquote>&lt;project ... &gt; &lt;target ... &gt; &lt;rmic ...&gt; &lt;classpath&gt; &lt;pathelement location="lib/"/&gt; &lt;pathelement path="${java.class.path}/"/&gt; &lt;pathelement path="${additional.path}"/&gt; &lt;/classpath&gt; &lt;/rmic&gt; &lt;/target&gt; &lt;target ... &gt; &lt;javac ...&gt; &lt;classpath&gt; &lt;pathelement location="lib/"/&gt; &lt;pathelement path="${java.class.path}/"/&gt; &lt;pathelement path="${additional.path}"/&gt; &lt;/classpath&gt; &lt;/javac&gt; &lt;/target&gt; &lt;/project&gt;
<pre></pre>
</blockquote>
<p>could be rewritten as:</p>
<blockquote>&lt;project ... &gt; &lt;path id="project.class.path"&gt; &lt;pathelement location="lib/"/&gt; &lt;pathelement path="${java.class.path}/"/&gt; &lt;pathelement path="${additional.path}"/&gt; &lt;/path&gt; &lt;target ... &gt; &lt;rmic ...&gt; &lt;classpath refid="project.class.path"/&gt; &lt;/rmic&gt; &lt;/target&gt; &lt;target ... &gt; &lt;javac ...&gt; &lt;classpath refid="project.class.path"/&gt; &lt;/javac&gt; &lt;/target&gt; &lt;/project&gt;
<pre></pre>
</blockquote>
<p>All tasks that use nested elements for <a href="http://coolbi.javaeye.com/blog/CoreTypes/patternset.html">PatternSet</a>s, <a href="http://coolbi.javaeye.com/blog/CoreTypes/fileset.html">FileSet</a>s, <a href="http://coolbi.javaeye.com/blog/CoreTypes/zipfileset.html">ZipFileSet</a>s or <a href="http://coolbi.javaeye.com/blog/192600#path">path-like structures</a> accept references to these structures as shown in the examples. Using <code>refid</code> 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 <a href="http://coolbi.javaeye.com/blog/CoreTasks/property.html">property</a> task is a handy example) deliberately assign a different meaning to <code>refid</code>.</p>
<h3><a name="external-tasks">Use of external tasks</a></h3>
<p><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> supports a plugin mechanism for using third party tasks. For using them you have to do two steps: </p>
<ol>
    <li>place their implementation somewhere where <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> can find them
    <li>declare them.
    </li>
</ol>
<p>Don't add anything to the CLASSPATH environment variable - this is often the reason for very obscure errors. Use <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span>'s own <a href="http://coolbi.javaeye.com/blog/install.html#optionalTasks">mechanisms</a> for adding libraries: </p>
<ul>
    <li>via command line argument <code>-lib</code>
    <li>adding to <code>${user.home}/.<span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">ant</span></span></span></span></span></span></span></span></span>/lib</code>
    <li>adding to <code>${<span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">ant</span></span></span></span></span></span></span></span></span>.home}/lib</code>
    </li>
</ul>
<p>For the declaration there are several ways: </p>
<ul>
    <li>declare a single task per using instruction using <code>&lt;<a href="http://coolbi.javaeye.com/blog/CoreTasks/taskdef.html">taskdef</a> name="taskname" classname="ImplementationClass"/&gt;</code> <br />
    <code>&lt;taskdef name="for" classname="net.sf.antcontrib.logic.For" /&gt; &lt;for ... /&gt;</code>
    <li>declare a bundle of tasks using a properties-file holding these taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code> <br />
    <code>&lt;taskdef resource="net/sf/antcontrib/antcontrib.properties" /&gt; &lt;for ... /&gt;</code>
    <li>declare a bundle of tasks using a <a href="http://coolbi.javaeye.com/blog/CoreTypes/antlib.html">xml-file</a> holding these taskname-ImplementationClass-pairs and <code>&lt;taskdef&gt;</code> <br />
    <code>&lt;taskdef resource="net/sf/antcontrib/antlib.xml" /&gt; &lt;for ... /&gt;</code>
    <li>declare a bundle of tasks using a xml-file named antlib.xml, XML-namespace and <a href="http://coolbi.javaeye.com/blog/CoreTypes/antlib.html#antlibnamespace"><code>antlib:</code> protocoll handler</a> <br />
    <code>&lt;project xmlns:ac="antlib:net.sf.antconrib"/&gt; &lt;ac:for ... /&gt;</code>
    </li>
</ul>
<p>If you need a special function, you should </p>
<ol>
    <li>have a look at this manual, because <span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> provides lot of tasks
    <li>have a look at the external task page in the <a href="http://coolbi.javaeye.com/external.html">manual</a> (or better <a href="http://ant.apache.org/external.html">online</a>)
    <li>have a look at the external task <a href="http://wiki.apache.org/ant/AntExternalTaskdefs">wiki page</a>
    <li>ask on the <a href="http://ant.apache.org/mail.html#User%20List"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1"><span class="hilite1">Ant</span></span></span></span></span></span></span></span></span> user</a> list
    <li><a href="http://coolbi.javaeye.com/blog/tutorial-writing-tasks.html">implement </a>(and share) your own
    </li>
</ol>
<p>&nbsp;</p>
<h3>Token Filters</h3>
<p>&nbsp;</p>
<h3>Example Buildfile</h3>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
</pre>
<img src ="http://www.blogjava.net/caizh2009/aggbug/279701.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/caizh2009/" target="_blank">小菜毛毛</a> 2009-06-02 21:41 <a href="http://www.blogjava.net/caizh2009/articles/279701.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>WHAT IS ANT</title><link>http://www.blogjava.net/caizh2009/articles/278935.html</link><dc:creator>小菜毛毛</dc:creator><author>小菜毛毛</author><pubDate>Fri, 29 May 2009 11:06:00 GMT</pubDate><guid>http://www.blogjava.net/caizh2009/articles/278935.html</guid><wfw:comment>http://www.blogjava.net/caizh2009/comments/278935.html</wfw:comment><comments>http://www.blogjava.net/caizh2009/articles/278935.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/caizh2009/comments/commentRss/278935.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/caizh2009/services/trackbacks/278935.html</trackback:ping><description><![CDATA[<pre>ANT介绍
－. Ant简介
Ant是一个类似make的工具，用来编译/运行/测试java程序。
构建、包装和发布过程中几乎每一件事都可以由Ant的任务来处理.
二．Ant的安装及配置
你可以从Jakata网站下载预编译的ANT,解压至任一目录；
设置自己的环境变量，即ANT_HOME，指向解压目录;
设置JAVA_HOME环境变量,指向jdk的根目录;
三：用ant管理项目
Ant一启动就会自动地加载一个叫做Build.xml的项目配置文件。如果想给这个项目配置文件取别的名字，你可以使用buildfile标记来运行ant，就像下面这样：
ant -buildfile Project_configuration.xml
主要特点：
使用xml文件作为配置文件，即build.xml；
可与junit作无缝整合，达到系统测试、编译到发布的功能；
强大、详细的报表功能；
控制灵活，可自定义要执行的任务组合。
build.xml主要节点：
proejct 项目
name  指定工程名称
default  指定默认的target（即任务）
basedir  指定系统的基本路径
property 属性，类似于全局变量
name  变量名称
value  变量值
属性访问方法： ${property}
内置属性：
basedir             工程的路径.
ant.file            build文件的路径.
ant.version         版本
ant.project.name    工程名
ant.java.version    jvm版本
target 目标，即任务
name  指定目标名称
depends  指定所依整的目标
if  条件，可达到控制流程目的
unless
description
task 一小段的任务
reference 引用
通过refid属性来引用在其他节中定义的id
内置命令：
&lt;tstamp/&gt;  建立时间
&lt;mkdir dir="${var}"/&gt; 建立目录
编译文件：
&lt;javac srcdir="${src}" destdir="${classes}" debug="off"&gt;
&lt;classpath refid="appclasspath"/&gt;
&lt;include name="**/*.java" /&gt;
&lt;/javac&gt;
执行文件：
&lt;exec executable="${base.dir}/email.bat" &gt;
&lt;/exec&gt;
junit命令：
&lt;junit&gt;
&lt;classpath refid="appclasspath"/&gt;
&lt;classpath&gt;
&lt;pathelement location="${base.dir}/defaultroot/WEB-INF/classes"/&gt;
&lt;/classpath&gt;
&lt;formatter type="xml"/&gt;
&lt;test name="junit.tests.AllTests" haltonfailure="no" outfile="result"/&gt;
&lt;/junit&gt;
&lt;junitreport todir="./report"&gt;
&lt;fileset dir="."&gt;
&lt;include name="result.xml"/&gt;
&lt;/fileset&gt;
&lt;report format="noframes" todir="./report"/&gt;
&lt;/junitreport&gt;
四：运行ant
ant [options] [target [target2 [target3] ...]]
Options:
-logfile &lt;file&gt;        use given file for log
-l     &lt;file&gt;                ''
-buildfile &lt;file&gt;      use given buildfile
-file    &lt;file&gt;              ''
-f       &lt;file&gt;              ''
-D&lt;property&gt;=&lt;value&gt;   use value for given property
-propertyfile &lt;name&gt;   load all properties from file with -D
properties taking precedence </pre>
<img src ="http://www.blogjava.net/caizh2009/aggbug/278935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/caizh2009/" target="_blank">小菜毛毛</a> 2009-05-29 19:06 <a href="http://www.blogjava.net/caizh2009/articles/278935.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ant入门教程</title><link>http://www.blogjava.net/caizh2009/articles/278936.html</link><dc:creator>小菜毛毛</dc:creator><author>小菜毛毛</author><pubDate>Fri, 29 May 2009 11:06:00 GMT</pubDate><guid>http://www.blogjava.net/caizh2009/articles/278936.html</guid><wfw:comment>http://www.blogjava.net/caizh2009/comments/278936.html</wfw:comment><comments>http://www.blogjava.net/caizh2009/articles/278936.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/caizh2009/comments/commentRss/278936.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/caizh2009/services/trackbacks/278936.html</trackback:ping><description><![CDATA[<table cellspacing="0" cellpadding="0" width="100%" border="0">
    <tbody>
        <tr>
            <td valign="top" width="358">
            <div style="text-indent: 21pt">本内容包含了Ant的历史简要介绍，Ant的功能以及Ant框架的介绍，并对下载安装使用Ant进行了示例介绍，同时通过一个Java程序讲解了Ant的基本使用方法。<br />
            <br />
            <br />
            </div>
            <div style="margin-left: 21pt; text-indent: -21pt"><span class="STYLE1"><strong>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </strong><strong>Ant简介：这里引用Ant帮助文档中对Ant</strong><strong>的介绍：</strong></span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; line-height: 12pt"><br />
            &nbsp;&nbsp;&nbsp;&nbsp; Apache Ant是一个基于Java的构建工具。从理论上讲，也是一种类似于Make的工具，只是去除了Make工具的缺点。</div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; line-height: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;既然已经有了make, gnumake, nmake, jam以及其他的构件工具，为什么还要Ant呢？因为Ant的早期开发者发现所有以上这些工具都或多或少的有一些局限性，使得在跨平台开发软件成为困难。类似于Make的工具都是传统的基于Shell的--首先进行依赖性检查，然后执行命令。这意味着你可以轻易的通过使用或者编写程序来扩展这些工具，以满足不同的平台。当然，这也意味着你将局限于特定的平台，至少可以说局限于特定类型的平台，例如：Unix平台。<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
            <br />
            </div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; line-height: 12pt"></div>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="20">
            <div style="margin-top: 0cm; margin-bottom: 0pt; line-height: 12pt">
            <div style="margin-top: 0cm; margin-bottom: 0pt; line-height: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;同时，Make文件也有一些先天的缺陷。好多人都会遇到恐怖的tab问题。Ant的最初开发者多次说&#8220;我的命令不能执行因为我在tab前面加了一个空格！&#8221;。一些工具如Jam一定程序上解决了这个问题，但仍有其它的格式问题。<br />
            <br />
            <br />
            </div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; line-height: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;Ant与从基于命令的那些扩展开来的那些工具不同，Ant是由java类扩展的。不用编写shell命令，而是配置基于XML的文件，形成多个任务的目标配置树。每一个任务都是通过一个实现了一个规定接口的java类来运行的。</div>
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;ant缺少了一些直接执行shell命令的能力，如find . -name foo -exec rm {}，但它给用户提供了跨平台的能力，可以在任何地方工作。实际上，Ant也提供了命令execute用来执行shell命令,这就是它的任务，它允许执行基于操作系统的命令。<br />
            <br />
            <br />
            </div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 0cm"><span style="font-size: 10.5pt">&nbsp;&nbsp;&nbsp;&nbsp;</span>简单的说，Ant是一个基于Java，并且主要用于Java工程的构建工具。Ant本意是Another Neat Tool,也就是另一种整洁的工具，取首字符就是Ant。<br />
            <br />
            <br />
            </div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 0cm">构建工具就是为了减少重复工作而产生的。<br />
            <br />
            <br />
            </div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt; text-indent: -21pt"><span style="color: #0000ff"><strong><span style="font-size: 10.5pt">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ant的一些核心概念：</span></strong></span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">XML:</span><span style="font-size: 10.5pt">构建文件是以</span><span style="font-size: 10.5pt">XML</span><span style="font-size: 10.5pt">文件来描述的，采用</span><span style="font-size: 10.5pt">XML</span><span style="font-size: 10.5pt">格式有很多好处。这里就不一一列举。</span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt">陈述式语法：构建文件短小精悍，且易于理解。</div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">每个构建文件包含一个工程</span><span style="font-size: 10.5pt">(project)</span><span style="font-size: 10.5pt">。</span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">每个工程包含若干个目标</span><span style="font-size: 10.5pt">(target)</span><span style="font-size: 10.5pt">。</span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">目标可以依赖于其他的目标</span><span style="font-size: 10.5pt">(depends)</span><span style="font-size: 10.5pt">。</span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">目标包含任务</span><span style="font-size: 10.5pt">(task)</span><span style="font-size: 10.5pt">。</span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">易于使用</span><span style="font-size: 10.5pt">Java</span><span style="font-size: 10.5pt">语言增加新的任务</span><span style="font-size: 10.5pt">---</span><span style="font-size: 10.5pt">易于扩展（自定义）。</span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt; text-indent: -21pt"><span class="STYLE1"><strong><span style="font-size: 10.5pt">3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></strong><strong><span style="font-size: 10.5pt">Ant</span></strong><strong><span style="font-size: 10.5pt">结构：</span></strong></span></div>
            <div style="margin-top: 0cm; margin-bottom: 0pt; margin-left: 21pt"><span style="font-size: 10.5pt">Ant</span><span style="font-size: 10.5pt">的结构如下图所示：</span></div>
            <div style="margin-top: 0cm; text-justify: inter-ideograph; margin-bottom: 0pt; margin-left: 0cm"><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/1dfd5fb0860e49caa4ac48253544cb79.png" /></div>
            <div style="margin-top: 0cm; text-justify: inter-ideograph; margin-bottom: 0pt; margin-left: 0cm"><span style="font-size: 10.5pt"><br />
            构建文件的概念视图：工程包含一个目标的集合。在每个目标里是任务的声明，它们是对</span><span style="font-size: 10.5pt">Ant</span><span style="font-size: 10.5pt">用于构建该目标的行为说明。目标生成一个依赖关系图表来声明该目标的依赖关系。当执行一个目标时，必须先执行它们依赖的目标。</span></div>
            <div style="margin-top: 0cm; text-justify: inter-ideograph; margin-bottom: 0pt; margin-left: 0cm">&nbsp;</div>
            <div style="margin-top: 0cm; text-justify: inter-ideograph; margin-bottom: 0pt; margin-left: 0cm"><strong><span style="font-size: 10.5pt"><span class="STYLE1">例子：一个典型的构建文件</span>：</span></strong></div>
            <div>&lt;?xml version="1.0" ?&gt;</div>
            <div>&lt;project name="OurProject" default="deploy"&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="init"&gt;</div>
            <div style="text-indent: 21pt">&lt;mkdir dir="build/classes" /&gt;</div>
            <div style="text-indent: 21pt">&lt;mkdir dir="dist" /&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="compile" depends="init" &gt;</div>
            <div style="text-indent: 21pt">&lt;javac srcdir="src" destdir="build/classes"/&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="doc" depends="init" &gt;</div>
            <div style="text-indent: 21pt">&lt;javadoc destdir="build/classes" sourcepath="src" packagenames="org.*" /&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="deploy" depends="compile,doc" &gt;</div>
            <div style="text-indent: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&lt;jar destfile="dist/project.jar" basedir="build/classes"/&gt; <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ftp server="<span style="color: red">"</span> userid="<span style="color: red">"</span> password="<span style="color: red">"&gt;</span>
            <div style="text-indent: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fileset dir="dist"/&gt;</div>
            <div style="text-indent: 21pt">&lt;/ftp&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div>&lt;/project&gt;</div>
            <div>&nbsp;</div>
            <div><strong>该构建过程如下：</strong></div>
            <div>系统初始化1、编译&nbsp;&nbsp;2、生成JAVADOC&nbsp;&nbsp;&nbsp;4、打包&nbsp;&nbsp;5、上传到FTP，其中后两步结合到一起叫部署。</div>
            <div>&nbsp;</div>
            <div>执行时输出如下：</div>
            <div>&gt; ant -propertyfile ftp.properties</div>
            <div>Buildfile: build.xml</div>
            <div>init:</div>
            <div>[mkdir] Created dir: /home/ant/Projects/OurProject/build/classes</div>
            <div>[mkdir] Created dir: /home/ant/Projects/OurProject/dist</div>
            <div>compile:</div>
            <div>[javac] Compiling 1 source file to /home/ant/Projects/OurProject/build/</div>
            <div>classes</div>
            <div>doc:</div>
            <div>[javadoc] Generating Javadoc</div>
            <div>[javadoc] Javadoc execution</div>
            <div>[javadoc] Loading source files for package org.example.antbook.lesson1...</div>
            <div>[javadoc] Constructing Javadoc information...</div>
            <div>[javadoc] Building tree for all the packages and classes...</div>
            <div>[javadoc] Building index for all the packages and classes...</div>
            <div>[javadoc] Building index for all classes...</div>
            <div>deploy:</div>
            <div>[jar] Building jar: /home/ant/Projects/OurProject/dist/project.jar</div>
            <div>[ftp] sending files</div>
            <div>[ftp] 1 files sent</div>
            <div>BUILD SUCCESSFUL</div>
            <div>Total time: 5 seconds.</div>
            <div><br />
            在执行时使用命令行参数以传入一个属性文件，属性文件中包含连接FTP服务器使用的服务器名，用户名，用户密码来给<span style="color: red">特性使用。</span></div>
            <div>这个例子很好的展示了Ant的一些基本要素：目标依赖、特性的使用、编译、文档生成、JAR打包（tar,Zip,WAR,EAR等），最后是部署。</div>
            <div>&nbsp;</div>
            <div>Ant的简单任务(&lt;mkdir&gt;)都是由Java类库来实现相应的功能。而一些复杂的任务&lt;ftp&gt;、&lt;junit&gt;还需要第三方库的支持。</div>
            <div>&nbsp;</div>
            <div>&nbsp;&nbsp;&nbsp;&nbsp;Ant的一个强大之处：它总能工作。只要正确的指定构建文件，Ant就能计算出目标的依赖性，并且按照正确的顺序调用目标。目标通过任务按序执行，而任务自身处理其文件依赖性以及实际的操作来完成工作。因为每个任务通常都是在高层陈述，所以一两行XML语句经常就已经足够描述任务的内容。</div>
            <div>&nbsp;</div>
            <div style="margin-left: 21pt; text-indent: -21pt"><span class="STYLE1"><strong>4.&nbsp;&nbsp;&nbsp;</strong><strong>下载并安装</strong><strong>Ant</strong></span></div>
            <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用Ant前提条件，系统中已经安装JDK以及Ant。在文档编写之时，Ant的最新版本是Ant 1.7,但是为了稳定性，本文档使用版本为Ant 1.6.5.</div>
            <div>首先下载Ant，到apache软件网站<a href="http://www.apache.org/">http://www.apache.org/</a>。</div>
            <div>其次，解压缩文件，放到指定的系统目录中，例如C:\Ant。</div>
            <div>再次，将其添加到path，以便从命令行使用。(一些IDE，例如Eclipse可以不需要设置path,而通过IDE相关设置将Ant添加到path中。)</div>
            <div>再次，设置一些环境变量指向JDK以及ANT。</div>
            <div>最后，添加需要的可选库。</div>
            <div>&nbsp;</div>
            <div>在Windows安装过程（以笔者的安装过程为例）</div>
            <div>&nbsp;&nbsp;&nbsp;&nbsp;下载apache-ant-1.6.5-bin.zip到本地硬盘，解压缩之后将文件夹命名为Ant,放在C:\Ant中。这个目录就是Ant主目录。</div>
            <div>应该将主目录中的bin目录添加到path属性中，这样就可以在命令行中调用ant命令，ANT_HOME是批处理文件所在目录的上级目录。最好明确设定。</div>
            <div><span style="color: black">现在许多工具已经集成了特定版本的</span><span style="color: black">Ant</span><span style="color: black">，一些操作系统甚至默认的已经安装了</span><span style="color: black">Ant</span><span style="color: black">。所以，你的系统中可能已经安装了</span><span style="color: black">Ant</span><span style="color: black">。</span></div>
            <div>首先可以通过运行以下命令：<br />
            <strong>ant -version</strong><br />
            和<br />
            ant -diagnostics <br />
            来确定。我们推荐您不设置CLASSPATH来运行Ant命令。如果任何版本的Ant可以从CLASSPATH加载 ，这时就会由于加载了不兼容的类而产生许多错误。</div>
            <div>&nbsp;</div>
            <div>一些其他问题请参阅Ant的FAQ设置。</div>
            <div>正常情况下，执行ant ?Cversion即可显示Ant版本，则说明安装配制成功：</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/ea29812585e24474a75be50e7407e4be.png" /></div>
            <div>&nbsp;</div>
            <div style="margin-left: 21pt; text-indent: -21pt"><strong>5.<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></strong><strong>运行第一个构建文件：</strong></div>
            <div style="margin-left: 21pt">首先创建一个Java工程，名为AntProject，工程中源文件和目标文件是分开的，分别为文件夹src和bin,然后创建一个Java类文件，类名为</div>
            <div style="margin-left: 21pt">com.neusoft.test.AntTest，只是为了测试，所以类的内容很简单：</div>
            <div style="margin-left: 21pt">package com.neusoft.test;</div>
            <div style="margin-left: 21pt">/**</div>
            <div style="margin-left: 21pt">&nbsp;*This is just a test class.</div>
            <div style="margin-left: 21pt">&nbsp;*/</div>
            <div style="margin-left: 21pt">public class AntTest{</div>
            <div style="margin-left: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String[] args){</div>
            <div style="margin-left: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i=0;i&lt;args.length;i++){</div>
            <div style="margin-left: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(args[i]);</div>
            <div style="margin-left: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
            <div style="margin-left: 21pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
            <div style="margin-left: 21pt">}</div>
            <div style="margin-left: 21pt">&nbsp;</div>
            <div style="margin-left: 21pt"><strong>然后我们在工程的路径下面建立一个构建文件</strong><strong>build.xml</strong><strong>，内容如下：</strong></div>
            <div>&lt;?xml version="1.0" ?&gt;</div>
            <div>&lt;project name="structured" default="archive" &gt;</div>
            <div style="text-indent: 21pt">&lt;target name="init"&gt;</div>
            <div style="text-indent: 21pt">&lt;mkdir dir="build/classes" /&gt;</div>
            <div style="text-indent: 21pt">&lt;mkdir dir="dist" /&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="compile" depends="init" &gt;</div>
            <div style="text-indent: 21pt">&nbsp;&nbsp;&lt;javac srcdir="src" destdir="build/classes"/&gt;</div>
            <div style="text-indent: 21pt"></div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="archive" depends="compile" &gt;</div>
            <div style="text-indent: 21pt">&lt;jar destfile="dist/project.jar"</div>
            <div style="text-indent: 21pt">basedir="build/classes" /&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div style="text-indent: 21pt">&lt;target name="clean" depends="init"&gt;</div>
            <div style="text-indent: 21pt">&lt;delete dir="build" /&gt;</div>
            <div style="text-indent: 21pt">&lt;delete dir="dist" /&gt;</div>
            <div style="text-indent: 21pt">&lt;/target&gt;</div>
            <div>&lt;/project&gt;<br />
            </div>
            <div>构建文件说明如下图：</div>
            <div style="text-indent: 21pt"><img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/d13b06a92da34475a024ee7923683186.png" /></div>
            <div><br />
            <br />
            关于XML的知识，请参考其他书籍，这里不做介绍。</div>
            <div>以上创建完成后，目录结构如下图：<br />
            <br />
            </div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/18957dabfc4747ea9fa861b1101371af.png" /></div>
            <div>&nbsp;<br />
            </div>
            <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ant构建文件总是有一个&lt;project&gt;元素做为根元素，它有两个属性，name和default，&lt;target&gt;元素是&lt;project&gt;元素的子元素，可以有多个，它有两个属性，name和depends，&lt;target&gt;元素包含的元素就是一些任务元素。<br />
            <br />
            </div>
            <div>&lt;target&gt;可以由命令行进行显示的调用，也可以在内部使用如可以直接调用ant init、ant compile等。如果不写参数，则默认的build文件是build.xml，默认的目标是&lt;project&gt;的default属性定义的目标。目标的名称是唯一的，可以是任意字符串。</div>
            <div>&nbsp;</div>
            <div>下面我们先运行一下这个Ant构建，再讲解其他的内容，进入工程目录，执行</div>
            <div>ant</div>
            <div>这里就相当于执行默认的目标，也就是&lt;project name="structured" default="archive" &gt;中的archive目标。</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/bf58caa3631043c9934e37a8c6842cd3.png" /></div>
            <div>&nbsp;</div>
            <div>这里说明了首先初始化创建两个目录，然后编译了一个JAVA文件，然后进行了打包的操作。</div>
            <div>&nbsp;</div>
            <div><strong>这里讲解一下如果构建失败了怎么办？</strong></div>
            <div>首先有可能是XML语法书写不正确(将&lt;target&gt;写成&lt;targe&gt;)，或者在任务执行过程中出现了错误(.java文件中包含编译错误)，或者任务名称书写错误(将&lt;javac&gt;写成&lt;javacc&gt;)等等，这些都不是Ant的错误，不需要填写Bug Report。写XML时一定要细心，一些IDE已经有验证功能，可以很好的防止书写的错误。</div>
            <div>&nbsp;</div>
            <div><strong>出现错误时，可以使用</strong></div>
            <div>ant ?Cverbose</div>
            <div>或者</div>
            <div>ant ?Cdebug来获取更加详细的构建信息，以解决问题。</div>
            <div>下图是使用ant ?Cverbose时的输出，使用ant ?Cdebug将获取比这更详细的信息，这里就不举例了。</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/457f2ee754f04d1380a08a6bc45e55b9.png" /></div>
            <div>&nbsp;</div>
            <div>本例中直接使用了软件工程中的构建结构，使用src作为源文件目录，build/class作为中间生成文件，以dist作为可发布文件。在最后把一些可执行文件可以放在bin目录中。此时目录结构如下图所示：</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/3112c28771af45a3be22d7e26e77836d.png" /></div>
            <div>&nbsp;</div>
            <div>我们需要一种办法来确定某些任务先执行，而有些任务后执行，比如必须先编译，才能执行程序或者打包。我们在声明目标的时候，就在其依赖属性中列出其依赖关系：</div>
            <div>&lt;target name="compile" depends="init" &gt;</div>
            <div>&lt;target name="archive" depends="compile" &gt;</div>
            <div>&lt;target name="clean" depends="init"&gt;</div>
            <div>如果一个目标依赖与多个其他目标，需要将它们都写到依赖属性中，例如：</div>
            <div>depents=&#8221;compile,test&#8221;。在我们的构建中，archive依赖于init和compile，但是我们不需要去写，因为compile已经依赖于init了。即：Ant的依赖关系是传递的，但不是自反的。</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/ca47e0d3fd364097922f0f809a976277.png" /></div>
            <div><br />
            如果在执行过程中两个目标共享同一个目标，则先导目标只被执行一次。</div>
            <div>可以通过指定目标来运行构建：</div>
            <div>例如执行完ant后，可以执行ant clean来清理构建：</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/19f835f2ad08455fa56fb807fa63c69e.png" /></div>
            <div>&nbsp;</div>
            <div>ant等价于ant archive</div>
            <div>ant init</div>
            <div>ant clean</div>
            <div>ant compile</div>
            <div>ant archive</div>
            <div>ant clean archive</div>
            <div>&nbsp;</div>
            <div><strong>当构建完成一次以后，再次执行构建会发生什么呢？</strong></div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/904f2e88b87947d09c5195ed45dbb523.png" /></div>
            <div>&nbsp;</div>
            <div>第二次执行构建时只花了2s，相比第一次的4s。并且没有任何一个目标表示做了任何工作。</div>
            <div>原因如下：所有的任务都检查了它们的依赖关系：</div>
            <div>&lt;mkdir&gt;没有创建目录因为已经存在</div>
            <div>&lt;javac&gt;比较了源文件和类文件的时间戳</div>
            <div>&lt;jar&gt;比较了要被加入文件与已经存在文件的时间</div>
            <div>只有更新的时候才进行任务执行。</div>
            <div>&nbsp;</div>
            <div><strong>Ant</strong><strong>如何处理命令行上的多个目标？</strong></div>
            <div>执行ant compile archive会怎么样？</div>
            <div>先实验一下：</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/b563e0adc32748f1a08f9ba0f2fb4667.png" /></div>
            <div>&nbsp;</div>
            <div>Ant依次执行每个目标和其依赖目标，即Ant的执行顺序是init compile init compile archive，虽然这样看起来增加了额外的工作，但是通过上面的执行过程就会发现，由于其依赖性检查的阻止，第二次的init和compile并未真正的执行，执行时间与直接执行archive的时间是一样的。</div>
            <div>&nbsp;</div>
            <div><strong>运行程序：</strong></div>
            <div>普通执行该类的方法是：</div>
            <div>java ?Ccp build/class com.neusoft.test.AntTest args1 args2</div>
            <div>而我们使用Ant的任务来执行它仅仅需要增加一个任务，好处在于：</div>
            <div style="text-indent: 21pt">让用于执行的目标依赖与编译的目标，确保运行最新版本</div>
            <div style="text-indent: 21pt">易于传递复杂参数</div>
            <div style="text-indent: 21pt">设置classpath更方便</div>
            <div style="text-indent: 21pt">在Ant自身的JVM中运行，载入更快</div>
            <div style="text-indent: 21pt"></div>
            <div>增加一个新的目标：</div>
            <div>&lt;target name="execute" depends="compile"&gt;</div>
            <div style="text-indent: 10.5pt">&lt;java classname="com.neusoft.test.AntTest"</div>
            <div style="text-indent: 10.5pt">classpath="build/classes"&gt;</div>
            <div style="text-indent: 10.5pt">&lt;arg value="a"/&gt;</div>
            <div style="text-indent: 10.5pt">&lt;arg value="b"/&gt;</div>
            <div style="text-indent: 10.5pt">&lt;arg file="."/&gt;</div>
            <div style="text-indent: 10.5pt">&lt;/java&gt;</div>
            <div>&lt;/target&gt;</div>
            <div>&nbsp;</div>
            <div>最后一个参数是file=&#8221;.&#8221;，表示传入的参数是一个目录，为文件绝对路径。</div>
            <div>运行该目标，输出如下：</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/8c8d4063542a498a9ef5d88fd5867661.png" /></div>
            <div>&nbsp;</div>
            <div>Ant命令行选项：</div>
            <div><img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/a1affbf572d04d559f61b9df8722fd57.png" /></div>
            <div>&nbsp;</div>
            <div>请参阅相关手册进行查询相关选项的功能。</div>
            <div>&nbsp;</div>
            <div><strong>当有多个构建文件时，可以指定构建文件：</strong></div>
            <div>ant ?Cbuildfile build.xml compile</div>
            <div>来表示执行build.xml这个构建文件中的compile目标。</div>
            <div>&nbsp;</div>
            <div>控制提供的信息量：</div>
            <div>ant ?Cquiet:安静模式，不给出任何输出。</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/e93c45b2b38f465783cc7dc668e69b4f.png" /></div>
            <div>&nbsp;</div>
            <div>ant ?Cemacs:简单模式，不显示任务名称。</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/96fc30525694483ea06e5d2f3764bcae.png" /></div>
            <div>&nbsp;</div>
            <div>ant ?Cprojecthelp:获取项目信息。</div>
            <div><br />
            <img alt="" src="http://www.java3z.com/cwbwebhome/article/article2/img1/52050191b8974e1e955de48d68730446.png" /></div>
            <div>&nbsp;</div>
            <div><strong>最终的构建文件，添加了</strong><strong>description</strong><strong>属性。</strong></div>
            <div>&lt;?xml version="1.0" ?&gt;</div>
            <div>&lt;project name="secondbuild" <strong>default="execute" </strong>&gt;</div>
            <div style="text-indent: 10.5pt"><strong>&lt;description&gt;Compiles and runs a simple program&lt;/description&gt;</strong></div>
            <div style="text-indent: 10.5pt">&lt;target name="init"&gt;</div>
            <div style="text-indent: 10.5pt">&lt;mkdir dir="build/classes" /&gt;</div>
            <div style="text-indent: 21pt">&lt;mkdir dir="dist" /&gt;</div>
            <div style="text-indent: 10.5pt">&lt;/target&gt;</div>
            <div style="text-indent: 10.5pt">&lt;target name="compile" depends="init"</div>
            <div style="text-indent: 10.5pt"><strong>description="Compiles the source code"</strong>&gt;</div>
            <div style="text-indent: 10.5pt">&lt;javac srcdir="src" destdir="build/classes"/&gt;</div>
            <div style="text-indent: 10.5pt">&lt;/target&gt;</div>
            <div style="text-indent: 10.5pt">&lt;target name="archive" depends="compile"</div>
            <div style="text-indent: 10.5pt"><strong>description="Creates the JAR file"</strong>&gt;</div>
            <div style="text-indent: 10.5pt">&lt;jar destfile="dist/project.jar" basedir="build/classes"/&gt;</div>
            <div style="text-indent: 10.5pt">&lt;/target&gt;</div>
            <div>&lt;target name="clean" depends="init"</div>
            <div style="text-indent: 10.5pt"><strong>description="Removes the temporary directories used"</strong>&gt;</div>
            <div style="text-indent: 10.5pt">&lt;delete dir="build" /&gt;</div>
            <div style="text-indent: 21pt">&lt;delete dir="dist" /&gt;</div>
            <div style="text-indent: 10.5pt">&lt;/target&gt;</div>
            <div style="text-indent: 10.5pt">&lt;target name="execute" depends="compile"</div>
            <div><strong>description="Runs the program"</strong>&gt;</div>
            <div style="text-indent: 10.5pt"><strong>&lt;echo level="warning" message="running" /&gt;</strong></div>
            <div style="text-indent: 10.5pt">&lt;java classname="org.example.antbook.lesson1.Main"</div>
            <div style="text-indent: 21pt">classpath="build/classes"&gt;</div>
            <div style="text-indent: 10.5pt">&lt;arg value="a"/&gt;</div>
            <div style="text-indent: 21pt">&lt;arg value="b"/&gt;</div>
            <div>&lt;arg file="."/&gt;</div>
            <div>&lt;/java&gt;</div>
            <div style="text-indent: 10.5pt">&lt;/target&gt;</div>
            <div>
            <p>&lt;/project&gt;</p>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/caizh2009/aggbug/278936.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/caizh2009/" target="_blank">小菜毛毛</a> 2009-05-29 19:06 <a href="http://www.blogjava.net/caizh2009/articles/278936.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>