??xml version="1.0" encoding="utf-8" standalone="yes"?>成人avav在线,欧美日韩国产精品专区,国产在线一二http://www.aygfsteel.com/caizh2009/category/39893.html与大家共同成?/description>zh-cnWed, 03 Jun 2009 07:38:23 GMTWed, 03 Jun 2009 07:38:23 GMT60ant入门教程Q一QHello World with AntQ英文教E文翻译)Q连载)http://www.aygfsteel.com/caizh2009/articles/279775.html菜毛毛菜毛毛Wed, 03 Jun 2009 04:01:00 GMThttp://www.aygfsteel.com/caizh2009/articles/279775.htmlhttp://www.aygfsteel.com/caizh2009/comments/279775.htmlhttp://www.aygfsteel.com/caizh2009/articles/279775.html#Feedback0http://www.aygfsteel.com/caizh2009/comments/commentRss/279775.htmlhttp://www.aygfsteel.com/caizh2009/services/trackbacks/279775.html 只能够翻译个大概意思,希望朋友们别见?..

教程: Hello World with Ant

本文提供了一步一步用ANT开始JAVA目构徏的教E?不包括更深入了解JAVA与ANTQ本教程的目的是Z让你看,使用ANT最单的步骤?/h1>

目的准备:

 我们希望从不同的来源所产生的文Ӟ因此我们的Java源文件将?/tt>文g夹中?所有生成的文g应根?tt> Qƈ分裂成数个子目录个h步骤Q??/tt>我们汇编文g?/tt> JAR为我们自q白佳文g?br />  

我们希望从所产生的文件来分离源文Ӟ因此我们的JAVA源文件将?tt>src folder。所有生成的文g应根据构建方式,qؓ单独的步骤分成数个子目录Qclasses是我们编译的文gQjar是我们JAR包文件?br />
我们必须创徏一个唯一的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>

在完成仅用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 会是:

<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了解。。?



菜毛毛 2009-06-03 12:01 发表评论
]]>Ant文译(q蝲)http://www.aygfsteel.com/caizh2009/articles/279701.html菜毛毛菜毛毛Tue, 02 Jun 2009 13:41:00 GMThttp://www.aygfsteel.com/caizh2009/articles/279701.htmlhttp://www.aygfsteel.com/caizh2009/comments/279701.htmlhttp://www.aygfsteel.com/caizh2009/articles/279701.html#Feedback0http://www.aygfsteel.com/caizh2009/comments/commentRss/279701.htmlhttp://www.aygfsteel.com/caizh2009/services/trackbacks/279701.html很想?span class="hilite1">Ant好好的学习一下,最好的学习Ҏ是看官Ҏ档,所以决定把原版英文文译一下,如果有翻译不对的地方Q请大家指出来,谢谢

英文部䆾没有译的部?/span>

文原始地址Q?/span>http://ant.apache.org/manual/index.html

参考资料:孙鑫老师的JAVA WEB开发详?/span> 

Using Ant

单的构徏文gQ?/a>

ant的构建文件是xml格式的,每一个构建文件包含一个project和至一?~省)targetQ目标包含Q?task)元素Q构建文仉的每一个Q务元素(taskQ可以指定一个id元素Q可以用它的值来引用q个taskQ这个值是唯一的,

Q更多说明,L下面?TasksQ?/p>

ProjectsQ工E)

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>

Targets

一个目标可以依赖其它的目标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.

Tasks

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.

Properties

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- and CLASSPATH-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 the path attribute accepts colon- or semicolon-separated lists of locations. The path attribute is intended to be used with predefined paths - in any other case, multiple elements with location attributes should be preferred.

As a shortcut, the <classpath> tag supports path and location 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 the lib directory, the classes directory, all directories named classes under the apps subdirectory of ${build.dir}, except those that have the text Test 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.

Examples

<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.

References

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.

Use of external tasks

Ant supports a plugin mechanism for using third party tasks. For using them you have to do two steps:

  1. place their implementation somewhere where Ant can find them
  2. declare them.

