Ant :Property

Ant :Property

 

 

properties是由key-value组成的汇集,就是Java中的Properties集合。属性的概念使用的是<property>。经常情状下,property值1经设置,就无法再变动了。Property是大局范围的properties中的3个成分,所以每一个property都是大局的,是足以被别的的target、task使用。

 

Ant内置的属性

系统天性

Ant对Java程序的系统本性做了支撑,可以一向的造访系统品质:

<project default="showSystemProperties">

    <target name="showSystemProperties">

       <echo>${java.version}</echo>

       <echo>${java.vendor}</echo>

       <echo>${java.vendor.url}</echo>

       <echo>${java.home}</echo>

       <echo>${java.vm.specification.version}</echo>

       <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>

 

Ant附加的习性

 

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 ant.jar file.

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

 

 

 

 

自定义Property

在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 ;).
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.

No

classpathref

the classpath to use when looking up a resource,

given as reference to
<path> defined elsewhere..

No

prefix

Prefix to apply to properties loaded using 

fileresource,
or url. A
“.” is appended to the prefix if not specified.

No

prefixValues

Whether to apply the prefix when expanding the right hand
side of

properties loaded using file,resource,
or urlSince Ant 1.8.2

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营造文件中,有四种方法能够安装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

 

 

二)钦点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)钦定UCR-VL,通过互联网能源来安顿

 

将下边包车型客车本子做如下调节就可以测试:

       <!–

       <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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关文章