zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    linq to sql内链接,左右链接(示例)
    ASP.NET MVC HtmlHelper用法大全
    浅析PHP学习的路线图
    c#大话设计模式(带目录完整版)[中文PDF+源代码]
    SQL语言实务速查效率手册
    设计模式基于C#的工程化实现及扩展
    sql 时间类型 like 查询
    DWZ表单验证规则一览表
    【今日CS 视觉论文速览】Thu, 13 Dec 2018
    【今日CS 视觉论文速览】Wed, 12 Dec 2018
  • 原文地址:https://www.cnblogs.com/anyehome/p/8471478.html
Copyright © 2011-2022 走看看