Don't add anything to the CLASSPATH environment variable - this is often the reason for very obscure errors. Use Ant's own mechanisms for adding libraries:

  • via command line argument -lib
  • adding to ${user.home}/.ant/lib
  • adding to ${ant.home}/lib

For the declaration there are several ways:

  • declare a single task per using instruction using <taskdef name="taskname" classname="ImplementationClass"/>
    <taskdef name="for" classname="net.sf.antcontrib.logic.For" /> <for ... />
  • declare a bundle of tasks using a properties-file holding these taskname-ImplementationClass-pairs and <taskdef>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" /> <for ... />
  • declare a bundle of tasks using a xml-file holding these taskname-ImplementationClass-pairs and <taskdef>
    <taskdef resource="net/sf/antcontrib/antlib.xml" /> <for ... />
  • declare a bundle of tasks using a xml-file named antlib.xml, XML-namespace and antlib: protocoll handler
    <project xmlns:ac="antlib:net.sf.antconrib"/> <ac:for ... />

If you need a special function, you should

  1. have a look at this manual, because Ant provides lot of tasks
  2. have a look at the external task page in the manual (or better online)
  3. have a look at the external task wiki page
  4. ask on the Ant user list
  5. implement (and share) your own

 

Token Filters

 

Example Buildfile



菜毛毛 2009-06-02 21:41 发表评论
]]>
WHAT IS ANThttp://www.aygfsteel.com/caizh2009/articles/278935.html菜毛毛菜毛毛Fri, 29 May 2009 11:06:00 GMThttp://www.aygfsteel.com/caizh2009/articles/278935.htmlhttp://www.aygfsteel.com/caizh2009/comments/278935.htmlhttp://www.aygfsteel.com/caizh2009/articles/278935.html#Feedback0http://www.aygfsteel.com/caizh2009/comments/commentRss/278935.htmlhttp://www.aygfsteel.com/caizh2009/services/trackbacks/278935.htmlANT介绍 Q? Ant? Ant是一个类似make的工P用来~译/q行/试javaE序? 构徏、包装和发布q程中几乎每一件事都可以由Ant的Q务来处理. 二.Ant的安装及配置 你可以从Jakata|站下蝲预编译的ANT,解压至Q一目录Q? 讄自己的环境变量,即ANT_HOMEQ指向解压目? 讄JAVA_HOME环境变量,指向jdk的根目录; 三:用ant理目 Ant一启动׃自动地加载一个叫做Build.xml的项目配|文件。如果想l这个项目配|文件取别的名字Q你可以使用buildfile标记来运行antQ就像下面这P ant -buildfile Project_configuration.xml 主要特点Q? 使用xml文g作ؓ配置文gQ即build.xmlQ? 可与junit作无~整合,辑ֈpȝ试、编译到发布的功能; 强大、详l的报表功能Q? 控制灉|Q可自定义要执行的Q务组合? build.xml主要节点Q? proejct 目 name 指定工程名称 default 指定默认的targetQ即dQ? basedir 指定pȝ的基本\? property 属性,cM于全局变量 name 变量名称 value 变量? 属性访问方法: ${property} 内置属性: basedir 工程的\? ant.file build文g的\? ant.version 版本 ant.project.name 工程? ant.java.version jvm版本 target 目标Q即d name 指定目标名称 depends 指定所依整的目? if 条gQ可辑ֈ控制程目的 unless description task 一段的Q? reference 引用 通过refid属性来引用在其他节中定义的id 内置命oQ? <tstamp/> 建立旉 <mkdir dir="${var}"/> 建立目录 ~译文gQ? <javac srcdir="${src}" destdir="${classes}" debug="off"> <classpath refid="appclasspath"/> <include name="**/*.java" /> </javac> 执行文gQ? <exec executable="${base.dir}/email.bat" > </exec> junit命oQ? <junit> <classpath refid="appclasspath"/> <classpath> <pathelement location="${base.dir}/defaultroot/WEB-INF/classes"/> </classpath> <formatter type="xml"/> <test name="junit.tests.AllTests" haltonfailure="no" outfile="result"/> </junit> <junitreport todir="./report"> <fileset dir="."> <include name="result.xml"/> </fileset> <report format="noframes" todir="./report"/> </junitreport> 四:q行ant ant [options] [target [target2 [target3] ...]] Options: -logfile <file> use given file for log -l <file> '' -buildfile <file> use given buildfile -file <file> '' -f <file> '' -D<property>=<value> use value for given property -propertyfile <name> load all properties from file with -D properties taking precedence

