详见:http://ant.apache.org/manual/index.html
1.特点
大小写敏感;
不可改变,先到先得,谁先设定,之后的都不能改变。
如下的build.xml
<project name="MbopManageWeb" default="release" basedir=".."> <property name="releasedir" value="D:\anttest\mbop" /> <property name="releasedir" value="D:\anttest\manage" /> <echo>打包后发布路径:${releasedir}</echo> <target name="release"> <delete dir="${releasedir}" /> <mkdir dir="${releasedir}" /> </target> </project>
执行后控制台的打印信息如下所示:
Buildfile: D:\myworkspace\MbopManageWeb\build\build_test.xml [echo] 打包后发布路径:D:\anttest\mbop release: [delete] Deleting directory D:\anttest\mbop [mkdir] Created dir: D:\anttest\mbop BUILD SUCCESSFUL Total time: 172 milliseconds
“D:\anttest\mbop”,说明它的值一旦设定后,就无法再修改了。
2、property类似于变量,可以提供给build.xml中的其他元素使用
<property>元素可以跟很多种属性,各属性如下所示:
Attribute | Description | Required |
name | the name of the property to set. | No |
value | the value of the property. | 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 a <path> defined elsewhere.. |
No |
prefix | Prefix to apply to properties loaded using file , resource , 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 usingfile , resource , or url . Since 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}) |
3. 7种设置方式
1)、设置name和value属性值,比如:<property name="srcdir" value="${basedir}/src"/>
2)、设置name和refid属性值,比如:<property name="srcpath" refid="dao.compile.classpath"/>,其中dao.compile.classpath在别的地方定义。
3)、设置name和location属性值,比如:<property name="srcdir" location="src"/>,即将srcdir的值设置为:当前项目根目录的/src目录。
4)、设置file属性值,比如:<property file="build.properties"/>, 导入build.properties属性文件中的属性值
5)、设置resource属性值,比如:<propety resource="build.properties"/>,导入build.properties属性文件中的属性值
6)、设置url属性值,比如:<property url="http://www.mysite.com/bla/props/foo.properties"/>,导入http://www.mysite.com/bla/props/foo.properties属性文件中的属性值。
7)、设置环境变量,比如:<property environment="env"/>,设置系统的环境变量为前缀env.
<property name="tomcat.home" value="${env.CATALINA_HOME}"/> 将系统的tomcat安装目录设置到tomcat.home属性中。
Ant’s built-in properties:
basedir |
The absolute path of the project’s basedir. |
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. |
ant.project.default-target |
The name of the currently executing project’s default target. |
ant.project.invoked-targets |
A comma separated list of the targets that have been specified on the command line when invoking the current. |
ant.java.version |
The JVM version Ant detected. |
ant.core.lib |
The absolute path of the ant.jar file. |
内置属性basedir
-- 不需要定义就可以直接使用,${basedir},得到当前工程的绝对路径
-- 当在<project>标签的basedir属性中指定basedir时,之后工程中出现的所有相对路径都是相对于这个basedir所指向的路径,且${basedir}的值也将变为<project>标签中的basedir属性所指定的值。
5. 只有在声明了property之后,才能引用它,否则的话就会视为普通的字符串,比如 、
<mkdir dir="${build.classes.dir}" />
如果这个时候还没有声明build.classes.dir,那么就会视为${build.classes.dir}