<echo message="ant.file = ${ant.file}"/>
<echo message="ant.home = ${ant.home}"/>
<echo message="ant.java.version = ${ant.java.version}"/>
<echo message="ant.project.name = ${ant.project.name}"/>
<echo message="ant.version = ${ant.version}"/>
<echo message="basedir = ${basedir}"/>
</target>
<echo message="user.home = ${user.home}"/>
<echo message="java.home = ${java.home}"/>
[echo] user.home = C:\Documents and Settings\lcheng
user=admin
password=qqqqq
<?xml version="1.0" encoding="UTF-8"?> <project name="testCondition" default="test"> <property file="build.properties"/> <target name="test"> <echo message="name = ${name}"/> <echo message="user = ${user}"/> <echo message="password = ${password}"/> </target> </project>
运行,结果如下:
C:\testant>ant -f build1.xml
Buildfile: C:\testant\build1.xml
test:
[echo] name = Allen
[echo] user = admin
[echo] password = qqqqq
BUILD SUCCESSFUL
Total time: 0 seconds
<?xml version="1.0" encoding="UTF-8"?> <project name="testCondition" default="test"> <property environment="env" /> <target name="test"> <echo message="env.Path = ${env.Path}"/> <echo message="env.JAVA_HOME = ${env.JAVA_HOME}"/> </target> </project>
结果:
C:\testant>ant -f build1.xml
Buildfile: C:\testant\build1.xml
test:
[echo] env.Path = C:\Program Files (x86)\Common Files\NetSarang; C:\Program Files (x86)\Common Files\Microsoft Shared\Windows;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program File;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files (x86)\Windows
[echo] env.JAVA_HOME = C:\Program Files\Java\jdk1.6.0_21
BUILD SUCCESSFUL
Total time: 0 seconds
<?xml version="1.0"?> <customer name="zhangsan"> <sex>female</sex> <age>27</age> </customer>
<?xml version="1.0" encoding="UTF-8"?> <project name="testCondition"> <property environment="env"/> <target name="test"> <xmlproperty file="${customer}.xml" collapseAttributes="true"/> <echo message="${customer.name},${customer.sex},${customer.age}"/> </target> </project>
Buildfile: C:\testant\build1.xml
test:
[echo] zhangsan,female,27
BUILD SUCCESSFUL
Total time: 0 seconds
<?xml version="1.0"?> <customer name="zhangsan"> <sex>female</sex> <age>27</age> <sex>male</sex> <age>32</age> </customer>
则结果变为
C:\testant>ant -f build1.xml test -Dcustomer=web
Buildfile: C:\testant\build1.xml
test:
[echo] zhangsan,female,male,27,32
BUILD SUCCESSFUL
Total time: 0 seconds
<?xml version="1.0"?> <customers> <customer name="zhangsan"> <sex>female</sex> <age>27</age> </customer> <customer name="lisi"> <sex>male</sex> <age>32</age> </customer> </customers>
build1.xml 改为
<?xml version="1.0" encoding="UTF-8"?> <project name="testCondition"> <property environment="env"/> <target name="test"> <xmlproperty file="${customer}.xml" collapseAttributes="true"/> <echo message="${customers.customer.name}, ${customers.customer.sex}, ${customers.customer.age}"/> </target> </project>
运行:
ant -f build1.xml test -Dcustomer=web
结果:
C:\testant>ant -f build1.xml test -Dcustomer=web
Buildfile: C:\testant\build1.xml
test:
[echo] zhangsan,lisi, female,male, 27,32
BUILD SUCCESSFUL
Total time: 0 seconds