菜毛毛 2009-05-29 19:06 发表评论
]]>
Ant入门教程http://www.aygfsteel.com/caizh2009/articles/278936.html菜毛毛菜毛毛Fri, 29 May 2009 11:06:00 GMThttp://www.aygfsteel.com/caizh2009/articles/278936.htmlhttp://www.aygfsteel.com/caizh2009/comments/278936.htmlhttp://www.aygfsteel.com/caizh2009/articles/278936.html#Feedback0http://www.aygfsteel.com/caizh2009/comments/commentRss/278936.htmlhttp://www.aygfsteel.com/caizh2009/services/trackbacks/278936.html
本内容包含了Ant的历史简要介l,Ant的功能以及Ant框架的介l,q对下蝲安装使用Antq行了示例介l,同时通过一个JavaE序讲解了Ant的基本用方法?br />

1.       Ant介:q里引用Ant帮助文档中对Ant的介l:

     Apache Ant是一个基于Java的构建工兗从理论上讲Q也是一U类gMake的工P只是去除了Make工具的缺炏V?/div>
    
      既然已经有了make, gnumake, nmake, jam以及其他的构件工PZ么还要Ant呢?因ؓAnt的早期开发者发现所有以上这些工具都或多或少的有一些局限性,使得在跨q_开发Y件成为困难。类gMake的工具都是传l的ZShell?-首先q行依赖性检查,然后执行命o。这意味着你可以轻易的通过使用或者编写程序来扩展q些工具Q以满不同的^台。当Ӟq也意味着你将局限于特定的^収ͼ臛_可以说局限于特定cd的^収ͼ例如QUnixq_?br />                                                                                       

    同时QMake文g也有一些先天的~陷。好多h都会遇到恐怖的tab问题。Ant的最初开发者多ơ说“我的命o不能执行因ؓ我在tab前面加了一个空|”。一些工具如Jam一定程序上解决了这个问题,但仍有其它的格式问题?br />

    Ant与从Z命o的那些扩展开来的那些工具不同QAnt是由javacL展的。不用编写shell命oQ而是配置ZXML的文Ӟ形成多个d的目标配|树。每一个Q务都是通过一个实C一个规定接口的javacLq行的?/div>
    ant~少了一些直接执行shell命o的能力,如find . -name foo -exec rm {}Q但它给用户提供了跨q_的能力,可以在Q何地方工作。实际上QAnt也提供了命oexecute用来执行shell命o,q就是它的Q务,它允许执行基于操作系l的命o?br />

    单的_Ant是一个基于JavaQƈ且主要用于Java工程的构建工兗Ant本意是Another Neat Tool,也就是另一U整z的工具Q取首字W就是Ant?br />

构徏工具是Z减少重复工作而生的?br />

2.       Ant的一些核心概念:
XML:构徏文g是以XML文g来描q的Q采?/span>XML格式有很多好处。这里就不一一列D?/span>
陈述式语法:构徏文g短小_悍Q且易于理解?/div>
每个构徏文g包含一个工E?/span>(project)?/span>
每个工程包含若干个目?/span>(target)?/span>
目标可以依赖于其他的目标(depends)?/span>
目标包含d(task)?/span>
易于使用Java语言增加新的d---易于扩展Q自定义Q?/span>
3.       Antl构Q?/span>
Ant的结构如下图所C:


