Ant学习笔记

来源:互联网 发布:矢量软件coreldraw 编辑:程序博客网 时间:2024/06/02 04:54

构建属性文件的内容类似于普通的Java属性文件。他们每行包含一个属性。每个属性由一个名称和一个值对来表示。名称和值对由等号分开。强烈建议属性标注了正确的注释。注释列出所使用的哈希字符。

下面显示了一个build.xml文件和相关build.properties文件

build.xml

<?xml version="1.0"?>
<projectname="Hello World Project"default="info">
   <propertyfile="build.properties"/>
      <targetname="info">
         <echo>Apache Ant version is ${ant.version} - You are 
         at ${sitename} </echo>
     </target>
</project>

build.properties

# The Site Name
sitename=www.yiibai.com
buildversion=3.3.2

在上面的例子中,站点名是被映射到该站点的名称自定义属性,可以声明任意数目以这种方式自定义属性。在上面的例子中所列的另一个自定义属性是buildversion,其中,在这种情况下指的是创建的版本。

 

除上述者外,Ant附带了一些预定义的构建属性,它已被列入上一节中,但下面是代表一次。

属性

描述

ant.file

The full location of the build file

ant.version

The version of the Apache Ant installation

basedir

The basedir of the build, as specified in the basedir attribute of theproject element.

ant.java.version

The version of the JDK that is used by Ant.

ant.project.name

The name of the project, as specified in the name atrribute of theproject element.

ant.project.default-target

The default target of the current project

ant.project.invoked-targets

Comma separated list of the targets that were invoked in the current project

ant.core.lib

The full location of the ant jar file

ant.home

The home directory of Ant installation

ant.library.dir

The home directory for Ant library files - typically ANT_HOME/lib folder.

 

 

path

1、path是ant内置的一种datatype,作用是声明路径之类的东西,在官方的manual中也叫做Path-likeStructures,一般是这样声明的 

Xml代码  

1. <path id="id">  

2.     <pathelement location="location1" />  

3.     <pathelement location="location2" />  

4. </path>  


或者 

Xml代码  

1. <path id="id">  

2.     <pathelement path="location1;location2" />  

3. </path>  


或者 

Xml代码  

1. <path id="id">  

2.     <pathelement path="location1:location2" />  

3. </path>  


声明path的时候,可以用内嵌的<pathelement>元素,来指定若干个位置。<pathelement>元素常用的属性有2个,location可以声明一个路径,path可以声明多个路径,其中用;或者:来分隔 

2、不过简单的情况下,一般不这么写,而是用替代方式 

Xml代码  

1. <path id="id" location="location" />  


或者 

Xml代码  

1. <path id="id" path="location1;location2" />  


或者 

Xml代码  

1. <path id="id" path="location1:location2" />  


很容易看出来,这种形式是第一种的简化写法,效果是一样的 

但是有些时候比较复杂,也就需要用到<pathelement>元素了,比如ant自带的build.xml中 

Xml代码  

1. <path id="classpath">  

2.     <fileset dir="lib/optional" includes="*.jar"/>  

3. </path>  

4.   

5. <path id="tests-classpath">  

6.     <pathelement location="${build.classes}"/>  

7.     <path refid="classpath"/>  

8. </path>  


这个例子里,tests-classpath需要组合2个path,所以就不能使用上面说的简化方式了,这里就用到了<pathelement>元素 

3、<path>中可以带一个<fileset>,这种写法也是很常见的,比如说: 

Xml代码  

1. <path id="classpath">  

2.     <fileset dir="${lib.dir}">  

3.         <include name="**/*.jar" />  

4.     </fileset>  

5. </path>  


4、path的使用方式有2种 

第一种是用id声明,然后用refid来引用 
第二种是直接内联 

这2种方式在上面的2里都有例子 

5、build.xml本身是基于xml的,很多元素的写法都可以用属性或者子元素2选1来实现 

Xml代码  

1. <javac srcdir="src" />  


也可以写成 

Xml代码  

1. <javac>  

2.     <src dir="src" />  

3. </javac>  


效果是一样,其他类似这样的情况还有很多,看多了就习惯了 

6、关于<path>和<fileset>的区别,<path>元素期待看到的是一个路径,而<fileset>期待看到的是一个文件集合,从这个角度来理解就可以了

 

 

0 0
原创粉丝点击