ant 常用方法


1. property 任务:

用法:<property file="${basedir}/comversion.properties" /> 在构建文件中引入属性文件中的属性,以后直接用${key} 即可。

2. ant 任务:

用法:

<ant antfile="./${xpd.01.path}/buildcomponent.xml">   调用buildcomponent.xml 文件同的默认目标。
     <property name="fe.path" value="${xpd.01.path}" />      传递属性给目标
     <property name="fe.version" value="${xpd.01.version}" />
</ant>

3. delete 任务:删除文件和路径

用法:<delete dir="${fe.path}/../pl" /> 删除pl路径

4 foreach 任务:循环功能,包含在ant的扩展包中

声明:<taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" /> 在build文件中引入foreach task。

注:因为foreach 任务(task) 不是ant 核心包提供的,所以应该下载foreach的类包,添加到ant 类路径,并在build 文件中声明该任务。上面的taskdef 任务就完成了foreach的声明。

用法:<foreach target="fe.source" param="fe.source.path" list="${fe.plugin.path}" delimiter=";">

循环调用fe.source目标,循环的变量为fe.source.path,功能为:把list的值用delimiter分割,并把值循环赋值给param。

例子:

<target name="aaa">
    <echo>${letter}</echo>
</target>
<taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="${lib.dir}/ant-contrib-1.0b3.jar" />
<target name="fortest">
    <foreach target="aaa" list="1,2,3,4" delimiter="," param="letter">
    </foreach>
</target>

输出结果:

fortest:
 aaa:
      [echo] 1
 aaa:
      [echo] 2
 aaa:
      [echo] 3
 aaa:
      [echo] 4

5. if 任务:程序分支的作用

声明:<taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" /> 在build文件中引入if task。

用法:

<if>
    <and>判断条件1
    <equals arg1="${src.exit}" arg2="true" />
    <equals arg1="${MANIFEST.exit}" arg2="true" />
    </and>
<then>                满足判断条件1执行
    <antcall target="jartarget">
    <param name="pluginroot" value="${pluginpath}" />
    </antcall>
</then>
<elseif>               不满足判断条件1
    <not>          判断条件2
    <equals arg1="${MANIFEST.exit}" arg2="true" />
    </not>
    <then>        满足判断条件2
    <echo message="No MANIFEST.MF file" />
    </then>
</elseif>
<else>                 其他情况
    <echo message="No Src directory" />
</else>
</if>

6. propertyregex 任务

声明:

用法:

<propertyregex property="fe.name" input="${pluginsourcepath}" regexp="/.*" replace="" />
完成属性的替换,即:把pluginsourcepath中的以"." 开始的部分,替换成" "。

7. manifest 任务

用法:

<manifest file="${pluginroot.temp}/META-INF/MANIFEST.MF" mode="update">
    <attribute name="Bundle-SymbolicName" value="${pluginid}.source" />
    <attribute name="Eclipse-SourceBundle" value='${pluginid};version="${allversion}";roots:="."' />
    <attribute name="Bundle-Version" value="${allversion}" />
    <attribute name="Bundle-Name" value="non" />
    <attribute name="Bundle-ManifestVersion" value="2" />
</manifest>

8. jar 任务

用法:

<jar destfile="${release.plugin.path}/${pluginid}.source_${allversion}.jar" manifest="${pluginroot.temp}/META-INF/MANIFEST.MF">
    <fileset dir="${pluginroot}/src" />
    <!-- <fileset dir="${pluginroot}/schema" /> -->
    <fileset dir="${pluginroot}" includes="schema/**" />
</jar>

9. available 任务

用法: 当${pluginpath}/src/ 路径存在时,src.exit 将被赋值为真。

10. replace 任务

用法:把文件feature.xml 中包含6.2.1.qualifier 字符串替换为6.2.1.${plugindateversion}。

11. 打源jar包

<?xml version="1.0" encoding="UTF-8"?>  
<project name="demo-project" default="jar" basedir="." >  
    <property name="src.dir" value="src"/>  
    <property name="target.dir" value="target"/>    
    <!--清理任务-->  
    <target name="clean">  
        <delete dir="target">  
        </delete>  
    </target>        
    <!--创建目录-->  
    <target name="init" depends="clean">  
        <mkdir dir="target"/>  
    </target>  

    <!--将源代码打成jar包的形式-->  
    <target name="jar" depends="init">  
        <jar jarfile="${target.dir}/demo-project-src.jar" basedir="${src.dir}">  
            <include name="**/*.java"/><!--只将java文件打包-->  
        </jar>  
    </target>  
</project>  

12. 发邮件

ant 发邮件:

<target name="sendmail" description="aa">
     <mail mailhost="smtp.ylzinfo.com" user="***@ylzinfo.com" password="*****" subject="jmeterTest" mailport="25">
         <from address="***@ylzinfo.com" />
         <to address="***@ylzinfo.com" />
         <message>This is autoPerfTest results(without apache)</message>
         <attachments>
             <fileset dir="${test.results.file}">
                 <include name="*.html" />
             </fileset>
         </attachments>
     </mail>
</target>

运行的时候可能会报:Failed to initialise MIME mail: javax/mail/MessagingException,这时要加入两个jar包到lib下:mail.jar , activation.jar

但是运行时还是会报错,因为运行mail的时候不能加classpath它不读不到这两个jar包,所以要把这两个jar包加到ant home里面(eclipse):