构徏文g的概念视图:工程包含一个目标的集合。在每个目标里是d的声明,它们是对
Ant用于构徏该目标的行ؓ说明。目标生成一个依赖关pd表来声明该目标的依赖关系。当执行一个目标时Q必d执行它们依赖的目标?/span>
 
例子Q一个典型的构徏文gQ?/span>
<?xml version="1.0" ?>
<project name="OurProject" default="deploy">
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init" >
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="doc" depends="init" >
<javadoc destdir="build/classes" sourcepath="src" packagenames="org.*" />
</target>
<target name="deploy" depends="compile,doc" >
    <jar destfile="dist/project.jar" basedir="build/classes"/>
         <ftp server="" userid="" password="">
     <fileset dir="dist"/>
</ftp>
</target>
</project>
 
该构E如下:
pȝ初始?、编?nbsp; 2、生成JAVADOC   4、打?nbsp; 5、上传到FTPQ其中后两步l合C起叫部v?/div>
 
执行时输出如下:
> ant -propertyfile ftp.properties
Buildfile: build.xml
init:
[mkdir] Created dir: /home/ant/Projects/OurProject/build/classes
[mkdir] Created dir: /home/ant/Projects/OurProject/dist
compile:
[javac] Compiling 1 source file to /home/ant/Projects/OurProject/build/
classes
doc:
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package org.example.antbook.lesson1...
[javadoc] Constructing Javadoc information...
[javadoc] Building tree for all the packages and classes...
[javadoc] Building index for all the packages and classes...
[javadoc] Building index for all classes...
deploy:
[jar] Building jar: /home/ant/Projects/OurProject/dist/project.jar
[ftp] sending files
[ftp] 1 files sent
BUILD SUCCESSFUL
Total time: 5 seconds.

在执行时使用命o行参C传入一个属性文Ӟ属性文件中包含q接FTP服务器用的服务器名Q用户名Q用户密码来l?span style="color: red">Ҏ用?/span>
q个例子很好的展CZAnt的一些基本要素:目标依赖、特性的使用、编译、文生成、JAR打包Qtar,Zip,WAR,EAR{)Q最后是部v?/div>
 
Ant的简单Q?<mkdir>)都是由Javacd来实现相应的功能。而一些复杂的d<ftp>?lt;junit>q需要第三方库的支持?/div>
 
    Ant的一个强大之处:它总能工作。只要正的指定构徏文gQAntp计算出目标的依赖性,q且按照正确的顺序调用目标。目标通过d按序执行Q而Q务自w处理其文g依赖性以及实际的操作来完成工作。因为每个Q务通常都是在高层陈qͼ所以一两行XML语句l常已l够描qCQ务的内容?/div>
 
4.   下蝲q安?/strong>Ant
     使用Ant前提条gQ系l中已经安装JDK以及Ant。在文~写之时QAnt的最新版本是Ant 1.7,但是ZE_性,本文档用版本ؓAnt 1.6.5.
首先下蝲AntQ到apache软g|站http://www.apache.org/?/div>
其次Q解压羃文gQ放到指定的pȝ目录中,例如C:\Ant?/div>
再次Q将其添加到pathQ以便从命o行用?一些IDEQ例如Eclipse可以不需要设|path,而通过IDE相关讄Antd到path中?
再次Q设|一些环境变量指向JDK以及ANT?/div>
最后,d需要的可选库?/div>
 
在Windows安装q程Q以W者的安装q程ZQ?/div>
    下蝲apache-ant-1.6.5-bin.zip到本地硬盘,解压~之后将文g夹命名ؓAnt,攑֜C:\Ant中。这个目录就是Antȝ录?/div>
