Ant -----ant標簽和自定義任務


隨便記一下 Ant的用法吧。ant ,maven, gradle ,三個打包工具到齊了,Ant 常見標簽解析,ant 自定義task 。

<?xml version="1.0" encoding="UTF-8"?>
<project name="pase2" default="allElements">
<property environment="env" />

<!-- ===================================================================== -->
<!-- Run a given ${target} on all elements being built -->
<!-- Add on <ant> task for each top level element being built. -->
<!-- ===================================================================== -->
<available property="allElementsFile" file="${builder}/allElements.xml" value="${builder}/allElements.xml"/>
<property name="allElementsFile" location="${eclipse.pdebuild.templates}/headless-build/allElements.xml"/>

<import file="${allElementsFile}" />
<target name="allElements">
<antcall target="allElementsDelegator" />
</target>
<target name="getBaseComponents" unless="skipBase">
<get src="${eclipseBaseURL}" dest="${buildDirectory}/../temp-base.zip" />
<unzip dest="${base}" overwrite="true" src="${buildDirectory}/../temp-base.zip" />
</target>
<!--文件打包-->
<target name="local-zip">
<zip destfile="${local_home}/目標文件夾名.zip">
<zipfileset dir="${local_home}/myproject/"/>
</zip>
</target>
<!--path標簽-->
<path id="build.classpath.jar">
<pathelement path="${env.J2EE_HOME}/${j2ee.jar}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<!--get標簽 get一組文件-->
<get usetimestamp="true" verbose="true" dest="${antfile.dir}/36ria-index.html" src="http://www.36ria.com/"></get>
<get dest="downloads">
<url url="http://ant.apache.org/index.html"/>
<url url="http://ant.apache.org/faq.html"/>
</get>
<!--touch 文件 -->
<touch file="myfile"></touch>
<!--指定touch 時間-->
<touch datetime="18/10/2010 2:02 pm" file="myfile"></touch>
<!--文件中字符串替換-->
<replace file="configure.sh" token="token" value="value"/>
<property name="a" value="aaa" />

<!--
<property name="b" value="bbb" />
-->
<!--condition的用法-->
<!-- 如果設置了屬性b則值為${b},否則值為${a}-->
<condition property="val" value="${b}" else="${a}">
<!-- 判斷是否設置了指定屬性 -->
<isset property="b" />
</condition>
<target name="clean" unless="noclean">
<antcall target="allElements">
<param name="target" value="cleanElement" />
</antcall>
</target>
<!--ant 刪除任務-->
<delete file="../tmpcopy/filter.txt" />
<delete dir="../tmpcopy/afterfilter" />
<delete includeEmptyDirs="true" failonerror="failonerror">
<fileset dir="../tmpcopy/new"/>
</delete>
<cvs cvsRoot="${mapsRepo}" package="${mapsRoot}" dest="${buildDirectory}/maps" tag="${mapsCheckoutTag}" />
<!--available 驗證文件或者目錄的標簽-->
<available property="is" type="file" file="${ui}"></available>
<!--ant 編譯任務-->
<javac
nowarn="on"
source="1.6"
target="1.6"
deprecation="true"
debug="true" encoding="UTF-8"
srcdir="${base.src.dir}"
destdir="${classes.dir}"
classpathref="lib.class.path" >
</javac>
<!--ant svn 操作-->
<svn>
<delete>
<fileset dir="workingcopy/deleteTest">
<include name="**/*.del"/> </fileset>
</delete>
<commit message="commit deleted files" dir="workingcopy/deleteTest"/>
</svn>
<!--自定義task-->
</project>

大概就那么幾個標簽,不常見的以后再補。


<replace dir ="." includes="*.txt" encoding="GBK">
<replacefilter token ="Task" value="that" />

</replace>

說明 字符串替換標簽。 並且是批量進行的。

 Mkdir
 創建一個目錄,如果他的父目錄不存在,也會被同時創建。
 例子:<mkdir dir="build/classes"/>    說明: 如果build不存在,也會被同時創建


 Copy
 拷貝一個(組)文件、目錄
 例子:

1. 拷貝單個的文件:<copy file="myfile.txt" tofile="mycopy.txt"/>
2. 拷貝單個的文件到指定目錄下 <copy file="myfile.txt" todir="../some/other/dir"/>
3. 拷貝一個目錄到另外一個目錄下

<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
拷貝一批文件到指定目錄下
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
拷貝一批文件到指定目錄下,將文件名后增加。Bak后綴
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<mapper type="glob" from="*" to="*.bak"/>
</copy>
拷貝一組文件到指定目錄下,替換其中的@標簽@內容
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="TITLE" value="Foo Bar"/>
</filterset>
</copy>


 Delete
 刪除一個(組)文件或者目錄
 例子:
