天天看点

ant 学习(4)--常用task

 ant核心task

ant

Invokes Ant on another buildfile.

antcall

Calls a target in the current buildfile.

antstructure

Creates an XML Document Type Definition (DTD) for Ant buildfiles.

apply

Executes a system command on a set of files.

available

Sets a property if a resource is available.

chmod

Changes permissions on files and directories (Unix platforms only).

condition

Sets a property if a condition is true.

copy

Copies files and directories.

copydir

Deprecated in Ant 1.2; use the copy task instead.

copyfile

cvs

Executes Concurrent Versions System (CVS) commands.

cvspass

Adds passwords to a .cvspass file; equivalent to the CVS login command.

delete

Deletes files and directories.

deltree

Deprecated in Ant 1.2; use the delete task instead.

dependset

Manages dependencies between files, removing|target files if any are out-ofdate

ear

Builds Enterprise Application Archive (EAR) files.

echo

Writes a message to the Ant log or a file.

exec

Executes a native system command.

execon

Deprecated in Ant ; use the apply task instead.

fail

Throws a BuildException, causing the current build to terminate.

filter

Sets token filters for the current project.

fixcrlf

Cleans up special characters in source files, such as tabs, carriage returns,linefeeds, and EOF characters.

genkey

Generates a key in a keystore.

get

Gets a file from a URL.

gunzip

Unzips a GZip file.

gzip

Creates a GZip file.

jar

Creates a JAR file.

java

Executes a Java class.

javac

Compiles Java source code.

javadoc

Runs the JavaDoc utility to generate source code documentation.

mail

Sends email using SMTP.

mkdir

Creates a directory.

move

Moves files and directories.

parel

Executes multiple tasks in concurrent threads.

patch

Applies a diff file to originals.

pathconvert

Converts Ant paths into platform-specific paths.

property

Sets properties in the project.

record

Logs output from the current build process.

rename

Deprecated in Ant 1.2; use the move task instead.

replace

Performs string replacement in one or more files.

rmic

Runs the rmic compiler.

sequential

Executes multiple tasks sequentiy; designed for use with the parel task.

signjar

Executes the javasign command-line tool.

sleep

Pauses the build for a specified interval.

sql

Executes SQL commands using JDBC.

style

Performs XSLT transformations.

tar

Creates a tar archive.

taskdef

Adds custom tasks to the current project.

touch

Updates the timestamp of one or more files.

tstamp

Sets the DSTAMP, TSTAMP, and TODAY properties.

typedef

Adds a DataType to the current project.

unjar

Expands a ZIP file, WAR file, or JAR file.

untar

Expands a tar file.

unwar

unzip

uptodate

Sets a property if one or more target files are up-to-date with respect to corresponding source files

war

Creates a Web Application Archive (WAR) file.

zip

Creates a ZIP file.

Invoke the default target on util_buildfile.xml in the current directory:

<ant antfile="util_buildfile.xml"/>

Invoke the clean target on build.xml in the gui directory:

<ant dir="gui" target="clean"/>

Invoke  another buildfile, passing a new value for the builddir property. The  value is explicitly set to utiloutput even if the property was defined  elsewhere in the calling buildfile:

<ant antfile="util_buildfile.xml">

<property name="builddir" value="utiloutput"/>

</ant>

Copy

<copy todir="${builddir}/srccopy">

<fileset dir="${srcdir}">

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

</fileset>

<filterset>

<filter token="VERSION" value="${app.version}"/>

</filterset>

</copy>

Here is a common target found in just about every Ant buildfile. It deletes the build directory and all of its contents:

<target name="clean" description="Remove all generated code">

<delete dir="${builddir}"/>

</target>

The  first of the following examples specifies the text to write using the  message attribute. The second example specifies the text to write by  enclosing it within <echo>...</echo> tags.

<echo message="Building to ${builddir}"/>

<echo>You are using version ${java.version}

of Java! This message spans two lines.</echo>

Compile all Java source files in the com.oreilly.antbook package and subpackages, placing results in ${builddir}:

<javac srcdir="${srcdir}"

destdir="${builddir}"

includes="com/oreilly/antbook/**"/>

This  task is commonly used in a prepare target that other targets depend on.  This ensures that necessary destination directories are created before  other targets are executed.

<target name="prepare">

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

<mkdir dir="${deploydir}/docs"/>

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

...

<!-- start a web server, then wait a few seconds for it to initialize -->

<sleep seconds="10"/>

<!-- now start the client unit tests -->

<code>&lt;?</code><code>xml</code> <code>version</code><code>=</code><code>"1.0"</code>

<code>encoding</code><code>=</code><code>"GBK"</code><code>?&gt;</code>

<code>[code]</code><code>&lt;!-- 定义生成文件的project根元素,默认的target为空</code>

<code>--&gt;</code>

<code>&lt;</code><code>project</code> <code>name</code><code>=</code><code>"antQs"</code> <code>basedir</code><code>=</code><code>"."</code>

<code>default</code><code>=</code><code>""</code><code>&gt;</code>

<code>&lt;!-- 定义三个简单属性 --&gt;</code>

<code>&lt;</code><code>property</code> <code>name</code><code>=</code><code>"src"</code>

<code>value</code><code>=</code><code>"src"</code><code>/&gt;</code>

<code>&lt;</code><code>property</code> <code>name</code><code>=</code><code>"classes"</code>

<code>value</code><code>=</code><code>"classes"</code><code>/&gt;</code>

<code>&lt;</code><code>property</code> <code>name</code><code>=</code><code>"dest"</code>

<code>value</code><code>=</code><code>"dest"</code><code>/&gt;</code>

<code>&lt;!-- 定义一组文件和目录集 --&gt;</code>

<code>&lt;</code><code>path</code> <code>id</code><code>=</code><code>"classpath"</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>pathelement</code>

<code>path</code><code>=</code><code>"${classes}"</code><code>/&gt;</code>

<code>&lt;/</code><code>path</code><code>&gt;</code>

<code>&lt;!-- 定义help target,用于输出该生成文件的帮助信息</code>

<code>&lt;</code><code>target</code> <code>name</code><code>=</code><code>"help"</code>

<code>description</code><code>=</code><code>"打印帮助信息"</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>echo</code><code>&gt;help -</code>

<code>打印帮助信息&lt;/</code><code>echo</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>echo</code><code>&gt;compile</code>

<code>- 编译Java源文件&lt;/</code><code>echo</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>echo</code><code>&gt;run -</code>

<code>运行程序&lt;/</code><code>echo</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>echo</code><code>&gt;build</code>

<code>- 打包JAR包&lt;/</code><code>echo</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>echo</code><code>&gt;clean</code>

<code>- 清除所有编译生成的文件&lt;/</code><code>echo</code><code>&gt;</code>

<code>&lt;/</code><code>target</code><code>&gt;</code>

<code>&lt;!-- 定义compile target,用于编译Java源文件</code>

<code>&lt;</code><code>target</code> <code>name</code><code>=</code><code>"compile"</code>

<code>description</code><code>=</code><code>"编译Java源文件"</code><code>&gt;</code>

<code>     </code><code>&lt;!-- 先删除classes属性所代表的文件夹</code>

<code>     </code><code>&lt;</code><code>delete</code>

<code>dir</code><code>=</code><code>"${classes}"</code><code>/&gt;</code>

<code>     </code><code>&lt;!-- 创建classes属性所代表的文件夹</code>

<code>     </code><code>&lt;</code><code>mkdir</code>

<code>     </code><code>&lt;!--</code>

<code>编译Java文件,编译后的class文件放到classes属性所代表的文件夹内 --&gt;</code>

<code>     </code><code>&lt;</code><code>javac</code> <code>destdir</code><code>=</code><code>"${classes}"</code>

