Ant :Property
- Property
Ant :Property
properties是由key-value组成的集合,就是Java中的Properties集合。属性的定义使用的是<property>。通常情况下,property值一经设置,就不能再改变了。Property是全局范围的properties中的一个元素,所以每个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 ;). even if the environment e.g. Windows 2000's system 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 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 |
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 <property <property <property <target <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, [echo] c:\hello [echo] main: BUILD SUCCESSFUL Total time: 0 seconds |
2)指定name并以及嵌入文本的方式
<property name=”xxx”>your text
value</property>
3)指定name,refid
示例:
|
<property name="test.ant.property.text">your <target <echo>${test.ant.property.text}</echo> </target> <property <target <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 test.ant.property.propertiesFile.B=hello test.ant.property.propertiesFile.C=hello test.ant.property.propertiesFile.D=hello test.ant.property.propertiesFile.E=hello |
使用<property
file/>设置属性:
|
<property <target <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 --> <target <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 <target |
6)指定resource ,加载classpath下属性文件
如果属性文件放在指定的classpath下,可以通过使用resource来指定属性文件位置。
默认是当前路径下。也就是说在使用resource时,如果指定了classpath属性,则从指定的classpath下加载,如果没有指定,则从当前classpath下加载。
根据上面的说明,可以知道resource是和classpath(或者classpathref)结合使用的。
所以使用resource时,可以是下面三种形式:
|
<property resource=”xxxx” /> <property resource=”xxxx” classpath=”yyyy” <property resource=”xxxx” |
7)将环境变量作为property使用
使用方法是:<property
environment=”env” />
这样就是将环境变量作为属性加载的。加载的变量名是:env.XXX。例如环境变量中的PATH、JAVA_HOME,加载后的变量名是:env.JAVA_HOME,env.PATH。
|
<property <target <echo>${env.JAVA_HOME}</echo> </target> |
Ant :Property的更多相关文章
- Cesium原理篇:Property
之前主要是Entity的一个大概流程,本文主要介绍Cesium的属性,比如defineProperties,Property(ConstantProperty,CallbackProperty,Con ...
- python面对对象编程-------5:获取属性的四种办法:@property, __setattr__(__getattr__) ,descriptor
一:最基本的属性操作 class Generic: pass g= Generic() >>> g.attribute= "value" #创建属性并赋值 > ...
- OC中两个关键字的作用:@property和@synthesize
两个关键字的使用:@property和@synthesize 一.@property关键字这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题 ...
- python对象属性管理(2):property管理属性
使用Property管理属性 python提供了一种友好的getter.setter.deleter类方法的属性管理工具:property. property()是一个内置函数,它返回一个Proper ...
- 搭建Turbine时,报错误:Property or field 'default' cannot be found on object of type 'com.netflix.appinfo.InstanceInfo'
Spring Boot + Eureka Server + Hystrix with Turbine: empty turbine.stream 配置的时候遇到了问题: Property or fie ...
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- Python进阶:@property 动态属性
Python进阶:@property 动态属性 Python 动态属性的概念可能会被面试问到,在项目当中也非常实用,但是在一般的编程教程中不会提到,可以进修一下. 先看一个简单的例子.创建一个 Stu ...
- spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 无法启动 ...
- 第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter
上节详细介绍了利用@property装饰器定义属性的语法,本节通过具体案例来进一步说明. 一. 案例说明 本节的案例是定义Rectangle(长方形)类,为了说明问题,除构造函数外,其他方法都只 ...
随机推荐
- WebService入门案例
关于WebService的作用和好处,大家应该都了解.但如何在Asp.Net中添加Web Service等问题一直是新手朋友的一大难题.鉴于网上没有一个像样的实际案例,特将课程设计中运用到的WebSe ...
- jQuery-1.9.1源码分析系列(二)jQuery选择器续1
在分析之前说一点题外话. ownerDocument和 documentElement的区别 ownerDocument是Node对象的一个属性,返回的是某个元素的根节点文档对象:即document对 ...
- jQuery-1.9.1源码分析系列(十) 事件系统——事件包装
首先需要明白,浏览器的原生事件是只读的,限制了jQuery对他的操作.举个简单的例子就能明白为什么jQuery非要构造一个新的事件对象. 在委托处理中,a节点委托b节点在a被click的时候执行fn函 ...
- iOS 商品倒计时 限时特价 限时优惠 功能的封装
最近项目中多个页面用到了 商品特价倒计时的功能 为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下: 定向价 限时特价 模型代码实现: #pragma mark 商品定向价模型 @ ...
- linux中的通配符与正则表达式
在linux中,有通配符及正则表达式,那么什么是通配符和正则表达式,什么时候用? 通配符 它是由shell解析,并且一般用于匹配文件名,实际上就是shell解释器去解析的特殊符号,linux系统通 ...
- 关于C#的继承结论
1.继承:继承是一个类可以使用另一个类的属性和方法(成员) 2.子类继承父类使用的符号为“ : ”(冒号) 3.子类可以继承父类私有成员,但不能访问(不能使用) 4.父类的构造,子类不能被继承 5. ...
- JavaScript中的匿名函数及函数的闭包
1.匿名函数 函数是JavaScript中最灵活的一种对象,这里只是讲解其匿名函数的用途.匿名函数:就是没有函数名的函数. 1.1 函数的定义,首先简单介绍一下函数的定义,大致可分为三种方式 第一种: ...
- jQuery动画
一.显示和隐藏 hide().show() 1.show():显示被选的元素 2.hide():隐藏被选的元素 3.toggle():对被选元素进行隐藏和显示的切换 语法: $(selector).h ...
- Git GUI下载远程源代码简单使用介绍
学习编程,适当的时候弄些开源项目的源代码来学习实践,对技术是很有帮助的.如果学习.NET.C#的话,codeplex上面有许多不错的源代码,推荐一个ASP.ENT的:HoverTree http:// ...
- Win10计算器在哪里?三种可以打开Win10计算器的方法图文介绍
全新的windows10系统带来了不少新的特性和改变,其中win10的计算器位置就发生了很多的变化,导致很多网友们都以为win10计算器不见了,那么,win10计算器在哪里?如何打开?针对此问题,本文 ...