ant学习2(ant标签及build.xml)

ant学习二(ant标签及build.xml)
1、build.xml
build.xml是ant的默认buildfile,如果想执行其它的buildfile,通过ant -buildfile filename可指定,在ant学习一中已有介绍。
Ant的buildfile是用XML写的。每个buildfile含有一个project,project中可包含多个target,多个property,多个taskdef等,通过ant targetname来调用指定的target。
2、project介绍
project有下面的属性:
name:项目名称
default:当没有指定target时使用的缺省target
basedir:用于计算所有其他路径的基路径。该属性可以被basedir property覆盖。当覆盖时,该属性被忽略。如果属性和basedir property都没有设定,就使用buildfile文件的父目录。
description:项目的描述以一个*的<description>元素的形式出现(参看description小节)。
3、target
    一个项目可以定义一个或多个target,一个target是一系列你想要执行的代码,执行时,你可以通过ant targetname选择执行哪个target。
target有下面的属性:
(1)name:target的名字
(2)depends:用逗号分隔的target的名字列表,也就是依赖表。 一个target可以依赖其他的target,通过depends属性指定依赖的target,Ant会依照depends属性中target出现的顺序从左到右依次执行,但是要注意,depends属性只指定了target应该被执行的顺序,如果依赖的target无法运行,不会影响到当前的target。
    Ant会依照depends属性中target出现的顺序(从左到右)依次执行每个target。然而,要记住的是只要某个target依赖于一个target,后者就会被先执行。
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
假定我们要执行target D,从它的依赖属性来看,你可能认为先执行C,然后B,最后A被执行。错了,C依赖于B,B依赖于A,所以先执行A,然后B,然后C,最后D被执行。
    一个target只能被执行一次,即时有多个target依赖于它(看上面的例子)。
(3)if:执行target所需要设定的属性名,即if指定的属性必须被设定该target才会被执行。
(4)unless:执行target必须未设定的属性名,即如果该属性被设定,则不会执行该target。
    通过if和unless可以根据系统的状态(java version, OS, 命令行属性定义等等)来更好地控制build的过程。要想让一个target这样做,你就应该在target元素中,加入if(或unless)属性,值为target应该有所判断的属性。例如:
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
如果没有if或unless属性,target总会被执行。
(5)description:关于target功能的简短描述。
    可选的description属性可用来提供关于target的描述信息,如果设置了该属性,该任务可由-projecthelp命令行选项输出,没有设置则执行ant -p时不会显示该target。
4、task
    一个task是一段可执行的代码。
    一个task可以有多个属性(如果你愿意的话,可以将其称之为变量)。属性只可能包含对property的引用。这些引用会在task执行前被解析。
    下面是Task的一般构造形式:
         <name attribute1="value1" attribute2="value2" ... />
这里name是task的名字,attributeN是属性名,valueN是属性值。
    Ant有一套内置的(built-in)task,以及一些可选task,但你也可以编写自己的task。所有的task都有一个task名字属性。Ant用属性值来产生日志信息。
    可以给task赋一个id属性:
        <taskname id="taskID" ... />
这里taskname是task的名字,而taskID是这个task的唯一标识符。通过这个标识符,你可以在脚本中引用相应的task。例如,在脚本中你可以这样:
<script ... >
task1.setFoo("bar");
</script>
设定某个task实例的foo属性。在另一个task中(用java编写),你可以利用下面的语句存取相应的实例。
project.getReference("task1")
Ant自带很多task,可参考apache官网http://ant.apache.org/manual/
注意1:如果task1还没有运行,就不会被生效(例如:不设定属性),如果你在随后配置它,你所作的一切都会被覆盖。
注意2:未来的Ant版本可能不会兼容这里所提的属性,因为很有可能根本没有task实例,只有proxies。
5、Ant属性及property
    property标签可用于task的属性值的设定,一个project可以有很多的property。
     Ant属性(不是标签的属性)是指可以在一个构建文件中用作符号常量的名称-值对,属性的值是通过用${}中括起属性名的方式引用的。例如,如果一个属性junit_home的值为D:/junit/junit3.8.1(<property name="junit_home" value="D:\junit\junit3.8.1" /> ),你可以使用该属性${junit_home}以在编译时添加junit的JAR文件到classpath中。
Ant属性可以用下面几种方式定义:
1)Ant预定义
   Ant内置属性:basedir
              ant.file
              ant.version
              ant.project.name
              ant.project.default-target
              ant.project.invoked-targets
              ant.home
              ant.java.version  //ant检测到的JVM版本
              ant.core.lib
  另外还有一些java属性,ant也可以获取到:
                java.version   //Java的版本信息。
                java.home      //Java的根目录位置。
                java.vendor   //Java供应商
                java.vendor.url
                java.class.path  //Java类库的文件的位置。
                java.vm.version  //Java虚拟机版本
                java.vm.vendor  //Java虚拟机供应商
                java.vm.name    //Java虚拟机名称
                java.vm.specification.version
                java.vm.specification.vendor
                java.vm.specification.name
                java.class.version  //java类格式版本号
                java.class.path   //java类路径
                java.library.path
                java.io.tmpdir
                java.compiler
                java.ext.dirs
                line.separator  //行分隔符(在unix中是"/n")
                path.separator  //路径分隔符(在unix中是":")
                file.separator  //文件分隔符(在unix中是"/")
                user.home    //用户的home目录,在Linux和UNIX中经常用到
                user.name   //用户的账户名
                user.dir   //用户的当前工作目录
                os.name  //操作系统名称
                os.version  //操作系统版本
                os.arch     //操作系统版本
另外还有很多Ant预定义的属性,这里不一一列举
2)在Ant命令行使用-D选项定义,例如ant -Djunit_home=D:/junit/junit3.8.1
3)在一个构建文件中property定义
说明:target标签中的if、unless属性用于判断Ant属性是否存在,来决定该target是否执行。 

property特点:大小写敏感;
              不可改变,先到先得,谁先设定,之后的都不能改变
property标签的属性如下:
- name:the name of the property to set.
- value:the value of the property. One of these or nested text, when using the name attribute
- location:Sets the property to the absolute filename of the given file. If the value of this attribute is an absolute path, it is left unchanged (with / and \ characters converted to the current platforms conventions). Otherwise it is taken as a path relative to the project's basedir and expanded.
- refid:Reference to an object defined elsewhere. Only yields reasonable results for references to PATH like structures or properties.
- resource:the name of the classpath resource containing properties settings in properties file format. One of these, when not using the name attribute
file the location of the properties file to load.
- url:a url containing properties-format settings.
- environment:the prefix to use when retrieving environment variables. Thus if you specify environment="myenv" you will be able to access OS-specific environment variables via property names "myenv.PATH" or "myenv.TERM". Note that if you supply a property name with a final "." it will not be doubled; i.e. environment="myenv." will still allow access of environment variables through "myenv.PATH" and "myenv.TERM". This functionality is currently only implemented on select platforms. Feel free to send patches to increase the number of platforms on which this functionality is supported ;).
Note also that properties are case-sensitive, even if the environment variables on your operating system are not; e.g. Windows 2000's system path variable is set to an Ant property named "env.Path" rather than "env.PATH".
- classpath:the classpath to use when looking up a resource.  
- classpathref:the classpath to use when looking up a resource, given as - -reference to a <path> defined elsewhere..  
- prefix:Prefix to apply to properties loaded using file, resource, or url. A "." is appended to the prefix if not specified.  
- prefixValues:Whether to apply the prefix when expanding the right hand side of - properties loaded using file, resource, or url. Since Ant 1.8.2 No (default=false)
- relative:If set to true the relative path to basedir is set. Since Ant 1.8.0   (default=false)
- basedir:The basedir to calculate the relative path from. Since Ant 1.8.0

怎样设置
1)设置 name 和 value 属性值,比如: <property name="srcdir" value="${basedir}/src"/>
2)设置 name 和 refid 属性值,比如: <property name="srcpath" refid="dao.compile.classpath"/> ,其中 dao.compile.classpath 在别的地方定义。
3)设置 name 和 location 属性值,比如: <property name="srcdir" location="src"/> ,即将 srcdir 的值设     置为:当前项目根目录的 /src 目录。
4)设置 file 属性值,比如: <property file="build.properties"/> , 导入 build.properties 属性文件中的属性值
5)设置 resource 属性值,比如: <propety resource="build.properties"/>, 导入 build.properties 属性文件中的属性值
6)设置 url 属性值,比如: <property url="http://www.blogjava.net/wiflish/build.properties"/>, 导入 http://www.blogjava.net/wiflish/build.properties 属性文件中的属性值。
7)设置环境变量,比如: <property environment="env"/> ,设置系统的环境变量为前缀 env.
     <property name="tomcat.home" value="${env.CATALINA_HOME}"/> 将系统的 tomcat 安装目录设置到tomcat.home 属性中。