`

Ant Property

    博客分类:
  • Ant
 
阅读更多

 

Ant property(特征)

 

        Ant property表示任何一类可用字符串表达的内容,其并不是构建过程中共享信息的关键,更是使用者从外部控制Ant的重要手段。

        Ant propertyg一旦设置,在整个构建中固定不变。

 

    1.  常用Ant内置特征

ant.file 构建文件的绝对路径
ant.home 用于Ant执行的根目录。某些IDE不对此进行设置
ant.java.version Ant检测的JVM版本。
ant.project.name 当前执行项目的名称,可用<project>中的name属性进行设置
ant.version Ant的版本号
basedir 项目基目录的绝对路径,其他相对路径都是由它解析而出的。

 

 

2. 可以用<echoproperties/>打印全部特征集。

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">
	
	<target name="echoall" depends="echoxml">
		<echoproperties/>
	</target>

	<target name="echoxml">
		<echoproperties format="xml" destfile="properties.xml"/>
	</target>
	
</project>

     输出的properties.xml文件中的内容:

<?xml version="1.0" encoding="UTF-8"?><properties>
	<property name="ant.core.lib" value="D:\dev\ant\ant1.8.4\lib\ant.jar" />
	<property name="ant.file" value="e:\antspace\property\build.xml" />
	<property name="ant.file.property" value="e:\antspace\property\build.xml" />
	<property name="ant.file.type" value="file" />
	<property name="ant.file.type.property" value="file" />
	<property name="ant.home" value="D:\dev\ant\ant1.8.4" />
	<property name="ant.java.version" value="1.6" />
	<property name="ant.library.dir" value="D:\dev\ant\ant1.8.4\lib" />
	<property name="ant.project.default-target" value="echoall" />
	<property name="ant.project.invoked-targets" value="echoall" />
	<property name="ant.project.name" value="property" />
	<property name="ant.version" value="Apache Ant(TM) version 1.8.4 compiled on May 22 2012" />
	<property name="awt.toolkit" value="sun.awt.windows.WToolkit" />
	<property name="basedir" value="e:\antspace\property" />
	<property name="file.encoding" value="GBK" />
	<property name="file.encoding.pkg" value="sun.io" />
	<property name="file.separator" value="\" />
	<property name="java.awt.graphicsenv" value="sun.awt.Win32GraphicsEnvironment" />
	<property name="java.awt.printerjob" value="sun.awt.windows.WPrinterJob" />
	<property name="java.class.path" value="D:\dev\ant\ant1.8.4\lib\ant-launcher.jar;e:\antspace\property\.\;D:\dev\java\jdk16027\lib\dt.jar;D:\dev\java\jdk16027\lib\tools.jar;D:\dev\ant\ant1.8.4\lib\ant-antlr.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-bcel.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-bsf.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-log4j.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-oro.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-regexp.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-resolver.jar;D:\dev\ant\ant1.8.4\lib\ant-apache-xalan2.jar;D:\dev\ant\ant1.8.4\lib\ant-commons-logging.jar;D:\dev\ant\ant1.8.4\lib\ant-commons-net.jar;D:\dev\ant\ant1.8.4\lib\ant-jai.jar;D:\dev\ant\ant1.8.4\lib\ant-javamail.jar;D:\dev\ant\ant1.8.4\lib\ant-jdepend.jar;D:\dev\ant\ant1.8.4\lib\ant-jmf.jar;D:\dev\ant\ant1.8.4\lib\ant-jsch.jar;D:\dev\ant\ant1.8.4\lib\ant-junit.jar;D:\dev\ant\ant1.8.4\lib\ant-junit4.jar;D:\dev\ant\ant1.8.4\lib\ant-launcher.jar;D:\dev\ant\ant1.8.4\lib\ant-netrexx.jar;D:\dev\ant\ant1.8.4\lib\ant-swing.jar;D:\dev\ant\ant1.8.4\lib\ant-testutil.jar;D:\dev\ant\ant1.8.4\lib\ant.jar;D:\dev\java\jdk16027\lib\tools.jar" />
	<property name="java.class.version" value="50.0" />
	<property name="java.endorsed.dirs" value="D:\dev\java\jdk16027\jre\lib\endorsed" />
	<property name="java.ext.dirs" value="D:\dev\java\jdk16027\jre\lib\ext;C:\Windows\Sun\Java\lib\ext" />
	<property name="java.home" value="D:\dev\java\jdk16027\jre" />
	<property name="java.io.tmpdir" value="C:\Users\xujing\AppData\Local\Temp\" />
	<property name="java.library.path" value="D:\dev\java\jdk16027\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\dev\java\jdk16027/bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;D:\Program Files\TortoiseSVN\bin;D:\Program Files (x86)\Rational\common;D:\dev\ant\ant1.8.4\bin;." />
	<property name="java.runtime.name" value="Java(TM) SE Runtime Environment" />
	<property name="java.runtime.version" value="1.6.0_27-b07" />
	<property name="java.specification.name" value="Java Platform API Specification" />
	<property name="java.specification.vendor" value="Sun Microsystems Inc." />
	<property name="java.specification.version" value="1.6" />
	<property name="java.vendor" value="Sun Microsystems Inc." />
	<property name="java.vendor.url" value="http://java.sun.com/" />
	<property name="java.vendor.url.bug" value="http://java.sun.com/cgi-bin/bugreport.cgi" />
	<property name="java.version" value="1.6.0_27" />
	<property name="java.vm.info" value="mixed mode" />
	<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM" />
	<property name="java.vm.specification.name" value="Java Virtual Machine Specification" />
	<property name="java.vm.specification.vendor" value="Sun Microsystems Inc." />
	<property name="java.vm.specification.version" value="1.0" />
	<property name="java.vm.vendor" value="Sun Microsystems Inc." />
	<property name="java.vm.version" value="20.2-b06" />
	<property name="line.separator" value="&#xd;&#xa;" />
	<property name="os.arch" value="amd64" />
	<property name="os.name" value="Windows 7" />
	<property name="os.version" value="6.1" />
	<property name="path.separator" value=";" />
	<property name="sun.arch.data.model" value="64" />
	<property name="sun.boot.class.path" value="D:\dev\java\jdk16027\jre\lib\resources.jar;D:\dev\java\jdk16027\jre\lib\rt.jar;D:\dev\java\jdk16027\jre\lib\sunrsasign.jar;D:\dev\java\jdk16027\jre\lib\jsse.jar;D:\dev\java\jdk16027\jre\lib\jce.jar;D:\dev\java\jdk16027\jre\lib\charsets.jar;D:\dev\java\jdk16027\jre\lib\modules\jdk.boot.jar;D:\dev\java\jdk16027\jre\classes" />
	<property name="sun.boot.library.path" value="D:\dev\java\jdk16027\jre\bin" />
	<property name="sun.cpu.endian" value="little" />
	<property name="sun.cpu.isalist" value="amd64" />
	<property name="sun.desktop" value="windows" />
	<property name="sun.io.unicode.encoding" value="UnicodeLittle" />
	<property name="sun.java.command" value="org.apache.tools.ant.launch.Launcher -cp .;D:\dev\java\jdk16027/lib/dt.jar;D:\dev\java\jdk16027/lib/tools.jar" />
	<property name="sun.java.launcher" value="SUN_STANDARD" />
	<property name="sun.jnu.encoding" value="GBK" />
	<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers" />
	<property name="sun.os.patch.level" value="Service Pack 1" />
	<property name="user.country" value="CN" />
	<property name="user.dir" value="e:\antspace\property" />
	<property name="user.home" value="C:\Users\xujing" />
	<property name="user.language" value="zh" />
	<property name="user.name" value="xujing" />
	<property name="user.timezone" value="" />
	<property name="user.variant" value="" />
</properties>

   

     3.  <property>任务设置特征

    build.properties文件内容:

#properties
fromdir=src
#property文件中的\要写两次,单个\为转义符
todir=${build.dir}\\tosrc
build.dir=build/classes

      测试1:

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">
	
	<property name="build.debug" value="true"/>

	<property name="release.dir" location="..\release"/>

	<!-- 引入特征文件 -->
	<property file="build.properties"/>

	<!-- 转换为绝对路径 -->
	<property name="fromdir.absolute" location="${fromdir}"/>
	<property name="todir.absolute" location="${todir}"/>

	<target name="echoall">
		<echo message="basedir=${basedir}"/>
		<echo message="build.debug=${build.debug}"/>
		<echo message="release.dir=${release.dir}"/>
		<echo message="build.dir=${build.dir}"/>
		<echo message="fromdir=${fromdir}"/>
		<echo message="todir=${todir}"/>
		<echo message="fromdir.absolute=${fromdir.absolute}"/>
		<echo message="todir.absolute=${todir.absolute}"/>
	</target>

</project>

    输出:

e:\antspace\property>ant
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] basedir=e:\antspace\property
     [echo] build.debug=true
     [echo] release.dir=e:\antspace\release
     [echo] build.dir=build/classes
     [echo] fromdir=src
     [echo] todir=build/classes\tosrc
     [echo] fromdir.absolute=e:\antspace\property\src
     [echo] todir.absolute=e:\antspace\property\build\classes\tosrc

BUILD SUCCESSFUL
Total time: 0 seconds

     property一旦设定则不能改变,查看下列输出:

e:\antspace\property>ant -Dbuild.debug=false -Dbuild.dir=output
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] basedir=e:\antspace\property
     [echo] build.debug=false
     [echo] release.dir=e:\antspace\release
     [echo] build.dir=output
     [echo] fromdir=src
     [echo] todir=output\tosrc
     [echo] fromdir.absolute=e:\antspace\property\src
     [echo] todir.absolute=e:\antspace\property\output\tosrc

BUILD SUCCESSFUL
Total time: 0 seconds

 

测试2:

    

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">
	
	<property name="build.debug" value="true"/>

	<property name="release.dir" location="..\release"/>

	<!-- 引入特征文件,文件不存在 -->
	<property file="build.properties2"/>

	<!-- 转换为绝对路径 -->
	<property name="fromdir.absolute" location="${fromdir}"/>
	<property name="todir.absolute" location="${todir}"/>

	<target name="echoall">
		<echo message="basedir=${basedir}"/>
		<echo message="build.debug=${build.debug}"/>
		<echo message="release.dir=${release.dir}"/>
		<echo message="build.dir=${build.dir}"/>
		<echo message="fromdir=${fromdir}"/>
		<echo message="todir=${todir}"/>
		<echo message="fromdir.absolute=${fromdir.absolute}"/>
		<echo message="todir.absolute=${todir.absolute}"/>
	</target>

</project>

 输出:

e:\antspace\property>ant
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] basedir=e:\antspace\property
     [echo] build.debug=true
     [echo] release.dir=e:\antspace\release
     [echo] build.dir=${build.dir}
     [echo] fromdir=${fromdir}
     [echo] todir=${todir}
     [echo] fromdir.absolute=e:\antspace\property\${fromdir}
     [echo] todir.absolute=e:\antspace\property\${todir}

BUILD SUCCESSFUL
Total time: 0 seconds

     

    4.  加载环境变量未研究

 

    5.  检查文件可用性<available>

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">
	
	<property name="mainsrc" location="src"/>
	<property name="maindir" location="${mainsrc}/xuj/ant"/>

	<available property="java.main" file="${maindir}/Main.java" type="file"/>
	<available property="main" classpath="${mainsrc}"  classname="xuj.ant.Main"/>

	<target name="echoall">
		<echo message="mainsrc=${mainsrc}"/>
		<echo message="maindir=${maindir}"/>
		<echo message="main=${main}"/>
		<echo message="java.main=${java.main}"/>
	</target>

</project>

 输出:

e:\antspace\property>ant
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] mainsrc=e:\antspace\property\src
     [echo] maindir=e:\antspace\property\src\xuj\ant
     [echo] main=true
     [echo] java.main=true

BUILD SUCCESSFUL
Total time: 0 seconds

 

    6. <condition>测试条件

        可以用逻辑运算符<and>、<or>、<xor>和<not>构成复杂的测试条件。

 

Attribute Description Required
property The name of the property to set. Yes
value The value to set the property to. Defaults to "true". No
else The value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Apache Ant 1.6.3 No

 

测试各类:

These are the nested elements that can be used as conditions in the <condition> and <waitfor> tasks.

not

The <not> element expects exactly one other condition to be nested into this element, negating the result of the condition. It doesn't have any attributes and accepts all nested elements of the condition task as nested elements as well.

and

The <and> element doesn't have any attributes and accepts an arbitrary number of conditions as nested elements - all nested elements of the condition task are supported. This condition is true if all of its contained conditions are, conditions will be evaluated in the order they have been specified in the build file.

The <and> condition has the same shortcut semantics as the Java && operator, as soon as one of the nested conditions is false, no other condition will be evaluated.

or

The <or> element doesn't have any attributes and accepts an arbitrary number of conditions as nested elements - all nested elements of the condition task are supported. This condition is true if at least one of its contained conditions is, conditions will be evaluated in the order they have been specified in the build file.

The <or> condition has the same shortcut semantics as the Java || operator, as soon as one of the nested conditions is true, no other condition will be evaluated.

xor

The <xor> element performs an exclusive or on all nested elements, similar to the ^ operator in Java. It only evaluates to true if an odd number of nested conditions are true. There is no shortcutting of evaluation, unlike the <and> and <or> tests. It doesn't have any attributes and accepts all nested elements of the condition task as nested elements as well.

available

This condition is identical to the Available task, all attributes and nested elements of that task are supported, the property and value attributes are redundant and will be ignored.

uptodate

This condition is identical to the Uptodate task, all attributes and nested elements of that task are supported, the property and value attributes are redundant and will be ignored.

os

Test whether the current operating system is of a given type. Each defined attribute is tested and the result is true only if all the tests succeed.

Attribute Description Required
family The name of the operating system family to expect. No
name The name of the operating system to expect. No
arch The architecture of the operating system to expect. No
version The version of the operating system to expect. No

Supported values for the family attribute are:

  • windows (for all versions of Microsoft Windows)
  • dos (for all Microsoft DOS based operating systems including Microsoft Windows and OS/2)
  • mac (for all Apple Macintosh systems)
  • unix (for all Unix and Unix-like operating systems)
  • netware (for Novell NetWare)
  • os/2 (for OS/2)
  • tandem (for HP's NonStop Kernel - formerly Tandem)
  • win9x for Microsoft Windows 95 and 98, ME and CE
  • winnt for Microsoft Windows NT-based systems, including Windows 2000, XP and successors
  • z/os for z/OS and OS/390
  • os/400 for OS/400
  • openvms for OpenVMS

equals

Tests whether the two given values are equal.

Attribute Description Required
arg1 First value to test. Yes
arg2 Second value to test. Yes
casesensitive Perform a case sensitive comparision. Default is true. No
trim Trim whitespace from arguments before comparing them. Default is false. No
forcestring Force string comparison of arg1/arg2. Default is false. Since Apache Ant 1.8.1 No

isset

Test whether a given property has been set in this project.

Attribute Description Required
property The name of the property to test. Yes

checksum

This condition is identical to the Checksum task, all attributes and nested elements of that task are supported, the property and overwrite attributes are redundant and will be ignored.

http

The http condition checks for a valid response from a web server of the specified url. By default, HTTP responses errors of 400 or greater are viewed as invalid.

Attribute Description Required
url The full URL of the page to request. The web server must return a status code below the value of errorsBeginAt Yes.
errorsBeginAt The lowest HTTP response code that signals an error; by default '400'; server errors, not-authorized, not-found and the like are detected No
requestMethod The HTTP method to be used when issuing the request. Any of GET, POST, HEAD, OPTIONS, PUT, DELETEm and TRACE are valid, subject to protocol restrictions. The default if not specified is "GET".
since Ant 1.8.0
No

socket

The socket condition checks for the existence of a TCP/IP listener at the specified host and port.

Attribute Description Required
server The DNS name or IP address of the server. Yes.
port The port number to connect to. Yes.

filesmatch

Test two files for matching. Nonexistence of one file results in "false", although if neither exists they are considered equal in terms of content. This test does a byte for byte comparision, so test time scales with byte size. NB: if the files are different sizes, one of them is missing or the filenames match the answer is so obvious the detailed test is omitted.

Attribute Description Required
file1 First file to test Yes
file2 Second file to test Yes
textfile Whether to ignore line endings when comparing files; defaults to false, while true triggers a binary comparison. Since Ant 1.7 No

contains

Tests whether a string contains another one.

Attribute Description Required
string The string to search in. Yes
substring The string to search for. Yes
casesensitive Perform a case sensitive comparision. Default is true. No

istrue

Tests whether a string equals any of the ant definitions of true, that is "true","yes", or "on"

Attribute Description Required
value value to test Yes
<istrue value="${someproperty}"/>
<istrue value="false"/>

isfalse

Tests whether a string is not true, the negation of <istrue>

Attribute Description Required
value value to test Yes
<isfalse value="${someproperty}"/>
<isfalse value="false"/>

isreference

Test whether a given reference has been defined in this project and - optionally - is of an expected type.

This condition has been added in Apache Ant 1.6.

Attribute Description Required
refid The id of the reference to test. Yes
type Name of the data type or task this reference is expected to be. No

issigned

Test whether a jarfile is signed. If the name of the signature is passed, the file is checked for presence of that particular signature; otherwise the file is checked for the existence of any signature. It does not perform rigorous signature validation; it only looks for the presence of a signature.

This condition was added in Apache Ant 1.7.

Attribute Description Required
file The jarfile that is to be tested for the presence of a signature. Yes
name The signature name to check for. No

isfileselected

Test whether a file passes an embedded selector.

This condition was added in Apache Ant 1.6.3.

Attribute Description Required
file The file to check if is passes the embedded selector. Yes
basedir The base directory to use for name based selectors. It this is not set, the project's basedirectory will be used. No

Example usage:

<isfileselected file="a.xml">
  <date datetime="06/28/2000 2:02 pm" when="equal"/>
</isfileselected>

typefound

Test whether a given type is defined, and that its implementation class can be loaded. Types include tasks, datatypes, scriptdefs, macrodefs and presetdefs.

This condition was added in Apache Ant 1.7.

Attribute Description Required
name name of the type Yes
uri The uri that this type lives in. No

Example usages:

<typefound name="junit"/>
<typefound uri="antlib:org.apache.maven.artifact.ant" name="artifact"/>

scriptcondition

Evaluate a condition based on a script in any Apache BSF or JSR 223 supported language.

See the Script task for an explanation of scripts and dependencies.

This condition was added in Apache Ant 1.7.

Attribute Description Required
language script language Yes
manager The script engine manager to use. See the script task for using this attribute. No - default is "auto"
value default boolean value No -default is "false"
src filename of script source No
setbeans whether to have all properties, references and targets as global variables in the script. since Ant 1.8.0 No, default is "true".
classpath The classpath to pass into the script. No
classpathref The classpath to use, given as a reference to a path defined elsewhere. No
Parameters specified as nested elements
classpath

See the script task for using this nested element.

Description

The script supports script language inline, this script has access to the same beans as the <script> task, and to the self bean, which refers back to the condition itself. If the script evaluates to a boolean result, this is the result of the condition's evaluation (since Ant 1.7.1). Alternatively, self.valuecan be used to set the evaluation result.

Example:

<scriptcondition language="javascript"
        value="true">
    self.setValue(false);
</scriptcondition>

Sets the default value of the condition to true, then in the script, sets the value to false. This condition always evaluates to "false"

parsersupports

Tests whether Ant's XML parser supports a given feature or property, as per the SAX/JAXP specifications, by attempting to set the appropriate property/feature/

This condition was added in Apache Ant 1.7.

Attribute Description Required
property property to set one of property or feature
feature feature to set one of property or feature
value string (property) or boolean (feature) For property tests, but not for feature tests
<parsersupports feature="http://xml.org/sax/features/namespaces"/>

Check for namespace support. All SAX2 parsers should have this.

<or>
  <parsersupports
    feature="http://apache.org/xml/features/validation/schema"/>
  <parsersupports
    feature="http://java.sun.com/xml/jaxp/properties/schemaSource"/>
</or>

Check for XML Schema support.

<parsersupports
  property="http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"
  value="document.xsd"/>

Check for Xerces-specific definition of the location of the no namespace schema.

isreachable

Uses Java1.5+ networking APIs to probe for a (remote) system being reachable. Exactly what probe mechanisms are used is an implementation feature of the JVM. They may include ICMP "ping" packets, UDP or TCP connections to port 7 "echo service" or other means. On Java1.4 and earlier, being able to resolve the hostname is considered success. This means that if DNS is not working or a URL/hostname is bad, the test will fail, but otherwise succeed even if the remote host is actually absent.

This condition turns unknown host exceptions into false conditions. This is because on a laptop, DNS is one of the first services when the network goes; you are implicitly offline.

If a URL is supplied instead of a host, the hostname is extracted and used in the test - all other parts of the URL are discarded.

The test may not work through firewalls, that is, something may be reachable using a protocol such as HTTP, while the lower level ICMP packets get dropped on the floor. Similarly, a host may detected as reachable with ICMP, but not reachable on other ports (i.e. port 80), because of firewalls.

This condition was added in Apache Ant 1.7.

Attribute Description Required
host host to check for one of url or host
url URL containing hostname one of url or host
timeout timeout in seconds no, default is 30s
<condition property="offline">
  <isreachable url="http://ibiblio.org/maven/" />
</condition>

Probe for the maven repository being reachable.

<condition property="offline">
  <isreachable host="ibiblio.org" timeout="10" />
</condition>

Probe for the maven repository being reachable using the hostname, ten second timeout..

length

This condition is a facet of the Length task. It is used to test the length of a string or one or more files. Since Ant 1.6.3

<length string=" foo " trim="true" length="3" />

Verify a string is of a certain length.

<length file="foo" when="greater" length="0" />

Verify that file foo is not empty.

isfailure

Test the return code of an executable (see the Exec task) for failure. Since Ant 1.7

Attribute Description Required
code The return code to test. Yes

resourcecount

This condition is a facet of the ResourceCount task. It is used to test the size of a resource collection. Since Ant 1.7

<resourcecount refid="myresourcecollection" when="greater" count="0" />

Verify that a resource collection is not empty.

resourcesmatch

Test resources for matching. Nonexistence of one or more resources results in "false", although if none exists they are considered equal in terms of content. By default this test does a byte for byte comparision, so test time scales with byte size. NB: if the files are different sizes, one of them is missing or the filenames match the answer is so obvious the detailed test is omitted. The resources to check are specified as nested resource collections, meaning that more than two resources can be checked; in this case all resources must match. Since Ant 1.7

Attribute Description Required
astext Whether to ignore line endings when comparing resource content; defaults to false, while true triggers a binary comparison. No

resourcecontains

Tests whether a resource contains a given (sub)string.

The resources to check are specified via references or - in the case of file resources via the resource attribute. Since Ant 1.7.1

Attribute Description Required
resource Name of a file that is the resource to test. One of the two
refid Reference to a resource defined inside the project.
substring The string to search for. Yes
casesensitive Perform a case sensitive comparision. Default is true. No

hasmethod

Tests for a class having a method or field. If the class is not found or fails to load, the build fails. Since Ant 1.7

Attribute Description Required
classname name of the class to load yes
field name of a field to look for one of field or method
method name of a method to look for one of field or method
ignoreSystemClasses should system classes be ignored? No -default is false
classpath a class path No
classpathref reference to a class path No

There is also a nested <classpath> element, which can be used to specify a classpath.

<hasmethod classname="java.util.ArrayList" method="trimToSize"  />

Looks for the method trimToSize in the ArrayList class.

matches

Test if the specified string matches the specified regular expression pattern. Since Ant 1.7

Attribute Description Required
string The string to test. Yes
pattern The regular expression pattern used to test. Yes, unless there is a nested<regexp> element.
casesensitive Perform a case sensitive match. Default is true. No
multiline Perform a multi line match. Default is false. No
singleline This allows '.' to match new lines. SingleLine is not to be confused with multiline, SingleLine is a perl regex term, it corresponds to dotall in java regex. Default is false. No

There is also an optional <regexp> element, which can be used to specify a regular expression instead of the "pattern" attribute. See Regexp Type for the description of the nested element regexp and of the choice of regular expression implementation.

An example:

<condition property="legal-password">
  <matches pattern="[1-9]" string="${user-input}"/>
</condition>
<fail message="Your password should at least contain one number"
      unless="legal-password"/>

The following example sets the property "ok" if the property "input" is three characters long, starting with 'a' and ending with 'b'.

<condition property="ok">
  <matches string="${input}" pattern="^a.b$"/>
</condition>

The following defines a reference regular expression for matching dates and then uses antunit to check if the property "today" is in the correct format:

<regexp id="date.pattern" pattern="^[0123]\d-[01]\d-[12]\d\d\d$"/>

<au:assertTrue xmlns:au="antlib:org.apache.ant.antunit">
  <matches string="${today}">
    <regexp refid="date.pattern"/>
  </matches>
</au:assertTrue>

The following example shows the use of the singleline and the casesensitive flags.

<au:assertTrue>
  <matches string="AB${line.separator}C" pattern="^ab.*C$"
           casesensitive="false"
           singleline="true"/>
</au:assertTrue>
<au:assertFalse>
  <matches string="AB${line.separator}C" pattern="^ab.*C$"
           casesensitive="false"
           singleline="false"/>
</au:assertFalse>

antversion

This condition is identical to the Antversion task, all attributes are supported, the property attribute is redundant and will be ignored.

hasfreespace

Tests a partition to see if there is enough space. Since Ant 1.7.0

Needed attribute can be specified using standard computing terms:

  • K : Kilobytes (1024 bytes)
  • M : Megabytes (1024 K)
  • G : Gigabytes (1024 M)
  • T : Terabytes (1024 G)
  • P : Petabytes (1024 T)

 

Attribute Description Required
partition The partition or filesystem to check for freespace Yes
needed The amount of freespace needed. Yes

An example:

<hasfreespace partition="c:" needed="100M"/>

islastmodified

Tests the last modified date of a resource. Since Ant 1.8.0

Attribute Description Required
millis Specifies the expected modification time of the resource in milliseconds since midnight Jan 1 1970. Exactly one of the two.
datetime Specifies the expected modification time of the resource. The special value "now" indicates the current time.
pattern SimpleDateFormat-compatible pattern string. Defaults to MM/DD/YYYY HH:MM AM_or_PM or MM/DD/YYYY HH:MM:SS AM_or_PM. No
mode How to compare the timestamp. Accepted values are "equals", "before", "not-before", "after" and "not-after". No, defaults to "equals".

The actual resource to test is specified as a nested element.

An example:

<islastmodified dateTime="08/18/2009 04:41:19 AM" mode="not-before">
  <file file="${file}"/>
</islastmodified>

resourceexists

Tests a resource for existance. since Ant 1.8.0

The actual resource to test is specified as a nested element.

An example:

<resourceexists>
  <file file="${file}"/>
</resourceexists>

测试:

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">

	<condition property="os1" value="windows" else="other">
		<or>
			<os family="win9x"/>
			<os family="winnt"/>
		</or>
	</condition>

	<condition property="isExists" else="false">
		<available file="src/xuj/ant/Main.java" type="file"/>
	</condition>

	<target name="echoall">
		<echo message="isExists=${isExists}"/>
		<echo message="os1=${os1}"/>
	</target>

</project>

 

输出:

e:\antspace\property>ant
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] isExists=true
     [echo] os1=windows

BUILD SUCCESSFUL
Total time: 0 seconds

 

    7.<tstamp>创建时间戳

Parameters

Attribute Description Required
prefix Prefix used for all properties set. The default is no prefix. No

Nested Elements

The Tstamp task supports a <format> nested element that allows a property to be set to the current date and time in a given format. The date/time patterns are as defined in the Java SimpleDateFormat class. The format element also allows offsets to be applied to the time to generate different time values. 

Attribute Description Required
property The property to receive the date/time string in the given pattern. Yes
pattern The date/time pattern to be used. The values are as defined by the Java SimpleDateFormat class. Yes
timezone The timezone to use for displaying time. The values are as defined by the Java TimeZone class. No
offset The numeric offset to the current time No
unit The unit of the offset to be applied to the current time. Valid Values are
  • millisecond
  • second
  • minute
  • hour
  • day
  • week
  • month
  • year
No
locale The locale used to create date/time string. The general form is "language, country, variant" but either variant or variant and country may be omitted. For more information please refer to documentation for the Locale class. No

 测试:

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">

	<tstamp/>

	<tstamp prefix="start"/>

	<tstamp>
		<format property="touch.time" pattern="yyyy-MM-dd hh:mm:ss.SSS"
              offset="-5" unit="hour"/>
	</tstamp>

	<target name="echoall">
		<echo message="tstamp:"/>
		<echo message="DSTAMP=${DSTAMP}"/>
		<echo message="TSTAMP=${TSTAMP}"/>
		<echo message="TODAY=${TODAY}"/>
		<echo message="tstamp prefix=start:"/>
		<echo message="DSTAMP=${start.DSTAMP}"/>
		<echo message="TSTAMP=${start.TSTAMP}"/>
		<echo message="TODAY=${start.TODAY}"/>
		<echo message="touch.time:"/>
		<echo message="touch.time=${touch.time}"/>
	</target>
</project>

 输出:

e:\antspace\property>ant
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] tstamp:
     [echo] DSTAMP=20130116
     [echo] TSTAMP=1333
     [echo] TODAY=January 16 2013
     [echo] tstamp prefix=start:
     [echo] DSTAMP=20130116
     [echo] TSTAMP=1333
     [echo] TODAY=January 16 2013
     [echo] touch.time:
     [echo] touch.time=2013-01-16 08:33:54.270

BUILD SUCCESSFUL
Total time: 0 seconds

 

    8. 命令行设置property(特征)

        用-D或-propertyfile设置命令行特征,且设置后不会改变。其中-D设置的优先。

测试:

<?xml version="1.0" encoding="UTF-8"?>
<project name="property" default="echoall">

	<tstamp/>

	<tstamp prefix="start"/>

	<tstamp>
		<format property="touch.time" pattern="yyyy-MM-dd hh:mm:ss.SSS"
              offset="-5" unit="hour"/>
	</tstamp>

	<target name="echoall">
		<echo message="tstamp:"/>
		<echo message="DSTAMP=${DSTAMP}"/>
		<echo message="TSTAMP=${TSTAMP}"/>
		<echo message="TODAY=${TODAY}"/>
		<echo message="tstamp prefix=start:"/>
		<echo message="DSTAMP=${start.DSTAMP}"/>
		<echo message="TSTAMP=${start.TSTAMP}"/>
		<echo message="TODAY=${start.TODAY}"/>
		<echo message="touch.time:"/>
		<echo message="touch.time=${touch.time}"/>
	</target>
</project>

 build.properties文件内容:

#properties
touch.time=day

 输出:

e:\antspace\property>ant -propertyfile build.properties
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] tstamp:
     [echo] DSTAMP=20130116
     [echo] TSTAMP=1339
     [echo] TODAY=January 16 2013
     [echo] tstamp prefix=start:
     [echo] DSTAMP=20130116
     [echo] TSTAMP=1339
     [echo] TODAY=January 16 2013
     [echo] touch.time:
     [echo] touch.time=day

BUILD SUCCESSFUL
Total time: 0 seconds

e:\antspace\property>ant -propertyfile build.properties -Dtouch.time=week
Buildfile: e:\antspace\property\build.xml

echoall:
     [echo] tstamp:
     [echo] DSTAMP=20130116
     [echo] TSTAMP=1339
     [echo] TODAY=January 16 2013
     [echo] tstamp prefix=start:
     [echo] DSTAMP=20130116
     [echo] TSTAMP=1339
     [echo] TODAY=January 16 2013
     [echo] touch.time:
     [echo] touch.time=week

BUILD SUCCESSFUL
Total time: 0 seconds

 

分享到:
评论

相关推荐

    Ant 常见属性标签--property

    在Java开发领域,Apache Ant是一个广泛应用的自动化构建工具,它基于XML来定义项目构建过程。Ant的任务是通过一系列预先定义好的任务(tasks)来编译、打包、测试和部署Java应用。本文将深入探讨Ant中的“属性”标签...

    Ant 资源 Ant 资源

    ### 关于Ant构建工具 #### 一、Ant简介 **Ant**是一款开源的Java构建工具,主要用于自动化构建过程,如编译、测试、部署等。它最初由James Duncan Davidson开发,现归属于Apache软件基金会。 ##### 1.1 什么是Ant...

    替换文本文件中Ant表达式的Ant扩展任务

    该程序为Ant的扩展任务,在拷贝文本格式的文件的同时替换文件中的Ant表达式,类似于 ${property.name},属性值来自于Ant脚本加载的属性文件,如果没有属性文件中没有定义相关属性,那么保留原来的表达式;...

    ant工具和ant教程

    项目元素定义了整个构建的根,通常包括版本信息和属性(property)定义。属性可以在整个构建文件中引用,允许动态配置。目标是一系列任务的集合,它们可以被单独调用或者按顺序执行。任务是Ant的基本执行单元,例如`...

    apache-ant-1.9.16-bin.tar.gz

    Apache Ant 是一个由Apache软件基金会开发的Java库和命令行工具,其设计目的是驱动构建过程。这个工具的名字“Ant”来源于“蚂蚁”,象征着它在软件工程中的小而有力的角色,能够处理各种复杂的构建任务。在Java开发...

    ant简明教程;ant调用bat方法

    Ant构建文件的核心元素包括`project`、`target`、`property`和`task`: 1. `project`元素是整个构建文件的根节点,它必须存在。`name`属性用于设置项目名称,`default`属性指定默认执行的目标,`basedir`属性定义...

    ant打包ant的build.xml的详解

    Ant 打包 ant 的 build.xml 详解 Ant 是一个流行的自动化构建工具,广泛应用于软件开发和项目自动化构建中。本文将详细介绍 Ant 的环境变量配置和 build.xml 文件的配置详解。 第一步:安装 Apache Ant 在开始...

    mac下ant打包android

    2. 使用`&lt;property&gt;`元素设置渠道标识。 3. 使用`&lt;replace&gt;`任务在资源文件中替换渠道标识。 4. 调用`apkbuilder`或`aapt`命令生成APK。 5. 可选地,使用`jarsigner`和`zipalign`进行签名和优化。 最后,运行`ant ...

    hello-ant 项目 ,学习ant工具的build file. java

    &lt;property name="ant.home" value="${env.ANT_HOME}"/&gt; ``` - `&lt;property environment="env"/&gt;`: 获取操作系统环境变量。 - `${env.JAVA_HOME}`: 获取`JAVA_HOME`环境变量的值。 - `${env.ANT_HOME}`: 获取`...

    apache ant使用例子

    Apache Ant是一种广泛使用的Java构建工具,它以XML为基础来描述构建过程,允许开发者定义构建任务、依赖关系以及项目构建的整个流程。Ant以其灵活性和可扩展性而受到开发者喜爱,它可以自动化编译、打包、测试等Java...

    java ant 详细解析文档

    本文档旨在深入探讨Ant中的`property`和`xmlinclude`特性,帮助读者更好地理解如何利用这些功能简化项目管理和构建流程。 #### 二、Property 的使用详解 **Property** 在Ant中主要用于定义和存储变量,这些变量...

    ant学习笔记之(ant执行命令的详细参数和Ant自带的系统属性)

    11. `-D&lt;property&gt;=&lt;value&gt;`:设置系统属性,这些属性可以在Ant脚本中被引用,也可以覆盖已经设定的属性值。 二、Ant系统属性 1. `${os.name}`:返回操作系统的名称,例如Windows、Linux等。 2. `${basedir}`:...

    Ant 1.9.1 学习笔记

    Ant构建文件中除了`&lt;project&gt;`和`&lt;target&gt;`外,还包括`&lt;property&gt;`和`&lt;path&gt;`等元素。`&lt;property&gt;`用于定义构建过程中的属性,它们可以是常量值,也可以通过属性文件来指定。属性的定义可以使用`name`、`value`、`...

    ant学习指南 ANT是一个基于Java的自动化脚本引擎

    Ant,全称Apache Ant,是一个基于Java的构建工具,它以XML格式定义构建脚本,使得构建过程在不同平台上保持一致。Ant的核心理念是“Java的Makefile”,它弥补了Java项目中自动化编译、打包、测试等重复工作的问题。...

    项目Ant配置实例一次下载各处使用

    在Ant构建过程中,可以使用`&lt;property&gt;`标签来导入这些属性,并在其他任务中引用它们,以实现环境间的差异性配置。 `build.properties`文件通常用于存储项目级别的配置信息,如源代码目录、输出目录、依赖库的位置...

    Ant权威指南(介绍ant的好书)

    Ant的核心在于其XML格式的构建文件——build.xml,书中详细介绍了如何编写这个文件,包括定义项目(project)、目标(target)、任务(task)以及属性(property)。此外,还深入探讨了Ant的依赖关系管理、条件语句...

    apache-ant-1.10.12-bin.tar.gz

    Apache Ant 是一个广泛使用的Java构建工具,它遵循XML格式的构建文件来定义任务,从而自动化构建过程,包括编译、打包、测试和部署等。在Java开发中,Ant以其灵活性和可扩展性著称,是许多项目管理和持续集成流程的...

    Ant详细配置总结

    ### Ant详细配置总结 #### 一、Ant简介与特点 Ant是Apache旗下的一款强大的构建工具,主要用于Java项目的自动化构建过程,如编译、测试、打包、部署等操作。它的核心优势在于其高度的可定制性和灵活性,使得开发者...

    ant工具使用.zip

    Apache Ant 是一个由Java语言编写的构建工具,它在软件开发过程中主要负责自动化构建任务,如编译、打包、测试和部署。Ant以其XML为基础的构建文件(build.xml)著称,开发者可以自定义构建过程,使其适应各种项目...

    Ant 项目构建教程

    Ant 是一个由 Apache 开发的 Java 项目构建工具,它简化了构建、编译、测试和部署Java应用程序的过程。...此外,Ant还支持通过`property`元素定义和使用属性,可以方便地进行配置管理和跨平台构建。

Global site tag (gtag.js) - Google Analytics