1. 刪除一個文件
<delete file="/lib/ant.jar"/>
2. 刪除指定目錄及其子目錄
<delete dir="lib"/>
3. 刪除指定的一組文件
<delete>
<fileset dir="." includes="**/*.bak"/>
</delete>
4. 刪除指定目錄及其子目錄,包括他自己
<delete includeEmptyDirs="true">
<fileset dir="build"/>
</delete>


 Move
 移動或重命名一個(組)文件、目錄
 例子:
移動或重命名一個文件
<move file="file.orig" tofile="file.moved"/>
 移動或重命名一個文件到另一個文件夾下面
<move file="file.orig" todir="dir/to/move/to"/>
 將一個目錄移到另外一個目錄下
<move todir="new/dir/to/move/to">
<fileset dir="src/dir"/>
</move>
將一組文件移動到另外的目錄下
<move todir="some/new/dir">
<fileset dir="my/src/dir">
<include name="**/*.jar"/>
<exclude name="**/ant.jar"/>
</fileset>
</move>
移動文件過程中增加。Bak后綴
<move todir="my/src/dir">
<fileset dir="my/src/dir">
<exclude name="**/*.bak"/>
</fileset>
<mapper type="glob" from="*" to="*.bak"/>
</move>

 

 

自定義任務

    開發非常簡單,如下 繼承Task 類 ,在execute 里 編寫task 內容即可。Jar引用是ant安裝目錄下lib里自帶的ant.jar。示例代碼如下:

import org.apache.tools.ant.Task;

public class MyTask extends Task {

       @Override

       public void execute() {

       }

}

用taskdef表簽聲明自定義任務,屬性classname 指定自定義任務類的類名

   <target  name="mytask"  depends="compile">

      <taskdef  name="mytask" classname="org.zrz.MyTask" classpath="${build.dir}"/>

   </target>

    Ant自定義任務返回值是通過在繼承Task的類中serProperty方法將一個Task屬性字段設置進去,然后通過getProject().setNewProperty(propertyName,propertyValue)來進行取值設置。示例代碼如下:

 

 1 import org.apache.tools.ant.Task;
 2 
 3 public class MyTask extends Task {
 4 
 5        private String name;
 6 
 7        private String resultProperty;
 8 
 9        public String getName() {
10 
11               return name;
12 
13        }
14 
15  
16 
17        public void setName(String name) {
18 
19               this.name = name;
20 
21        }
22 
23  
24 
25        public void setProperty(String resultProperty) {
26 
27               this.resultProperty = resultProperty;
28 
29        }
30 
31  
32 
33        @Override
34 
35        public void execute() {
36 
37               //任務處理此處省略
38 
39               //....
40 
41               getProject().setNewProperty(resultProperty, name);
42 
43        }
44 
45  
46 
47 }

 

在target里取出返回值

<target  name="mytask"  depends="compile">

              <taskdef  name="mytask" classname="org.zrz.MyTask" classpath="${build.dir}"/>

              <mytask property="result" 

                          name="Sample" 

                      />

              <echo message="result=${result}"/>

       </target>

ant引用三方jar

引用三方jar包時,在ant的build.xml文件中添加子元素path,該path包含引用的三方jar包,形式如下:

<path id="compile.path">

       <fileset dir="lib">

              <include name="**/*.jar"/>

       </fileset>

<pathelement path="${build.path}"/>

</path>

其中:path標簽的id屬性自定義,fileset標簽的dir屬性為引入三方jar包所在路徑(可以是build.xml的相對路徑), include標簽的name屬性表示要包含的jar包(文件)。

在編譯時引用三方jar包需要在target標簽的子元素標簽javac標簽下再添加classpath子元素,<classpath refid="compile.path"/> 或者javac標簽添加屬性classpathref="compile.path"      。

 

<pathelement path="${build.path}"/>標簽是在執行時所用的,該路徑是項目java文件編譯后所存放的類文件的位置,如果只編譯,則不需要,如執行時的target:

<target name="junit" depends="compile">

       <junit printsummary="true">

              <classpath refid="compile.path"/>

              <test name="com.neusoft.cc.test.TestAdd"></test>

       </junit>

</target>

 

完整的build.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<project name="Junit" default="junit" basedir=".">

 

<property name="build.path" value="generator/classes"></property>

      

<path id="compile.path">

       <fileset dir="lib">

              <include name="**/*.jar"/>

       </fileset>

       <pathelement path="${build.path}"/>

</path>

      

<target name="init">

       <mkdir dir="${build.path}"/>

</target>

 

<target name="clean">

       <delete dir="${build.path}"></delete>

</target>

      

<target name="compile" depends="clean,init">

       <javac srcdir="src" destdir="${build.path}" classpathref="compile.path" includeantruntime="false"/>

</target>

      

<target name="junit" depends="compile">

       <junit printsummary="true">

              <classpath refid="compile.path"/>

              <test name="com.neusoft.cc.test.TestAdd"></test>

       </junit>

</target>

 

</project>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM