---
title: "ant的一些任务,可能不是很常用,但感觉不错"
author: "laorer"
date: "2008-04-16T21:38:54Z"
tags: []
slug: "ant"
draft: false
lastmod: "2008-04-16T21:38:54Z"
id: 183526
url: "https://laorer.iteye.com/blog/183526"
---

ant的一些核心任务;

1. 
basename
 Sets a property to the last element of a specified path in an effort to determine a file's name without directory structure

2.
dirname
 Assigns a file's directory path to a property
 
3.
input
 Displays a message and reads a line of input from the console, allowing for user input during the build process

4.
import
 Allows the use of other Ant files
 
5.
record
 Runs a listener that records the logging output of the build process to a file
 
6.
sequential
 A container task that can contain other Ant tasks and run them in sequence
 
7.
rmic
 Invokes the rmic compiler
 
8.
subant
 Runs Ant within all subdirectories of the project directory
 
9.
tstamp
 Sets time-based properties to the current time
 
10.
Ant has some additional Ant-specific properties:

ant.file

Contains the absolute path of the build file

ant.java.version

Contains the JVM version Ant is using (can hold only 1.1, 1.2, 1.3, 1.4 and  1.5)

ant.project.name 

Holds the name of the project that is executing (set with the name attribute of project)

ant.version 

Contains the version of Ant running

basedir 

Holds the absolute path of the project's base directory (set with the basedir attribute of project)


=======================
摘抄自 java development with ant
ant-contrlib的一些task，觉得可能有用的
1.
propertycopy，感觉有点像赋值，但是不是简单的赋值
<target name="propertycopy">
<property name="X" value="Y"/>
<property name="Y" value="Z"/>
<propertycopy name="A" from="${X}"/>
<echo message="A = ${A}"/>
</target>
The value of ${X} is “Y”. The from attribute of <propertycopy> refers to an Ant
property name, “Y” in this example. The value of the property Y is “Z”, so the output
is “A = Z”. This is a much nicer alternative than using the refid tricks. 

2.osfamily 和操作系统相关的
<target name="osfamily">
<osfamily property="os.family" />
<echo message="O/S family is ${os.family}"/>
<property file="${os.family}.properties"/>
</target>
Executing this target on a Windows 2000 machine would load windows.properties.
Loading properties based on operating system family, or by hostname, enables build
files to adapt easily to their operating environment.

3.
Using if/then/else logic 逻辑判断，觉得会用到
<target name="if">
<if>
<equals arg1="${foo}" arg2="bar" />
<then>
<echo message="The value of property foo is bar" />
</then>
<else>
<echo message="The value of property foo is not bar" />
</else>
</if>
</target>

4.
Multiple value switching 开关控制

<target name="switch">
<switch value="${foo}">
<case value="bar">
<echo message="The value of property foo is bar" />
</case>
<case value="baz">
<echo message="The value of property foo is baz" />
</case>
<default>
<echo message="The value of property foo is not sensible" />
</default>
</switch>
</target>

5.
Catching task exceptions 使用try catch，不知道用处有多大

<target name="trycatch">
<trycatch property="exception.message"
reference="exception.ref">
<try>
<fail>Oops!</fail>
</try>
<catch>
<echo>Caught</echo>
</catch>
<finally>
<echo>Finally</echo>
</finally>
</trycatch>
<echo>As property: ${exception.message}</echo>
<property name="exception.value" refid="exception.ref" />
<echo>From reference: ${exception.value}</echo>
</target>
.....
Executing this target produces this output:
trycatch:
 Caught exception: Oops!
 Caught
 Finally
 As property: Oops!
 From reference: C:\AntBook\Sections\Applying\tasks\
ant-contrib.xml:72: Oops!
BUILD SUCCESSFUL

6.
Using explicit iteration  ，我一般使用for, 不知道为什么，这个应该还有点用处的吧
<target name="for-each">
<foreach list="1,2,3" target="loop" param="var"
delimiter=",">
<fileset dir="."/>
</foreach>
</target>
<target name="loop">
<echo message="var = ${var}"/>
</target>
...
out put is 
for-each:
loop:
 var = 1
loop:
 var = 2
loop:
 var = 3
loop:
 var = C:\AntBook\Sections\Applying\tasks\ant-contrib.xml
loop:
 var = C:\AntBook\Sections\Applying\tasks\build\build.properties
.
.
The target is invoked for each iteration by using the underlying mechanism that the
<antcall> task uses, which means that the dependencies of the target are reevaluated
each iteration.