Ant 详解

Ant 详解

Target属性:

name: target名字

depends:用逗号分隔的当前target所依赖的target

if/unless:执行target所需要的条件

description:对这个target的一些描述

Task属性:

一个task是一段可执行的代码。task可以

		<!--在指定的类路径下是否有可用的资源-->
	  <available resource="TestTest.class">
	     <classpath refid="all.test.classes" />       
	  </available>

 有多个属性(也可以叫变量)。属性值可能包含对property的引用。这些应用会在task执行前被解析。

<target name="compile" depends="init">  
<javac srcdir="${src}" destdir="${dest}" />  </target>
 
其中的<javac srcdir="${src}" destdir="${dest}" /> 就是一个task。
 其中的 ${src} 就是对前面定义的property的引用。再执行javac 之前这些${*}会被解析。

复制文件到指定位置:

	        <copy file="1.txt" tofile="21.txt" />
		<copy file="myfile.txt" todir="build/newdir" />
		<copyfile src="test.java" dest="subdir/test.java"/>
		<copy todir="build/newdir"> 
			<fileset dir="src_dir"/>
		</copy>
		<copy todir="dest/dir"> 
		    <fileset dir="src_dir" excludes="**/*.java"/>
		</copy> 

 

 删除文件目录:

<delete file="/lib/aaa.jar"/>
<delete dir="lib"/>
<delete> 
	<fileset dir="." includes="**/*.bak"/>
</delete>

 移动文件目录:

<move todir="new/dir">
		<fileset dir="src/dir"> 
			<include name="**/*.jar"/> 
		    <exclude name="**/aaa.jar"/>
		</fileset>
</move> 

 重命名文件:

<rename src="foo.jar" dest="foo-${version}.jar"/>

简历临时文件:

<tempfile  property="temp. xml"  destDir="build"  suffix=".bak"/>

调用其他目录下的build.xml:

<ant antfile="aaa/bbb/build.xml" /> 

<ant antfile="bbb/build.xml" dir="aaa" />

<ant antfile="build.xml" dir="aaa/bbb" />

调用其他build.xml文件中的target:

<ant antfile="subproject/subbuild.xml" target="compile"/>

使用三方的一些task:首先的导入jar文件,然后通过taskdef的classname制定哪个类来处理

taskdef

    <taskdef name="if" classname="ise.antelope.tasks.IfTask" classpath="${global.dir}/../lib/atg-ant.jar" />

    <taskdef name="else" classname="ise.antelope.tasks.ElseTask" classpath="${global.dir}/../lib/atg-ant.jar" />

MacroDef——“宏”,在Ant中是非常实用的,它的作用相当于java里面的为防止代码重复而提取的公共方法。比如我现在需要将某几个目录分别进 行编译和打包,不使用MacroDef的情况下,如果有10个目录,那么我就至少要写10个类似 的<target></target>来做完对这10个目录的操作;但在使用MacroDef的情况下,你只用写一个通用 的<macrodef></macrodef>,再在其他地方调用它就可以了,既减少了代码量,又提高了工作效率。

<macrodef name="macrodef">
        <attribute name="target" />
        <sequential>
            <subant target="@{target}">
                <property name="skip.run.assembler" value="true" />
                <property name="cobertura.on" value="${cobertura.on}" />
                <property name="junit.fullsuite" value="${junit.fullsuite}" />
                <property name="sql.export.only" value="${sql.export.only}" />
                <filelist refid="module.files" />
            </subant>
        </sequential>
    </macrodef>

<filterset id="filterSet" begintoken="@" endtoken="@">
        <filter token="jdbc.url" value="${jdbc.url}" />
        <filter token="jdbc.pub.url" value="${jdbc.pub.url}" />
        <filter token="jdbc.staging.url" value="${jdbc.staging.url}" />

</filterset>

Condition:

		<target name="conditionTest">
			<condition property="term">
				<!--set the property value to true or false -->
				<!--istrue value="true" term=true-->
				<!--istrue value="false" term=false-->
				<!--isfalse value="true" term=false-->
				<!--isfalse value="false" term=true-->
				<isfalse value="true" />
			</condition>
			 <!-- 调用isTrue和isFalse这两个target-->  
			    <antcall target="isTrue">  
			    </antcall>  
			    <antcall target="isFalse">  
			    </antcall>  
		</target>
		<!-- if条件判断,成立执行-->  
		<target name="isTrue" if="term">  
		    <echo>is ture</echo>  
		</target>  
		<!-- unless条件判断,不成立时执行-->  
		<target name="isFalse" unless="term">  
		    <echo>is false</echo>  
		</target>  
             <condition property="term">
                <not>
                    <!--逻辑非,如果istrue 为true 那么 term 为false-->
                    <istrue value="true" />
                </not>
            </condition>
 <condition property="scondition">  
        <or>  
            <istrue value="true" />  
            <istrue value="false" />  
        </or>  
    </condition>  
		<!--在指定的类路径下是否有可用的资源-->
	  <available resource="TestTest.class">
	     <classpath refid="all.test.classes" />       
	  </available>
 <condition property="scondition">  
        <!--如果属性name不存在则返回false-->  
        <isset property="name"/>  
    </condition>  
	<condition property="scondition">
		<!--如果arg1的值与arg2的值相等返回true,否则为false-->
		<equals arg1="${prop1}" arg2="${prop2}"/>
	</condition>

判断操作系统 与 执行脚本 :

<os family="unix" />

<condition property="isWindows">
        <os family="windows" />
</condition>

如果操作系统是unix执行以下脚本

<exec executable="${app.path}/${app.name}/control/set_templates.sh" osfamily="unix" failonerror="yes" />

猜你喜欢

转载自nicky19870612.iteye.com/blog/2186282
ANT