您的位置:首页 > 其它

ant使用手册

2015-09-24 06:58 429 查看

ant安装:

下载Apache ant,然后将bin目录添加到path。临时使用可以设置临时环境变量:

Windows:

set ANT_HOME=f:\ant
set PATH=%PATH%;%ANT_HOME%\bin
Linux:

export ANT_HOME=/usr/local/ant
export PATH=${PATH}:${ANT_HOME}/bin

ant命令

执行ant:将ant文件保存为build.xml,则在当前目录输入ant即可执行build.xml。如果使用其他名字,则使用 ant 文件名 即可执行ant文件。

基本格式:

<project name="Tutorial" default="welcome" basedir=".">

<!-- Property -->
<property name= "AUTHOR" value= "Geurney"/>

<!-- welcome -->
<target name="welcome" description="Basic Information">
<echo message="Author: ${AUTHOR}"/>
</target>

<!-- greeting -->
<target name="greeting" description="Basic Information" depends="welcome">
<echo message="How are you?"/>
<input message="Press Return key to continue..."/>
</target>

</project>


其中default是指当执行ant命令时默认首先执行welcome这个target,如果想执行greeting这个target,则输入 ant greeting 。depends是指先执行depends的target,再执行本target。

输入任务和条件任务:

<!-- input task & condition task -->
<target name="input" description="Input test">
<input
message="Yes or No (y/n)?"
validargs="y,n"
addproperty="choice"
defaultvalue="y"
/>
<condition property="choiceIsNo">
<equals arg1="n" arg2="${choice}"/>
</condition>
<echo message="${choice} is ${choiceIsNo}"/>
<fail if="choiceIsNo">Build aborted by user.</fail>
</target>
1. validargs是允许的输入,addproperty是将输入生成一个新的property,defaultvalue是默认值。
2. condition的property是只有当条件满足才会生成这个property,上面的是当arg1和arg2相等时,才生成choiceIsNo这个property,其值为true。

3. fail当条件满足时,终止并退出build。

创建文件夹/拷贝文件/删除文件夹:

<property name="filepath" location="test.txt"/>
<property name="directorypath" location="testdir/"/>
<target name="filetest">
<mkdir dir="${directorypath}"/>
<copy file="${filepath}" todir="${directorypath}"/>
<copy file="${filepath}" tofile="${directorypath}/tt.txt"/>
<delete dir="${directorypath}"/>
</target>

java任务:

编译java文件:

<!-- Compile Star -->
<property name="StarJavaPath"  location="src/p2"/>
<property name="StarClassPath" location="classes2"/>
<target name="compile-star">
<javac srcdir="${StarJavaPath}" destdir="${StarClassPath}" includeantruntime="false"/>
</target>
直接使用srcdir,将该目录下的所有java文件编译。注意StarJavaPath是到java文件地址的(进入包了)。

另一种编译方式:

<!-- Compile Universe -->
<property name="UnivereJavaPath"   location="src/p1"/>
<property name="UniverseClassPath" location="classes1"/>
<property name="UniverseJarPath"   location="jar/Universe.jar"/>
<target name="compile-universe">
<javac destdir="${UniverseClassPath}" includeantruntime="false">
<src path = "${UnivereJavaPath}"/>
<include name="Universe.java"/>
<include name="Sun.java"/>
<classpath>
<pathelement location="${StarClassPath}"/>
</classpath>
</javac>

<jar destfile="${UniverseJarPath}">
<fileset dir="${UniverseClassPath}" includes="p1/*.class"/>
<fileset dir="${StarClassPath}"     includes="p2/*.class"/>
<manifest>
<attribute name="Implementation-Vendor"  value="${AUTHOR}"/>
<attribute name="Implementation-Version" value="${VERSION}"/>
<attribute name="Main-Class" value="p1.Universe"/>
</manifest>
</jar>
</target>
这里使用了src和include的组合添加java文件,可以使用多组src、include。classpath可以使用多个pathelement。jar使用了fileset和manifest。

运行universe:

<!-- Run Universe -->
<target name="run-universe">
<java jar="${UniverseJarPath}" fork="true">
<arg value="hello!"/>
</java>

<java classname="p1.Universe" fork="true">
<classpath>
<pathelement location="${UniverseJarPath}"/>
<fileset dir="jar"  includes="Universe.jar"/>
</classpath>
</java>
</target>

两种运行方式,第一种使用jar,可以使用多个arg传入参数。第二种使用classpath,既可以用pathelement也可使用fileset来指定文件。

生成javadoc:

<!-- Generate javadoc -->
<target name="javadoc" description="create javadocs">
<javadoc packagenames="p1.*, p2.*" sourcepath="src" destdir="javadocs" />
</target>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: