Ant Datatype之Path

Datatype在Ant中扮演任务参数的角色,可以在一个任务内部声明,也可以在任务外部定义,并为它起个名字,再把这个名字传给任务。这样就可以在多个任务中共享一个Datatype。

Datatype可以存储大量在构建过程中使用的复杂信息,比如一个待编译的文件列表或者一个待删除的目录集。

path是一个有序的元素列表,这些元素可以是文件或目录。它的名字可能与任务相关,或者只是简单<path>。

1. 如果路径只包含一个path或location,可以简写为,其中location都可换为path:

        Java文件:

package xuj.ant;

import org.apache.commons.lang.StringUtils;

public class Main
{
	public static void main(String[] args){
		System.out.println(StringUtils.capitalize("test"));
	}
}

        build.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="datatype_path" default="execute">

	<!--<path id="compile.classpath" location="lib/commons-lang-2.4.jar" />-->

	<path id="compile.classpath">
		<pathelement location="lib/commons-lang-2.4.jar"/>
	</path>

	<target name="init" description="创建项目文件夹">
		<mkdir dir="build/classes"/>
	</target>

	<target name="compile" depends="init" description="编译Java源文件">
		<javac includeAntRuntime="false" srcdir="src" destdir="build/classes">
			<classpath refid="compile.classpath"/>
		</javac>
		<echo level="info">compilation complete!</echo>
	</target>

	<target name="clean" description="清除项目文件夹">
		<delete dir="build"/>
		<delete dir="dist"/>
	</target>

	<target name="execute" depends="compile" description="运行项目">
		<java classname="xuj.ant.Main" classpath="build/classes">
			<classpath refid="compile.classpath"/>
		</java>
	</target>
</project>

         输出:

>ant
Buildfile: E:\antspace\datatype\build.xml

init:
    [mkdir] Created dir: E:\antspace\datatype\build\classes

compile:
    [javac] Compiling 1 source file to E:\antspace\datatype\build\classes
     [echo] compilation complete!

execute:
     [java] Test

BUILD SUCCESSFUL
Total time: 1 second

2. 如果是多个文件,可以用“;”或者“:”分隔,或一个文件集合,或单独写每个path:

        Java文件:

package xuj.ant;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.Email;

public class Main
{
	public static void main(String[] args){
		System.out.println(StringUtils.capitalize("test"));
		System.out.println(Email.MAIL_SMTP_TIMEOUT);
	}
}

        build.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project name="datatype_path" default="execute">

	<!--方法一
		<path id="compile.classpath" path="lib/commons-lang-2.4.jar;lib/commons-email-1.2.jar" />
	-->

	<!--方法二
	<path id="compile.classpath">
		<pathelement location="lib/commons-lang-2.4.jar"/>
		<pathelement location="lib/commons-email-1.2.jar"/>
	</path>
	-->
	<!-- 方法三 -->
	<path id="compile.classpath">
		<fileset dir="lib">
			<include name="*.jar"/>
		</fileset>
	</path>
	
	<target name="init" description="创建项目文件夹">
		<mkdir dir="build/classes"/>
	</target>

	<target name="compile" depends="init" description="编译Java源文件">
		<javac includeAntRuntime="false" srcdir="src" destdir="build/classes">
			<classpath refid="compile.classpath"/>
		</javac>
		<echo level="info">compilation complete!</echo>
	</target>

	<target name="clean" description="清除项目文件夹">
		<delete dir="build"/>
		<delete dir="dist"/>
	</target>

	<target name="execute" depends="compile" description="运行项目">
		<java classname="xuj.ant.Main" classpath="build/classes">
			<classpath refid="compile.classpath"/>
		</java>
	</target>
</project>

        或

<?xml version="1.0" encoding="UTF-8"?>
<project name="datatype_path" default="execute">

	<path id="compile.classpath1" location="lib/commons-lang-2.4.jar" />
	<path id="compile.classpath2" location="lib/commons-email-1.2.jar" />
	
	<target name="init" description="创建项目文件夹">
		<mkdir dir="build/classes"/>
	</target>

	<target name="compile" depends="init" description="编译Java源文件">
		<javac includeAntRuntime="false" srcdir="src" destdir="build/classes">
			<classpath refid="compile.classpath1"/>
			<classpath refid="compile.classpath2"/>
		</javac>
		<echo level="info">compilation complete!</echo>
	</target>

	<target name="clean" description="清除项目文件夹">
		<delete dir="build"/>
		<delete dir="dist"/>
	</target>

	<target name="execute" depends="compile" description="运行项目">
		<java classname="xuj.ant.Main" classpath="build/classes">
			<classpath refid="compile.classpath1"/>
			<classpath refid="compile.classpath2"/>
		</java>
	</target>
</project>

       输出:

>ant
Buildfile: E:\antspace\datatype\build.xml

init:
    [mkdir] Created dir: E:\antspace\datatype\build\classes

compile:
    [javac] Compiling 1 source file to E:\antspace\datatype\build\classes
     [echo] compilation complete!

execute:
     [java] Test
     [java] mail.smtp.timeout

BUILD SUCCESSFUL
Total time: 1 second

3.最终的build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="datatype_path" default="execute">
	
	<property name="build.dir" location="build/classes"/>

	<path id="compile.classpath">
		<pathelement location="lib/commons-lang-2.4.jar"/>
		<pathelement location="lib/commons-email-1.2.jar"/>
	</path>
	
	<target name="init" description="创建项目文件夹">
		<mkdir dir="${build.dir}"/>
	</target>

	<target name="compile" depends="init" description="编译Java源文件">
		<javac includeAntRuntime="false" srcdir="src" destdir="${build.dir}">
			<classpath refid="compile.classpath"/>
		</javac>
		<echo level="info">compilation complete!</echo>
	</target>

	<target name="clean" description="清除项目文件夹">
		<delete dir="build"/>
		<delete dir="dist"/>
	</target>

	<target name="execute" depends="compile" description="运行项目">
		<java classname="xuj.ant.Main" classpath="${build.dir}">
			<classpath refid="compile.classpath"/>
		</java>
	</target>
</project>