windows-->Preference-->Ant-->Runtime,在它的ant home里面加入以上两个jar包就OK了

13. try catch

1.Copy ant-contrib-0.3.jar to the lib directory of your Ant installation. If you want to use one of the tasks in your own project, add the lines to your build file.

2.Keep ant-contrib-0.3.jar in a separate location. You now have to tell Ant explicitly where to find it (say in /usr/share/java/lib):

3.实例:

<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${lib.dir}/ant-contrib-1.0b3.jar"/>
<target name="testOOOO">
    <trycatch>
        <try>
            <antcall target="compile.selenium.tests" />
            <echo message="----success----"/>
        </try>
        <catch>
            <echo message="-----false-----"/>
        </catch>
    </trycatch>
</target>

14. Task_Parallel task

Parallel task

Parallel非常有用,比如我想一边开tomcat,一边做别的,就需要使用它,否则就只有用spawn=true属性把tomcat放在后台运行。spawn有很多不好的地方,比如不能即时在console看到信息,停止ant运行不能把tomcat关掉等。

Parallel相当于一个容器,放在里面的每个task都会被并行执行。如果想把某几个task顺序执行,用相当于()的Sequential task 包起来。

Waitfor task

暂停ant执行直到条件符合,比如就会等待tomcat启动后才会继续往下执行。

ant for循环

For

The for task iterates over a list, a list of paths, or any type that has a public iterator() method. The list will be evaluated first. Nested paths are evaluated in the order they appear in the task. Nested types will then be evalulated.

This task is the same as the task, except that

•it uses a nested sequential for each iteration; and
•it implements an additional "keepgoing" attribute.

makes use of ant's macrodef task, so the @{} notation is used for parameter substition.

This task only works for ant version greater than or equal to ant 1.6.0.

Parameters

Attribute Description Required
list The list of values to process, with the delimiter character, indicated by the "delimiter" attribute, separating each value. Yes, unless a nested path has been specified.
param Name of the parameter to pass the tokens or files in as to the sequential. Yes
delimiter The delimiter characters that separates the values in the "list" attribute. Each character in the supplied string can act as a delimiter. This follows the semantics of the StringTokenizer class. No, defaults to ",".
parallel If true, all iterations of the nested <sequential> will execute in parallel. Defaults to false, which forces sequential execution of the iterations. It is up to the caller to ensure that parallel execution is safe. No
keepgoing If true, all iterations of the called <sequential> will be executed, even if a task in one or more of them fails. Defaults to false, which forces execution to stop as soon as a task fails. At the end, if any iterator has failed, the <for> task will fail, otherwise <for> will succeed.

Note that execution does not proceed keepgoing from one task to the next within the <sequential>, but rather from one iteration to the next.

It is up to the caller to ensure that keepgoing execution is safe.

No
threadCount The maximum number of allowable threads when executing in parallel. No. Defaults to 5.
trim If true, any leading or trailing whitespace will be removed from the list item before it is passed to the sequential. No. Defaults to false.

Parameters specified as nested elements

path

Paths are used to select sets of files or directories to iterate over.

Using a path allows you to determine the order by which files are considered by using filelists or explicit pathelements. You also can specify whether you want to iterate over files or directories by chosing either filesets or dirsets.

fileset

FileSets are used to select sets of files to iterate over.

dirset

DirSets are used to select sets of directories to iterate over.

sequential

This is the list of tasks to be run for each iteration of the list.

Example

To print out the first five letters of the latin alphabet:

<echo message="The first five letters of the alphabet are:"/>
<for list="a,b,c,d,e" param="letter">
  <sequential>
    <echo>Letter @{letter}</echo>
  </sequential>
</for>

A more complicated example to to iterate over a set of c++ source files and invoke the task on them:

<for param="file">
  <path>
    <fileset dir="${test.dir}/mains" includes="*.cpp"/>
  </path>
  <sequential>
    <propertyregex override="yes"
      property="program"  input="@{file}"
      regexp=".*/([^\.]*)\.cpp" replace="\1"/>
    <mkdir dir="${obj.dir}/${program}"/>
    <mkdir dir="${build.bin.dir}"/>
    <cc link="executable" objdir="${obj.dir}/${program}"
        outfile="${build.bin.dir}/${program}">
      <compiler refid="compiler.options"/>
      <fileset file="@{file}"/>
      <linker refid="linker-libs"/>
    </cc>
  </sequential>
</for>

The preceding example will stop as soon as one of the tasks fails. If you change the first line of the example to

<for param="file" keepgoing="true">

All iterations will be executed, and then will fail if any one or more of the tasks failed.

The following example embeds an outofdate type and iterates over the sources that are newer than their corresponding targets.

<ac:for param="xmlfile" xmlns:ac="antlib:net.sf.antcontrib">
  <ac:outofdate>
    <ac:sourcefiles>
      <ac:fileset dir="${basedir}/xdocs" includes="**/*.xml"/>
    </ac:sourcefiles>
    <ac:mapper dir="${basedir}/xdocs"
               type="glob" from="*.xml" to="${basedir}/docs/*.html"/>
  </ac:outofdate>
  <ac:sequential>
    <echo>Need to generate a target html file from source file @{xmlfile}</echo>
  </ac:sequential>
</ac:for>
Joyce /
Published under (CC) BY-NC-SA in categories ant  tagged with ant