property特性设置有六种方式apache
一、直接赋值name,value直接赋值,赋值格式k/v,键值对的方式,看以下操做vim
<property name="name2.1" value="property"/>网络
事例:dom
[huang@aliyun_test test]$ vim property.xml ide
<project name="echo" default="A">ui
<property name="name2.1" value="property"/>url
<target name="A">xml
<echo message="${name2.1}"/>作用域
</target>get
</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
二、由文件路径相关设置特性
[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
三、直接导入的文件内容格式
建立一个文件以键值对的形式存入数据,以下
[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
四、从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
五、从网络文件中读取
<property url="http://www.baiud.com/file.property"/>
特性支持直接从网络文件中导入。网络文件的格式要求与从本地文件中导入读取的要求相同
文件内容亦都是以<K,V>形式存在,对字符集亦有一样要求。设置特性中”url”指定网络文件连接便可。
六、获取主机名,命令方式获取
<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