Java 2EE and XML Development

Скачать в pdf «Java 2EE and XML Development»


Ant dynamically constructs path variables by evaluating one or more <pathelement> and/or <fileset> elements contained within a <path> definition. A <pathelement> points at a file system directory, and adds all the contents of that directory to the path being defined. A <fileset> is more powerful, allowing you to filter the contents of a directory being added to the path. You can specify files to be excluded either explicitly or using regular expressions.


To see how this works, let us look at an example. Suppose you define a <path> element in your build file as follows:


<path id=»project.class.path»>


<pathelement path=»${build.dir}»/>


<fileset dir=»lib»>


<include name=»**/*.jar»/>


<include name=»**/*.zip»/>


</fileset>


</path>


Ant creates a path variable with an id (name) of project.class.path. The path includes all files and directories beneath the ${build.dir} directory (which you probably set earlier in the file via a global property). It also contains all ZIP and JAR files found in the lib relative directory and any subdirectories. And if you wanted to exclude a specific JAR file from the path, deprecated.jar, for example, you could add the following node to the fileset definition:


<exclude name=deprecated.jar/>


Once your globally declared path variable has been constructed, it can be referenced from tasks using its id attribute. To use the path above when compiling a Java class via the javac task, you would refer to it as follows:


<javac srcdir=»${src.dir}» classpathref=,,project.class.path» />


If you have done any Java development from the command line using the JDK, you will no doubt appreciate the power and flexibility of dynamically constructed paths, and specifically the capabilities of the <fileset> element.

Скачать в pdf «Java 2EE and XML Development»