<code>debug</code><code>=</code><code>"true"</code>

<code>           </code><code>deprecation</code><code>=</code><code>"false"</code>

<code>optimize</code><code>=</code><code>"false"</code> <code>fail&gt;</code>

<code>           </code><code>&lt;!--</code>

<code>指定需要编译的Java文件所在的位置 --&gt;</code>

<code>           </code><code>&lt;</code><code>src</code>

<code>path</code><code>=</code><code>"${src}"</code><code>/&gt;</code>

<code>指定编译Java文件所需要第三方类库所在的位置 --&gt;</code>

<code>           </code><code>&lt;</code><code>classpath</code>

<code>refid</code><code>=</code><code>"classpath"</code><code>/&gt;</code>

<code>     </code><code>&lt;/</code><code>javac</code><code>&gt;</code>

<code>&lt;!-- 定义run target,用于运行Java源文件,</code>

<code>     </code><code>运行该target之前会先运行compile target</code>

<code>&lt;</code><code>target</code> <code>name</code><code>=</code><code>"run"</code> <code>description</code><code>=</code><code>"运行程序"</code>

<code>depends</code><code>=</code><code>"compile"</code><code>&gt;</code>

<code>运行lee.HelloTest类,其中fork指定启动另一个JVM来执行java命令 --&gt;</code>

<code>     </code><code>&lt;</code><code>java</code>

<code>classname</code><code>=</code><code>"lee.HelloTest"</code> <code>fork</code><code>=</code><code>"yes"</code> <code>fail&gt;</code>

<code>运行Java程序时传入2个参数 --&gt;</code>

<code>           </code><code>&lt;</code><code>arg</code>

<code>line</code><code>=</code><code>"测试参数1 测试参数2"</code><code>/&gt;</code>

<code>     </code><code>&lt;/</code><code>java</code><code>&gt;</code>

<code>&lt;!-- 定义build target,用于打包JAR文件,</code>

<code>&lt;</code><code>target</code> <code>name</code><code>=</code><code>"build"</code> <code>description</code><code>=</code><code>"打包JAR文件"</code>

<code>     </code><code>&lt;!-- 先删除dest属性所代表的文件夹</code>

<code>dir</code><code>=</code><code>"${dest}"</code><code>/&gt;</code>

<code>     </code><code>&lt;!-- 创建dest属性所代表的文件夹</code>

<code>指定将classes属性所代表的文件夹下的所有</code>

<code>           </code><code>*.classes文件都打包到app.jar文件中</code>

<code>     </code><code>&lt;</code><code>jar</code>

<code>destfile</code><code>=</code><code>"${dest}/app.jar"</code> <code>basedir</code><code>=</code><code>"${classes}"</code>

<code>           </code><code>includes</code><code>=</code><code>"**/*.class"</code><code>&gt;</code>

<code>为JAR包的清单文件添加属性 --&gt;</code>

<code>           </code><code>&lt;</code><code>manifest</code><code>&gt;</code>

<code>               </code><code>&lt;</code><code>attribute</code>

<code>name</code><code>=</code><code>"Main-Class"</code> <code>value</code><code>=</code><code>"lee.HelloTest"</code><code>/&gt;</code>

<code>           </code><code>&lt;/</code><code>manifest</code><code>&gt;</code>

<code>     </code><code>&lt;/</code><code>jar</code><code>&gt;</code>

<code>&lt;!-- 定义clean target,用于删除所有编译生成的文件</code>

<code>&lt;</code><code>target</code> <code>name</code><code>=</code><code>"clean"</code>

<code>description</code><code>=</code><code>"清除所有编译生成的文件"</code><code>&gt;</code>

<code>     </code><code>&lt;!-- 删除两个目录,目录下的文件也一并删除</code>

<code>&lt;/</code><code>project</code><code>&gt;</code>

<code></code>

本文转自 326647452 51CTO博客,原文链接:http://blog.51cto.com/svsky/2074018,如需转载请自行联系原作者