应该主目录中的bin目录d到path属性中Q这样就可以在命令行中调用ant命oQANT_HOME是批处理文g所在目录的上目录。最好明设定?/div>
现在许多工具已经集成了特定版本的AntQ一些操作系l甚至默认的已经安装?/span>Ant。所以,你的pȝ中可能已l安装了Ant?/span>
首先可以通过q行以下命oQ?br /> ant -version
?br /> ant -diagnostics
来确定。我们推荐您不设|CLASSPATH来运行Ant命o。如果Q何版本的Ant可以从CLASSPATH加蝲 Q这时就会由于加载了不兼容的c而生许多错误?/div>
 
一些其他问题请参阅Ant的FAQ讄?/div>
正常情况下,执行ant ?Cversion卛_昄Ant版本Q则说明安装配制成功Q?/div>

 
5.       q行W一个构建文Ӟ
首先创徏一个Java工程Q名为AntProjectQ工E中源文件和目标文g是分开的,分别为文件夹src和bin,然后创徏一个JavacLӞcd?/div>
com.neusoft.test.AntTestQ只是ؓ了测试,所以类的内容很单:
package com.neusoft.test;
/**
 *This is just a test class.
 */
public class AntTest{
      public static void main(String[] args){
           for(int i=0;i<args.length;i++){
                 System.out.println(args[i]);
           }
      }
}
 
然后我们在工E的路径下面建立一个构建文?/strong>build.xmlQ内容如下:
<?xml version="1.0" ?>
<project name="structured" default="archive" >
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init" >
  <javac srcdir="src" destdir="build/classes"/>
</target>
<target name="archive" depends="compile" >
<jar destfile="dist/project.jar"
basedir="build/classes" />
</target>
<target name="clean" depends="init">
<delete dir="build" />
<delete dir="dist" />
</target>
</project>
构徏文g说明如下图:


关于XML的知识,请参考其他书c,q里不做介绍?/div>
以上创徏完成后,目录l构如下图:


 
     Ant构徏文gL有一?lt;project>元素做ؓ根元素,它有两个属性,name和defaultQ?lt;target>元素?lt;project>元素的子元素Q可以有多个Q它有两个属性,name和dependsQ?lt;target>元素包含的元素就是一些Q务元素?br />
