您的位置:首页 > 其它

Ant 常用任务

2012-01-05 16:50 183 查看
1 property 任务:

用法:

<property file="./xxx/comversion.properties" />

可以在构建文件中引入属性文件中的value属性,使用${key} 来引用。







2 ant 任务:

用法:

<ant antfile="./xxx/buildcomponent.xml" >

调用buildcomponent.xml 文件同的默认目标。

<property name="path" value="pathvalue" />
传递属性给目标

<property name="version" value="versionvalue" />

</ant>



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

用法:

<delete dir="../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="source" param="fe.source.path"

list="${plugin.path}" delimiter=";">

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



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

声明:

<taskdef name="propertyregex"

classname="net.sf.antcontrib.property.RegexTask" />

用法:

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

用法:

<available file="${pluginpath}/src/" type="dir" property="src.exit" />

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

10 replace 任务


用法:

<replace file="${release.feature.path}/feature.xml" token='"1.0.0.qualifier"'

value='"1.0.0.${plugindateversion}"' />

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

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

<project default="myJar" basedir="." >

<property name="destdir" value="destdir" />

<property name="jardestdir" value="jardestdir" />

<property name="jardestdir2" value="jardestdir2" />

<target name="createdir" >

<mkdir dir="${destdir}" />

<mkdir dir="${jardestdir2}" />

</target>

<target name="myComplie" depends="createdir">

<javac srcdir="src" destdir="${destdir}" />

</target>

<target name="myJar" depends="myComplie">

<jar basedir="${destdir}" destfile="${jardestdir}/package.jar" />

</target>

<target name="moveFile" depends="myJar">

<copy file="${jardestdir}/package.jar" todir="${jardestdir2}" />

</target>

<taskdef name="renameFile" classname="db.pojo.AntTest" classpath="WebRoot\WEB-INF\classes"></taskdef>

<target name="rename">

<renameFile file="c:\test" includeFileName="hello"/>

<!-- c:\test目录中的文件名含有hello的文件去掉 -->

</target>

</project>

4.1File(Directory)类

4.1.1Mkdir

?创建一个目录,如果他的父目录不存在,也会被同时创建。

?例子:

<mkdirdir="build/classes"/>

?说明:如果build不存在,也会被同时创建

4.1.2Copy

?拷贝一个(组)文件、目录

?例子:

1.拷贝单个的文件:

<copyfile="myfile.txt"tofile="mycopy.txt"/>

2.拷贝单个的文件到指定目录下

<copyfile="myfile.txt"todir="../some/other/dir"/>

3.拷贝一个目录到另外一个目录下

<copytodir="../new/dir">

<filesetdir="src_dir"/>

</copy>

4.拷贝一批文件到指定目录下

<copytodir="../dest/dir">

<filesetdir="src_dir">

<excludename="**/*.Java"/>

</fileset>

</copy>

<copytodir="../dest/dir">

<filesetdir="src_dir"excludes="**/*.java"/>

</copy>

5.拷贝一批文件到指定目录下,将文件名后增加。Bak后缀

<copytodir="../backup/dir">

<filesetdir="src_dir"/>

<mappertype="glob"from="*"to="*.bak"/>

</copy>

6.拷贝一组文件到指定目录下,替换其中的@标签@内容

<copytodir="../backup/dir">

<filesetdir="src_dir"/>

<filterset>

<filtertoken="TITLE"value="FooBar"/>

</filterset>

</copy>

4.1.3Delete

?删除一个(组)文件或者目录

?例子

1.删除一个文件

<deletefile="/lib/ant.jar"/>

2.删除指定目录及其子目录

<deletedir="lib"/>

3.删除指定的一组文件

<delete>

<filesetdir="."includes="**/*.bak"/>

</delete>

4.删除指定目录及其子目录,包括他自己

<deleteincludeEmptyDirs="true">

<filesetdir="build"/>

</delete>

4.1.4Move

?移动或重命名一个(组)文件、目录

?例子:

1.移动或重命名一个文件

<movefile="file.orig"tofile="file.moved"/>

2.移动或重命名一个文件到另一个文件夹下面

<movefile="file.orig"todir="dir/to/move/to"/>

3.将一个目录移到另外一个目录下

<movetodir="new/dir/to/move/to">

<filesetdir="src/dir"/>

</move>

4.将一组文件移动到另外的目录下

<movetodir="some/new/dir">

<filesetdir="my/src/dir">

<includename="**/*.jar"/>

<excludename="**/ant.jar"/>

</fileset>

</move>

5.移动文件过程中增加。Bak后缀

<movetodir="my/src/dir">

<filesetdir="my/src/dir">

<excludename="**/*.bak"/>

</fileset>

<mappertype="glob"from="*"to="*.bak"/>

</move>

参考:
http://blog.csdn.net/lihe2008125/article/details/4488632 http://www.mscto.com/JavaBase/2009022459996.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: