Ant :Propertyhtml
properties是由key-value組成的集合,就是Java中的Properties集合。屬性的定義使用的是<property>。一般狀況下,property值一經設置,就不能再改變了。Property是全局範圍的properties中的一個元素,因此每一個property都是全局的,是能夠被任何的target、task使用。java
Ant對Java程序的系統屬性作了支持,能夠直接的訪問系統屬性:apache
<project default="showSystemProperties">網絡 <target name="showSystemProperties">app <echo>${java.version}</echo>ide <echo>${java.vendor}</echo>測試 <echo>${java.vendor.url}</echo>ui <echo>${java.home}</echo>this <echo>${java.vm.specification.version}</echo>url <echo>${java.vm.specification.vendor}</echo> <echo>${java.vm.specification.name}</echo> <echo>${java.vm.version}</echo> <echo>${java.vm.vendor}</echo> <echo>${java.vm.name}</echo> <echo>${java.specification.version}</echo> <echo>${java.specification.vendor}</echo> <echo>${java.specification.name}</echo> <echo>${java.class.version}</echo> <echo>${java.class.path}</echo> <echo>${java.library.path}</echo> <echo>${java.io.tmpdir}</echo> <echo>${java.compiler}</echo> <echo>${java.ext.dirs}</echo> <echo>${os.name}</echo> <echo>${os.arch}</echo> <echo>${os.version}</echo> <echo>${file.separator}</echo> <echo>${path.separator}</echo> <echo>${line.separator}</echo> <echo>${user.name}</echo> <echo>${user.home}</echo> <echo>${user.dir}</echo> </target> </project> |
basedir the absolute path of the project's basedir (as set
with the basedir attribute of <project>).
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;
it is set in the name attribute of <project>.
ant.project.default-target
the name of the currently executing project's
default target; it is set via the default
attribute of <project>.
ant.project.invoked-targets
a comma separated list of the targets that have
been specified on the command line (the IDE,
an <ant> task ...) when invoking the current
project.
This property is set properly when the first target is executed.
If you use it in the implicit target (directly
under the <project> tag) the list will be
empty if no target has been specified while it
will contain the project's default target in this
case for tasks nested into targets..
ant.java.version the JVM version Ant detected; currently it can hold
the values "1.9", "1.8",
"1.7", "1.6", "1.5",
"1.4", "1.3" and
"1.2". ant.core.lib the absolute path
of the file.ant.jar
ant.home home directory of Ant
ant.library.dir the directory that has been used to load Ant's
jars from. In most cases this is ANT_HOME/lib.
<target name="showAntBuildInProperties"> <echo>${basedir}</echo> <echo>${ant.file}</echo> <echo>${ant.version}</echo> <echo>${ant.project.name}</echo> <echo>${ant.project.default-target}</echo> <echo>${ant.project.invoked-targets}</echo> <echo>${ant.java.version}</echo> <echo>${ant.home}</echo> <echo>${ant.library.dir}</echo> </target> |
執行結果:
showAntBuildInProperties: [echo] D:\Ant_Test\task [echo] D:\Ant_Test\task\build.xml [echo] Apache Ant(TM) version 1.9.4 compiled on April 29 2014 [echo] ${ant.project.name} [echo] showAntBuildInProperties [echo] showAntBuildInProperties [echo] 1.7 [echo] E:\Program Files\apache\ant\apache-ant-1.9.4 [echo] E:\Program Files\apache\ant\apache-ant-1.9.4\lib |
在build.xml中可使用<property>來自定義屬性。屬性一經設置,將不可改變。
共有7種方式能夠設置屬性:
·
Attribute |
Description |
Required |
name |
屬性名 |
No |
value |
屬性值 |
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 toPATH like structures or properties. |
|
resource |
the name of the classpath resource containing properties settings in properties file format. |
One of these, whennot 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 ;). 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. |
No |
classpathref |
the classpath to use when looking up a resource, given as reference to a |
No |
prefix |
Prefix to apply to properties loaded using
|
No |
prefixValues |
Whether to apply the prefix when expanding the right hand side of properties loaded using |
No (default=false) |
relative |
If set to true the relative path to basedir is set. Since Ant 1.8.0 |
No (default=false) |
basedir |
The basedir to calculate the relative path from. Since Ant 1.8.0 |
No (default=${basedir}) |
Property實際上是一種特殊的task,它的做用就是爲變量設置值,也能夠理解爲定義變量。可是我沒有將其放在task一節中,是由於它是一種提供值的經常使用方式。
在Ant構建文件中,有7種方式能夠設置property。
1)指定name,以及value或者location的方式
<property name=」xxx」 value=」」 />或者<property name=」xxx」 location=」」 />
若是是value,則是直接設置爲字面量。若是是location,則表示該值是一個文件系統上的一個路徑,能夠設置爲絕對路徑,也能夠設置爲相對路徑。
示例:
<project default="main">
<target name="main" depends="showSimpleProperty" />
<property name="test.ant.property.useValue" value="hello, ant, property, value" /> <property name="test.ant.property.useLocation.absoulte" location="c:/hello"/> <property name="test.ant.property.useLocation.relative" location="."/>
<target name="showSimpleProperty"> <echo>${test.ant.property.useValue}</echo> <echo>${test.ant.property.useLocation.absoulte}</echo> <echo>${test.ant.property.useLocation.relative}</echo> </target>
</project> |
結果:
showSimpleProperty: [echo] hello, ant, property, value [echo] c:\hello [echo] D:\Projects\ant_test\property
main:
BUILD SUCCESSFUL Total time: 0 seconds |
2)指定name並以及嵌入文本的方式
<property name=」xxx」>your text value</property>
3)指定name,refid
示例:
<property id="test.ant.property.text" name="test.ant.property.text">your text value</property> <target name="showTextProperty"> <echo>${test.ant.property.text}</echo> </target>
<property name="test.ant.property.refid" refid="test.ant.property.text" /> <target name="showRefidProperty"> <echo>${test.ant.property.refid}</echo> </target> |
以前已提到過,每一個task都有一個id,property是一種特殊的task,因此也能夠爲它設置id屬性。
4)指定file屬性來設置多個Property
Java中兩種文件一般做爲配置文件:xml、properties。Ant也對properties文件提供了支持。
屬性文件能夠放在本地文件系統中,能夠是網絡上某個主機裏,也能夠是在當前classpath下。針對這幾種狀況,ant都作了支持。分別經過指定file,url,resource來加載屬性文件。
另外,在使用這三種方式(file,url,resource)時,能夠指定前綴prefix的。默認狀況下是沒有前綴的,也就是,定義的變量仍然屬性文件中的變量名。若是指定了前綴,就是定義了這樣的變量(前綴.屬性名)。
file屬性是指定本地文件的路徑,能夠是絕對路徑,也能夠是相對路徑
實例:
屬性文件以下:
test.ant.property.propertiesFile.A=hello a test.ant.property.propertiesFile.B=hello b test.ant.property.propertiesFile.C=hello c test.ant.property.propertiesFile.D=hello d test.ant.property.propertiesFile.E=hello e |
使用<property file/>設置屬性:
<property file="./build.properties" /> <target name="showFileProperty"> <echo>${test.ant.property.propertiesFile.A}</echo> <echo>${test.ant.property.propertiesFile.B}</echo> <echo>${test.ant.property.propertiesFile.C}</echo> <echo>${test.ant.property.propertiesFile.D}</echo> </target> |
結果以下:
showFileProperty: [echo] hello a [echo] hello b [echo] hello c [echo] hello d
|
5)指定URL,經過網絡資源來配置
將上面的腳本作以下調整便可測試:
<!-- <property file="./build.properties" /> --> <target name="showFileProperty"> <echo>${test.ant.property.propertiesFile.A}</echo> <echo>${test.ant.property.propertiesFile.B}</echo> <echo>${test.ant.property.propertiesFile.C}</echo> <echo>${test.ant.property.propertiesFile.D}</echo> </target>
<property url="file:///D:/Projects/ant_test/property/build.properties" /> <target name="showUrlProperty" depends="showFileProperty" /> |
6)指定resource ,加載classpath下屬性文件
若是屬性文件放在指定的classpath下,能夠經過使用resource來指定屬性文件位置。
默認是當前路徑下。也就是說在使用resource時,若是指定了classpath屬性,則從指定的classpath下加載,若是沒有指定,則從當前classpath下加載。
根據上面的說明,能夠知道resource是和classpath(或者classpathref)結合使用的。
因此使用resource時,能夠是下面三種形式:
<property resource=」xxxx」 /> <property resource=」xxxx」 classpath=」yyyy」 /> <property resource=」xxxx」 classpathref=」yyyy」 /> |
7)將環境變量做爲property使用
使用方法是:<property environment=」env」 />
這樣就是將環境變量做爲屬性加載的。加載的變量名是:env.XXX。例如環境變量中的PATH、JAVA_HOME,加載後的變量名是:env.JAVA_HOME,env.PATH。
<property environment="env"/> <target name="showEnvironmentProperty"> <echo>${env.JAVA_HOME}</echo> </target> |