Ant 之property 總結


原文:

http://chen592969029.iteye.com/blog/946056

特點

 大小寫敏感;

 不可改變,先到先得,誰先設定,之后的都不能改變。

 

怎樣設置

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 屬性中。

 

內置屬性

 

Ant’s built-in properties:

basedir

The absolute path of the project’s basedir.

ant.file

The absolute path of the buildfile.

ant.version

The version of Ant.

ant.project.name

The name of the project that is currently executing.

ant.project.default-target

The name of the currently executing project’s default target.

ant.project.invoked-targets

A comma separated list of the targets that have been specified on the command line when invoking the current.

ant.java.version

The JVM version Ant detected.

ant.core.lib

The absolute path of the ant.jar file.

 

System properties  

java.version

Java Runtime Environment version

java.vendor

Java Runtime Environment vendor

java.vendor.url

Java vendor URL

java.home

Java installation directory

java.vm.specification.version

Java Virtual Machine specification version

java.vm.specification.vendor

Java Virtual Machine specification vendor

java.vm.specification.name

Java Virtual Machine specification name

java.vm.version

Java Virtual Machine implementation version

java.vm.vendor

Java Virtual Machine implementation vendor

java.vm.name

Java Virtual Machine implementation name

java.specification.version

Java Runtime Environment specification version

java.specification.vendor

Java Runtime Environment specification vendor

java.specification.name

Java Runtime Environment specification name

java.class.version

Java class format version number

java.class.path        

Java class path

java.library.path

List of paths to search when loading libraries

java.io.tmpdir

Default temp file path

java.compiler

Name of JIT compiler to use

java.ext.dirs

Path of extension directory or directories

os.name

Operating system name

os.arch

Operating system architecture

os.version

Operating system version

file.separator

File separator ("/" on UNIX)

path.separator

Path separator (":" on UNIX)

line.separator

Line separator ("\n" on UNIX)

user.name

User's account name

user.home

User's home directory

user.dir

User's current working directory

 

用法

${key_name},如:${os.name},它將得到當前操作系統的名稱。

 

需注意

1. 內置屬性basedir

 -- 不需要定義就可以直接使用,${basedir},得到當前工程的絕對路徑

 -- 當在<project>標簽的basedir屬性中指定basedir時,之后工程中出現的所有相對路徑都是相對於這個basedir所指向的路徑,且${basedir}的值也將變為<project>標簽中的basedir屬性所指定的值。

 

2. property的不變性在使用<available><ant><antcall>時會被打破

 

3. 可以在命令行通過-DpropertyName=propertyValue的方式指定property,注意,-D於propertyName之間沒有空格,使用這種方式指定的屬性最先被賦值,它是在執行build文件之前就已經賦值了的。

 

Q&A

 

How can I do something like  <property name="prop" value="${${anotherprop}}"/>   (double expanding the property)?

Without any external help you can not.

With <script/>, which needs external libraries, you can do

 

Xml代碼   
  1. <script language="javascript">  
  2.     propname = project.getProperty("anotherprop");  
  3.     project.setNewProperty("prop", propname);  
  4. </script>  

 

With AntContrib (external task library) you can do <propertycopy name="prop" from="${anotherprop}"/> .

With Ant 1.6 you can simulate the AntContribs <propertycopy> and avoid the need of an external library:

 

Xml代碼   
  1. <macrodef name="propertycopy">  
  2.   <attribute name="name"/>  
  3.   <attribute name="from"/>  
  4.   <sequential>  
  5.     <property name="@{name}" value="${@{from}}"/>  
  6.   </sequential>  
  7. </macrodef>   

 

With the 'props' antlib (external, but also from Ant) you could do the dereferencing with  ${${anotherprop}   - not just in the property task - instead everywhere in your buildfile (after registering the required property helper).

 

Xml代碼   
  1. <propertyhelper>  
  2.   <props:nested />  
  3. </propertyhelper>  
  4. <property name="foo" value="foo.value" />  
  5. <property name="var" value="foo" />  
  6. <echo> ${${var}} = foo.value </echo>   

 

With  Flaka   (external Ant Plugin) you could do the dereferencing with  #{${anotherprop}}   - not just in flaka tasks, but all tasks after installing flaka's property handler.

 

Xml代碼   
  1. <project xmlns:fl="antlib:it.haefelinger.flaka">  
  2.   <fl:install-property-handler/>  
  3.   <property name="foo" value="foo.value"/>  
  4.   <property name="var" value="foo" />  
  5.   <property name="buildtype" value="test"/>  
  6.   <property name="appserv_test" value="//testserver"/>  
  7.   <echo>  
  8.     #{${var}} = foo.value  
  9.     <!-- nested property -->  
  10.     #{appserv_${buildtype}}  
  11.   </echo>  
  12. </project>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM