property特性设置有六种方式

1、直接赋值name,value直接赋值,赋值格式k/v,键值对的方式,看如下操作

<property name="name2.1" value="property"/>

事例:

[huang@aliyun_test test]$ vim property.xml 

<project name="echo" default="A">

 <property name="name2.1" value="property"/>

 <target name="A">

  <echo message="${name2.1}"/>

 </target>

</project>

运行结果如下:

[root@aliyun_test apache-ant-1.9.7]# bin/ant A -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] property       

BUILD SUCCESSFUL

Total time: 0 second


2、由文件路径相关设置特性

[huang@aliyun_test test]$ vim property.xml

<project name="echo" default="A">

 <property name="name2.1" location="example.xml" relative="true" basedir="/home/huang/test"/>

 <target name="A">

  <echo message="${name2.1}"/>

 </target>

</project>

运行结果如下:

[root@aliyun_test apache-ant-1.9.7]# bin/ant A -f /home/huang/test/property.xml   

Buildfile: /home/huang/test/property.xml

A:

     [echo] example.xml

BUILD SUCCESSFUL

Total time: 0 seconds


3、直接导入的文件内容格式

创建一个文件以键值对的形式存入数据,如下

[huang@aliyun_test test]$ cat file.property 

domain_home=/home/huang/test

xingming=xiaobai

然后创建xml文件

[huang@aliyun_test test]$ cat property.xml 

<project name="echo" default="A">

 <property file="/home/huang/test/file.property"/>

 <target name="A">

  <echo message="${xingming}"/>

 </target>

</project>

然后ant运行如下结果:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] xiaobai        这里显示的就是定义文件的property

BUILD SUCCESSFUL

Total time: 0 seconds


4、从jar中导入特性配置文件,此方法是调用系统环境变量

<property environment="env"/>  

<echo message="${env.OS}"/>

创建xml文件如下

[huang@aliyun_test test]$ vim property.xml 

<project name="echo" default="A">

 <property environment="env"/>

 <target name="A">

  <echo message="${env.USER}"/>     由echo $USER了解到此时用户为huang

 </target>

</project>

执行结果如下:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] huang         打印出当前系统的登录用户,这是系统环境变量

BUILD SUCCESSFUL

Total time: 0 seconds


5、从网络文件中读取

<property url="http://www.baiud.com/file.property"/>

特性支持直接从网络文件中导入。网络文件的格式要求与从本地文件中导入读取的要求相同

文件内容亦都是以<K,V>形式存在,对字符集亦有同样要求。设置特性中”url”指定网络文件链接即可。


6、获取主机名,命令方式获取

<exec executable="hostname" outputproperty="name5.host.name"  

 errorproperty=”name5.error”/>  

<echo message="${name5.host.name}"/>

通过exec任务执行本地命令”hostname”来获取设置的主机名,并将该命令正常运行的输出结果,即本设置的主机名,

将会赋值到特性”name5.host.name”中去,如果找不到该本地命令或者命令执行过程中出错,则会将出错内容提法赋值到”name5.error”中去。

创建xml文件如下:

[huang@aliyun_test test]$ vim property.xml 

<project name="echo" default="A">

 <exec executable="hostname" outputproperty="name.a" errorproperty="name.b"/>    此时特性property不再定义

 <target name="A">

  <echo message="${name.a}"/>

 </target>

</project>

运行结果如下:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] aliyun_test

BUILD SUCCESSFUL

Total time: 0 seconds

综合一个xml事例:

[huang@aliyun_test test]$ vim example.xml 

<project name="example" default="A">

 <property name="xingming" value="xiaobai"/>

 <property file="/home/huang/test/file.property"/>

 <property environment="env"/>

 <exec executable="getenforce" outputproperty="name" errorproperty="error"/>

 <target name="A">

  <echo message="${xingming}" />

  <echo message="${domain_home}"/>

  <echo message="${env.USER}"/>

  <echo message="${name}"/>

 </target>

</project>

运行如下:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/example.xml 

Buildfile: /home/huang/test/example.xml


A:

     [echo] xiaobai

     [echo] /home/huang/test

     [echo] huang

     [echo] Disabled


BUILD SUCCESSFUL

Total time: 0 seconds


######关于property特性还有以下内容

特性(property)在声明被赋值之后,其值是不可变的,在整个编译文件中将视为常量而非变量。

<property name="color" value="blue"/>  

<property name="color" value="red"/>  

<!--特性的不可变性,此处将打印出"color"的第一次赋值"blue"-->  

<echo message="${color}"/> 

事例:

[huang@aliyun_test test]$ vim property.xml 

<project name="echo" default="A">

 <property name="color" value="red"/>

 <property name="color" value="bule"/>

 <target name="A">

  <echo message="${color}"/>

 </target>

</project>

运行结果如下:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] red         这里显示的是第一次声明,不再是后面的声明替代

BUILD SUCCESSFUL

Total time: 0 seconds


###特性的作用域,特性可以作用在全局环境中,也可以作用在局部

在工程级元素(project level,即<project>元素内的XML级)的第一级特性(property)具有作用域是全局的,在构建文件中一直都有效

在目标(target)标签下声明的特性在当前目标target内有效,并且其作用域延续到之后运行的其它目标(target)内。

事例:

[huang@aliyun_test test]$ cat property.xml

<project name="echo" default="A">

 <property name="color" value="red"/>     此为全局变量color

 <target name="A">

  <property name="color" value="bule"/>   这里为局部变量color

  <echo message="${color}"/>

 </target>

 <target name ="B">

  <echo message="${color}"/>

 </target>

</project>

运行结果如下:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant A B -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] red

B:

     [echo] red

BUILD SUCCESSFUL

Total time: 0 seconds


target目标里面定义的property也可以作用在其他target中,看事例

[huang@aliyun_test test]$ cat property.xml 

<project name="echo" default="A">

 <property name="color" value="red"/>       定义的全局变量color--》red

 <target name="A">

  <property name="a.color" value="bule"/>     在target A中定义的property的a.color

  <echo message="${color}"/>

 </target>

 <target name ="B">

  <echo message="${a.color}"/>            能否作用在target B中?

 </target>

</project>

运行结果如下:

[huang@aliyun_test apache-ant-1.9.7]$ bin/ant A B -f /home/huang/test/property.xml 

Buildfile: /home/huang/test/property.xml

A:

     [echo] red

B:

     [echo] bule                 这里显示的也是target A定义的property的值

BUILD SUCCESSFUL

Total time: 0 seconds