随便记一下 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 > </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 >

       <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 ref      。

 

<pathelement path="${build.path}"/>标签是在执行时所用的,该路径是项目java文件编译后所存放的类文件的位置,如果只编译,则不需要,如执行时的target:

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

       <junit printsummary="true">

              <classpath ref/>

              <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 >

       <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 ref/>

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

       </junit>

</target>

 

</project>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
  • 2021-12-21
  • 2022-02-09
  • 2021-06-21
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2021-11-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
相关资源
相似解决方案