path,classpath的定义可以使用pathelement, fileset, filelist, dirset,同时还可以使用引用refid.
引用是解决重复配置脚本的方法,如同代码中的常量定义。
下面的内容摘自ant1.7.1的中文手册。
可以使用“:”和“;”作为 PATH 和 CLASSPATH 引用的分隔符。 Ant 会将分隔符转换成当前操作系统中使用的分隔符。
当需要指定路径一样的结构时,可以使用嵌套元素。可以使用下面的格式:
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>
|
location 属性表示一个单一的文件或目录的名称,这个名称可以是相对于 project 根目录的相对目录,也可以是文件的绝对路径。路径属性可以使用预定义的路径,在任何情况下,应该优先选择带有 location 属性的多个元素。
为了简单起见, <classpath> 字节就有 path 和 location 属性,所以:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
|
可以省略成:
<classpath path="${classpath}"/>
|
另外,一个或多个资源收集器可以有嵌套元素(必须由文件 - 类型资源组成)。同时,应该注意的是即使资源收集器使用遇到的顺序进行处理,某些资源手机类型比如 fileset , dirset 和文件没有定义顺序。
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
<dirset dir="build">
<include name="apps/**/classes"/>
<exclude name="apps/**/*Test*"/>
</dirset>
<filelist refid="third-party_jars"/>
</classpath>
|
上述脚本定义了名称为 ${classpath} 的路径,然后是 lib 目录下的所有 jar 文件, class 目录, app 子目录下的所有 classes 目录,排除路径名称中含有 Test 的目录,以及 FileLIst 的引用。
如果您想将这个路径一样的结构用在许多任务中,可以使用和 target 同级的 <path> 元素进行定义,并且通过 id 属性进行引用。
一个 path-like 结构可以包含其他 path-like 结构的引用(路径本身就是一个资源收集器),引用方式是嵌套的 <path> 元素。
<path id="base.path"> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement location="classes"/> </path>
<path id="tests.path"> <path refid="base.path"/> <pathelement location="testclasses"/> </path> |
上文提到的 <classpath> 的捷径在 <path> 元素中仍然有效。例如:
<path id="base.path">
<pathelement path="${classpath}"/>
</path>
可以省略成:
<path id="base.path" path="${classpath}"/>
|
从 Ant1.6 开始,在属性中加入了路径简写,通过简写可以将路径转换为 OS 中的特定字符串。可以使用表达式 ${toString:pathreference } 引用字符串作为路径参数,这个字符串可以转换为路径参数。例如:
<path id="lib.path.ref">
<fileset dir="lib" includes="*.jar"/>
</path>
<javac srcdir="src" destdir="classes">
<compilerarg arg="-Xbootstrap/p:${toString:lib.path.ref}"/>
</javac>
|
任何工程元素可以使用 id 属性赋予一个标识符。大多数情况下,在随后的脚本中,这个元素可以使用指定的 refid 属性被同类元素引用。用这种方式可以方便的复制同样的 XML 片段,例如,多次使用 <classpath> 结构。
例如:
<project ... >
<target ... >
<rmic ...>
<classpath>
<pathelement location="lib/"/>
<pathelement path="/home/kev/workspace/ant-core-171/bootstrap/lib/ant-launcher.jar:/home/kev/workspace/ant-core-171/lib/optional/ant-antunit-1.0.jar:/home/kev/workspace/ant-core-171/lib/optional/junit-3.8.2.jar:/home/kev/workspace/ant-core-171/lib/optional/activation.jar:/home/kev/workspace/ant-core-171/lib/optional/antlr-2.7.5.jar:/home/kev/workspace/ant-core-171/lib/optional/bcel-5.2.jar:/home/kev/workspace/ant-core-171/lib/optional/bsf.jar:/home/kev/workspace/ant-core-171/lib/optional/commons-logging-1.0.4.jar:/home/kev/workspace/ant-core-171/lib/optional/commons-net-1.4.1.jar:/home/kev/workspace/ant-core-171/lib/optional/groovy-all-1.0.jar:/home/kev/workspace/ant-core-171/lib/optional/jacl.jar:/home/kev/workspace/ant-core-171/lib/optional/jai_codec.jar:/home/kev/workspace/ant-core-171/lib/optional/jai_core.jar:/home/kev/workspace/ant-core-171/lib/optional/jakarta-oro-2.0.8.jar:/home/kev/workspace/ant-core-171/lib/optional/jakarta-regexp-1.5.jar:/home/kev/workspace/ant-core-171/lib/optional/jdepend-2.9.jar:/home/kev/workspace/ant-core-171/lib/optional/jruby.jar:/home/kev/workspace/ant-core-171/lib/optional/js.jar:/home/kev/workspace/ant-core-171/lib/optional/jsch-0.1.33.jar:/home/kev/workspace/ant-core-171/lib/optional/judo.jar:/home/kev/workspace/ant-core-171/lib/optional/jython_Release_2_2alpha1.jar:/home/kev/workspace/ant-core-171/lib/optional/log4j-1.2.14.jar:/home/kev/workspace/ant-core-171/lib/optional/mail-1.4.jar:/home/kev/workspace/ant-core-171/lib/optional/NetRexxC.jar:/home/kev/workspace/ant-core-171/lib/optional/NetRexxR.jar:/home/kev/workspace/ant-core-171/lib/optional/resolver.jar:/home/kev/workspace/ant-core-171/lib/optional/serializer.jar:/home/kev/workspace/ant-core-171/lib/optional/starteam93.jar:/home/kev/workspace/ant-core-171/lib/optional/stylebook-1.0-b2.jar:/home/kev/workspace/ant-core-171/lib/optional/tcljava.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogic.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogicaux.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogicclasses.jar:/home/kev/workspace/ant-core-171/lib/optional/xalan.jar:/home/kev/workspace/ant-core-171/lib/optional/xalan1.jar:/home/kev/workspace/ant-core-171/lib/optional/xsltc.jar:/home/kev/workspace/ant-core-171/lib/optional/:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jsch.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-antlr.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-netrexx.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-junit.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-log4j.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-starteam.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jmf.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-launcher.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-regexp.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-stylebook.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-commons-net.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-weblogic.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-testutil.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-resolver.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-bcel.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jai.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-bsf.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jdepend.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-trax.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-oro.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-commons-logging.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-javamail.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-swing.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-nodeps.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/xercesImpl.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/xml-apis.jar:/usr/lib/jvm/java-6-sun-1.6.0.06/lib/tools.jar/"/>
<pathelement path="${additional.path}"/>
</classpath>
</rmic>
</target>
<target ... >
<javac ...>
<classpath>
<pathelement location="lib/"/>
<pathelement path="/home/kev/workspace/ant-core-171/bootstrap/lib/ant-launcher.jar:/home/kev/workspace/ant-core-171/lib/optional/ant-antunit-1.0.jar:/home/kev/workspace/ant-core-171/lib/optional/junit-3.8.2.jar:/home/kev/workspace/ant-core-171/lib/optional/activation.jar:/home/kev/workspace/ant-core-171/lib/optional/antlr-2.7.5.jar:/home/kev/workspace/ant-core-171/lib/optional/bcel-5.2.jar:/home/kev/workspace/ant-core-171/lib/optional/bsf.jar:/home/kev/workspace/ant-core-171/lib/optional/commons-logging-1.0.4.jar:/home/kev/workspace/ant-core-171/lib/optional/commons-net-1.4.1.jar:/home/kev/workspace/ant-core-171/lib/optional/groovy-all-1.0.jar:/home/kev/workspace/ant-core-171/lib/optional/jacl.jar:/home/kev/workspace/ant-core-171/lib/optional/jai_codec.jar:/home/kev/workspace/ant-core-171/lib/optional/jai_core.jar:/home/kev/workspace/ant-core-171/lib/optional/jakarta-oro-2.0.8.jar:/home/kev/workspace/ant-core-171/lib/optional/jakarta-regexp-1.5.jar:/home/kev/workspace/ant-core-171/lib/optional/jdepend-2.9.jar:/home/kev/workspace/ant-core-171/lib/optional/jruby.jar:/home/kev/workspace/ant-core-171/lib/optional/js.jar:/home/kev/workspace/ant-core-171/lib/optional/jsch-0.1.33.jar:/home/kev/workspace/ant-core-171/lib/optional/judo.jar:/home/kev/workspace/ant-core-171/lib/optional/jython_Release_2_2alpha1.jar:/home/kev/workspace/ant-core-171/lib/optional/log4j-1.2.14.jar:/home/kev/workspace/ant-core-171/lib/optional/mail-1.4.jar:/home/kev/workspace/ant-core-171/lib/optional/NetRexxC.jar:/home/kev/workspace/ant-core-171/lib/optional/NetRexxR.jar:/home/kev/workspace/ant-core-171/lib/optional/resolver.jar:/home/kev/workspace/ant-core-171/lib/optional/serializer.jar:/home/kev/workspace/ant-core-171/lib/optional/starteam93.jar:/home/kev/workspace/ant-core-171/lib/optional/stylebook-1.0-b2.jar:/home/kev/workspace/ant-core-171/lib/optional/tcljava.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogic.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogicaux.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogicclasses.jar:/home/kev/workspace/ant-core-171/lib/optional/xalan.jar:/home/kev/workspace/ant-core-171/lib/optional/xalan1.jar:/home/kev/workspace/ant-core-171/lib/optional/xsltc.jar:/home/kev/workspace/ant-core-171/lib/optional/:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jsch.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-antlr.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-netrexx.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-junit.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-log4j.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-starteam.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jmf.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-launcher.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-regexp.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-stylebook.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-commons-net.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-weblogic.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-testutil.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-resolver.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-bcel.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jai.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-bsf.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jdepend.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-trax.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-oro.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-commons-logging.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-javamail.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-swing.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-nodeps.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/xercesImpl.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/xml-apis.jar:/usr/lib/jvm/java-6-sun-1.6.0.06/lib/tools.jar/"/>
<pathelement path="${additional.path}"/>
</classpath>
</javac>
</target>
</project>
|
可以简写为:
<project ... >
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="/home/kev/workspace/ant-core-171/bootstrap/lib/ant-launcher.jar:/home/kev/workspace/ant-core-171/lib/optional/ant-antunit-1.0.jar:/home/kev/workspace/ant-core-171/lib/optional/junit-3.8.2.jar:/home/kev/workspace/ant-core-171/lib/optional/activation.jar:/home/kev/workspace/ant-core-171/lib/optional/antlr-2.7.5.jar:/home/kev/workspace/ant-core-171/lib/optional/bcel-5.2.jar:/home/kev/workspace/ant-core-171/lib/optional/bsf.jar:/home/kev/workspace/ant-core-171/lib/optional/commons-logging-1.0.4.jar:/home/kev/workspace/ant-core-171/lib/optional/commons-net-1.4.1.jar:/home/kev/workspace/ant-core-171/lib/optional/groovy-all-1.0.jar:/home/kev/workspace/ant-core-171/lib/optional/jacl.jar:/home/kev/workspace/ant-core-171/lib/optional/jai_codec.jar:/home/kev/workspace/ant-core-171/lib/optional/jai_core.jar:/home/kev/workspace/ant-core-171/lib/optional/jakarta-oro-2.0.8.jar:/home/kev/workspace/ant-core-171/lib/optional/jakarta-regexp-1.5.jar:/home/kev/workspace/ant-core-171/lib/optional/jdepend-2.9.jar:/home/kev/workspace/ant-core-171/lib/optional/jruby.jar:/home/kev/workspace/ant-core-171/lib/optional/js.jar:/home/kev/workspace/ant-core-171/lib/optional/jsch-0.1.33.jar:/home/kev/workspace/ant-core-171/lib/optional/judo.jar:/home/kev/workspace/ant-core-171/lib/optional/jython_Release_2_2alpha1.jar:/home/kev/workspace/ant-core-171/lib/optional/log4j-1.2.14.jar:/home/kev/workspace/ant-core-171/lib/optional/mail-1.4.jar:/home/kev/workspace/ant-core-171/lib/optional/NetRexxC.jar:/home/kev/workspace/ant-core-171/lib/optional/NetRexxR.jar:/home/kev/workspace/ant-core-171/lib/optional/resolver.jar:/home/kev/workspace/ant-core-171/lib/optional/serializer.jar:/home/kev/workspace/ant-core-171/lib/optional/starteam93.jar:/home/kev/workspace/ant-core-171/lib/optional/stylebook-1.0-b2.jar:/home/kev/workspace/ant-core-171/lib/optional/tcljava.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogic.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogicaux.jar:/home/kev/workspace/ant-core-171/lib/optional/weblogicclasses.jar:/home/kev/workspace/ant-core-171/lib/optional/xalan.jar:/home/kev/workspace/ant-core-171/lib/optional/xalan1.jar:/home/kev/workspace/ant-core-171/lib/optional/xsltc.jar:/home/kev/workspace/ant-core-171/lib/optional/:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jsch.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-antlr.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-netrexx.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-junit.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-log4j.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-starteam.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jmf.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-launcher.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-regexp.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-stylebook.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-commons-net.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-weblogic.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-testutil.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-resolver.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-bcel.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jai.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-bsf.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-jdepend.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-trax.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-apache-oro.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-commons-logging.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-javamail.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-swing.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/ant-nodeps.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/xercesImpl.jar:/home/kev/workspace/ant-core-171/bootstrap/lib/xml-apis.jar:/usr/lib/jvm/java-6-sun-1.6.0.06/lib/tools.jar/"/>
<pathelement path="${additional.path}"/>
</path>
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
<target ... >
<javac ...>
<classpath refid="project.class.path"/>
</javac>
</target>
</project>
|
所有使用 PatternSets, FileSets, ZipFileSets 或 path-like 结构嵌套元素的任务都可以使用上例中的方式进行引用。在 task 中使用 refid 属性会有同样的效果(引用一个已经定义的 task ),但用户应该明白这个属性的解析依赖它引用的元素的实现。一些 task (例如 property task )故意的为 refid 设置了不同的含义。