ant property 总结

特点

大小写敏感;

不可改变,先到先得,谁先设定,之后的都不能改变。

使用Ant运行tomcat

Ant使用<exec>任务运行本地程序,先看一个例子:

<?xml version="1.0"?>
<project name="project" default="run">
 <target name="run">
  <exec executable="cmd">
   <arg value="/C a.bat"/>
  </exec>
 </target>
</project>

a.bat

@echo off
echo Hello >> a.txt

运行完后,会在根目录生成a.txt文件,里面内容为Hello

下面我们来运行tomcat

<?xml version="1.0"?>
<project name="project" default="tomcat-start">
 <property name="tomcat.dir" value="c:/Tomcat5"></property>
 
 <target name="tomcat-start">
  <exec dir="${tomcat.dir}/bin" executable="cmd">
   <env key="CATALINA_HOME" path="${tomcat.dir}"/>
   <arg value="/C startup.bat"/>
  </exec>
 </target>
 <target name="tomcat-stop">
  <exec dir="${tomcat.dir}/bin" executable="cmd">
   <env key="CATALINA_HOME" path="${tomcat.dir}"/>
   <arg value="/c shutdown.bat"/>
  </exec>
 </target>
</project>

成功!!

fix yourself in right way,fix the world in right codes

怎样设置

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.blogjava.net/wiflish/build.properties"/>,导入http://www.blogjava.net/wiflish/build.properties属性文件中的属性值。

7、设置环境变量,比如:<property environment="env"/>,设置系统的环境变量为前缀env.

<property name="tomcat.home" value="${env.CATALINA_HOME}"/>将系统的tomcat安装目录设置到 tomcat.home属性中。


从特性文件.propeties中载入特性:

    特性文件格式:name=value.如build.debug=off.

在构建文件中增加任务:

<target name="override">
    <property file="build.properties"/>
    <property name="build.debug" value="on" />
    <echo message="debugging is turned ${build.debug}" />
   </target>

内置属性

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.

System properties

java.version

Java Runtime Environment version

java.vendor

Java Runtime Environment vendor

java.vendor.url

Java vendor URL

java.home

Java installation directory

java.vm.specification.version

Java Virtual Machine specification version

java.vm.specification.vendor

Java Virtual Machine specification vendor

java.vm.specification.name

Java Virtual Machine specification name

java.vm.version

Java Virtual Machine implementation version

java.vm.vendor

Java Virtual Machine implementation vendor

java.vm.name

Java Virtual Machine implementation name

java.specification.version

Java Runtime Environment specification version

java.specification.vendor

Java Runtime Environment specification vendor

java.specification.name

Java Runtime Environment specification name

java.class.version

Java class format version number

java.class.path

Java class path

java.library.path

List of paths to search when loading libraries

java.io.tmpdir

Default temp file path

java.compiler

Name of JIT compiler to use

java.ext.dirs

Path of extension directory or directories

os.name

Operating system name

os.arch

Operating system architecture

os.version

Operating system version

file.separator

File separator ("/" on UNIX)

path.separator

Path separator (":" on UNIX)

line.separator

Line separator ("\n" on UNIX)

user.name

User's account name

user.home

User's home directory

user.dir

User's current working directory

用法

${key_name},如:${os.name},它将得到当前操作系统的名称。

需注意

1. 内置属性basedir

-- 不需要定义就可以直接使用,${basedir},得到当前工程的绝对路径

-- 当在<project>标签的basedir属性中指定basedir时,之后工程中出现的所有相对路径都是相对于这个basedir所指向的路径,且${basedir}的值也将变为<project>标签中的basedir属性所指定的值。

2. property的不变性在使用<available><ant><antcall>时会被打破

3. 可以在命令行通过-DpropertyName=propertyValue的方式指定property,注意,-D于propertyName之间没有空格,使用这种方式指定的属性最先被赋值,它是在执行build文件之前就已经赋值了的。

Q&A

How can I do something like<property name="prop" value="${${anotherprop}}"/>(double expanding the property)?

Without any external help you can not.

With <script/>, which needs external libraries, you can do

Xml代码 收藏代码
  1. <scriptlanguage="javascript">
  2. propname=project.getProperty("anotherprop");
  3. project.setNewProperty("prop",propname);
  4. </script>

With AntContrib (external task library) you can do<propertycopy name="prop" from="${anotherprop}"/>.

With Ant 1.6 you can simulate the AntContribs <propertycopy> and avoid the need of an external library:

Xml代码 收藏代码
  1. <macrodefname="propertycopy">
  2. <attributename="name"/>
  3. <attributename="from"/>
  4. <sequential>
  5. <propertyname="@{name}"value="${@{from}}"/>
  6. </sequential>
  7. </macrodef>

With the 'props' antlib (external, but also from Ant) you could do the dereferencing with${${anotherprop}- not just in the property task - instead everywhere in your buildfile (after registering the required property helper).

Xml代码 收藏代码
  1. <propertyhelper>
  2. <props:nested/>
  3. </propertyhelper>
  4. <propertyname="foo"value="foo.value"/>
  5. <propertyname="var"value="foo"/>
  6. <echo>${${var}}=foo.value</echo>

WithFlaka(external Ant Plugin) you could do the dereferencing with#{${anotherprop}}- not just in flaka tasks, but all tasks after installing flaka's property handler.

Xml代码 收藏代码
  1. <projectxmlns:fl="antlib:it.haefelinger.flaka">
  2. <fl:install-property-handler/>
  3. <propertyname="foo"value="foo.value"/>
  4. <propertyname="var"value="foo"/>
  5. <propertyname="buildtype"value="test"/>
  6. <propertyname="appserv_test"value="//testserver"/>
  7. <echo>
  8. #{${var}}=foo.value
  9. <!--nestedproperty-->
  10. #{appserv_${buildtype}}
  11. </echo>
  12. </project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值