<target>可以由命令行q行昄的调用,也可以在内部使用如可以直接调用ant init、ant compile{。如果不写参敎ͼ则默认的build文g是build.xmlQ默认的目标?lt;project>的default属性定义的目标。目标的名称是唯一的,可以是Q意字W串?/div>
 
下面我们先运行一下这个Ant构徏Q再讲解其他的内容,q入工程目录Q执?/div>
ant
q里q当于执行默认的目标,也就?lt;project name="structured" default="archive" >中的archive目标?/div>

 
q里说明了首先初始化创徏两个目录Q然后编译了一个JAVA文gQ然后进行了打包的操作?/div>
 
q里讲解一下如果构建失败了怎么办?
首先有可能是XML语法书写不正??lt;target>写成<targe>)Q或者在d执行q程中出C错误(.java文g中包含编译错?Q或者Q务名UC写错??lt;javac>写成<javacc>){等Q这些都不是Ant的错误,不需要填写Bug Report。写XML时一定要l心Q一些IDE已经有验证功能,可以很好的防止书写的错误?/div>
 
出现错误Ӟ可以使用
ant ?Cverbose
或?/div>
ant ?Cdebug来获取更加详l的构徏信息Q以解决问题?/div>
下图是用ant ?Cverbose时的输出Q用ant ?Cdebug获取比q更详细的信息,q里׃举例了?/div>

 
本例中直接用了软g工程中的构徏l构Q用src作ؓ源文件目录,build/class作ؓ中间生成文gQ以dist作ؓ可发布文件。在最后把一些可执行文g可以攑֜bin目录中。此时目录结构如下图所C:

 
我们需要一U办法来定某些d先执行,而有些Q务后执行Q比如必d~译Q才能执行程序或者打包。我们在声明目标的时候,在其依赖属性中列出其依赖关p:
<target name="compile" depends="init" >
<target name="archive" depends="compile" >
<target name="clean" depends="init">
如果一个目标依赖与多个其他目标Q需要将它们都写C赖属性中Q例如:
depents=”compile,test”。在我们的构ZQarchive依赖于init和compileQ但是我们不需要去写,因ؓcompile已经依赖于init了。即QAnt的依赖关pL传递的Q但不是自反的?/div>


如果在执行过E中两个目标׃n同一个目标,则先导目标只被执行一ơ?/div>
可以通过指定目标来运行构建:
例如执行完ant后,可以执行ant clean来清理构建:

 
ant{h于ant archive
ant init
ant clean
ant compile
ant archive
ant clean archive
 
当构建完成一ơ以后,再次执行构徏会发生什么呢Q?/strong>

 
W二ơ执行构建时只花?sQ相比第一ơ的4s。ƈ且没有Q何一个目标表C做了Q何工作?/div>
原因如下Q所有的d都检查了它们的依赖关p:
<mkdir>没有创徏目录因ؓ已经存在
<javac>比较了源文g和类文g的时间戳
<jar>比较了要被加入文件与已经存在文g的时?/div>
只有更新的时候才q行d执行?/div>
 
Ant如何处理命o行上的多个目标?
执行ant compile archive会怎么P
先实验一下:

 
Ant依次执行每个目标和其依赖目标Q即Ant的执行顺序是init compile init compile archiveQ虽然这Lh增加了额外的工作Q但是通过上面的执行过E就会发玎ͼ׃其依赖性检查的LQ第二次的init和compileq未真正的执行,执行旉与直接执行archive的时间是一L?/div>
 
q行E序Q?/strong>
普通执行该cȝҎ是:
java ?Ccp build/class com.neusoft.test.AntTest args1 args2
而我们用Ant的Q务来执行它仅仅需要增加一个Q务,好处在于Q?/div>
让用于执行的目标依赖与编译的目标Q确保运行最新版?/div>
易于传递复杂参?/div>
讄classpath更方?/div>
在Ant自n的JVM中运行,载入更快
增加一个新的目标:
<target name="execute" depends="compile">
<java classname="com.neusoft.test.AntTest"
classpath="build/classes">
<arg value="a"/>
<arg value="b"/>
<arg file="."/>
</java>
</target>
 
最后一个参数是file=”.”Q表CZ入的参数是一个目录,为文件绝对\径?/div>
q行该目标,输出如下Q?/div>

 
Ant命o行选项Q?/div>
 
请参阅相x册进行查询相关选项的功能?/div>
 
当有多个构徏文gӞ可以指定构徏文gQ?/strong>
ant ?Cbuildfile build.xml compile
来表C执行build.xmlq个构徏文g中的compile目标?/div>
 
控制提供的信息量Q?/div>
ant ?Cquiet:安静模式Q不l出M输出?/div>

 
ant ?Cemacs:单模式,不显CZQ务名U?/div>

 
ant ?Cprojecthelp:获取目信息?/div>

 
最l的构徏文gQ添加了description属性?/strong>
<?xml version="1.0" ?>
<project name="secondbuild" default="execute" >
<description>Compiles and runs a simple program</description>
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init"
description="Compiles the source code">
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="archive" depends="compile"
description="Creates the JAR file">
<jar destfile="dist/project.jar" basedir="build/classes"/>
</target>
<target name="clean" depends="init"
description="Removes the temporary directories used">
<delete dir="build" />
<delete dir="dist" />
</target>
<target name="execute" depends="compile"
description="Runs the program">
<echo level="warning" message="running" />
<java classname="org.example.antbook.lesson1.Main"
classpath="build/classes">
<arg value="a"/>
<arg value="b"/>
<arg file="."/>
</java>
</target>

</project>



菜毛毛 2009-05-29 19:06 发表评论
]]> վ֩ģ壺 | | | ߺ| | ɽ| | | ɽ| | | | ³| | | | | | | | | ʯ| Т| | ǰ| | | ˶| γ| ȳ| ӱ| | ̳| | ¬| ƽ| ƽ| ʡ| | ӽ| կ|