ant使用手册

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值