Chapter 3 Understanding Ant datatypes and properties.

22
Chapter 3 Understanding Ant datatypes and properties

Transcript of Chapter 3 Understanding Ant datatypes and properties.

Chapter 3 Understanding Ant

datatypes and properties

Ant datatype

An Ant datatype is equivalent to a Java class Behind the scenes they are actually

implemented as such Datatypes store complex pieces of

information used in the build For example

Datatypes can store a list of files to compile

Datatypes can store a set of directories to delete

Ant datatype

The datatypes act as a parameter to tasks

You can declare them inside a task or define them outside, give them a name, and then pass that name to a task

A typical Ant build has to handle files and paths, especially the notorious classpath

The fileset and path datatypes crop up throughout Ant build files

Example use of fileset datatype

<fileset id="source.fileset“ dir="src" includes=“**/*.java" This specifies all java files under all sub-

directories of dir src By providing an id attribute, we’re defining a

reference, this reference name can be used later wherever a fileset is expected

For example, copying our source code to another directory:

<copy todir="backup"><fileset refid="source.fileset"/>

</copy>

Datatype path

A path is an ordered list of elements, where each element can be a file or a directory

It describes paths such as the Java CLASSPATH, or the PATH environment variable of Unix and Windows.

It may have a task-specific name, such as <classpath> above, or it may just have the simple name <path>

Datatype path

Example<path>

<pathelement location= "lib/junit.jar" />

</path>

This definition contains one element, whose location attribute can specify a single file or directory

Datatype path

If you need to specify multiple files or directories, you may use path instead of location

For example<path>

<pathelement path=“build/classes;lib/junit.jar” />

</path> This path attribute separates its parameters into

individual elements, using either a semicolor (;) or colon (:) to split them

Datatype path

Directories used in datatype path can be separated by either a forward slash (/) or a back-slash (\), regardless of operating systems A build file should not have to care about what

system it runs on

Datatype path

One exception related to the property file However, if you are using Windows for your

homework/lab, in your property file build.properties for ANT, you need to make sure using "/" in your path.

Do not use "\" in your path in your property file for Windows (Inside Ant build file build.xml, it does not matter if you use "\" or "/" in your path).

Datatype path

For example, following build.properties file has the correct path.---------build.properties-------------tomcat.home=c:/apache-tomcat-6.0.18application=lab5-1--------------------------------------

If you use tomcat.home=c:\apache-tomcat-6.0.18, it will cause trouble to you when deploy it. In this case, what happened is that Ant will ignore "\" and deploy everything to a directory tree under your current directory where build.xml is located.

Datatype path

If a path structure consists of only a single path or location, it can be specified using a shortcut form as in

<path location=“lib/junit.jar” />

or with multiple files separated by either the : or ; path separator

<path path=“build/classes:lib/junit.jar” />

Datatype path

Paths can include a set of files

<path>

<fileset dir=“lib”>

<include name=“*.jar” />

<fileset>

</path>

Datatype path

How to use a path? A standalone path declaration can be given a

name via its id attribute<path location=“lib/junit.jar” id=“junit.path” />

The name can be referenced whenever a path is needed

<path refid=“junit.path” /> The other way to use a path is inline

For example, javac task has the element <classpath>

Datatype fileset

A fileset is a set of files rooted from a single directory This includes all the files in the directory tree,

including files in all subdirectories recursively For example

<copy todir=“newweb”> <fileset dir=“web” /></copy>

All files from the web dir (including sub-directories) are copied to the newweb directory

Datatype fileset

A fileset can contain multiple patternsets which restricts the files in the fileset to those that match or don’t match a specified pattern

For example, to include all jsp files in a dir<copy todir=“newweb”>

<fileset dir=“web” includes=“**/*.jsp” />

</copy> Had we only specified just *.jsp, only the jsp files in

the web dir would have been copied, but the files in any subdir would not have been copied

Datatype fileset

Files can also be excluded using attribute exclude

For example

<patternset> <include name=“**/*.jsp” />

<exclude name=“**/test/order*.jsp” />

</patternset>

Ant property

An Ant property represents any string-specified itemAnt properties are essential not just to share information around the build, but to control build files from the outsideExample<property name="host" value="localhost" /> After defining this property, if you want to use the property later, you

can use ${host} For example <echo>host=${host}</echo>

Ant property

Although Ant property’s functionality looks like a programming language’s variable, ant property is immutable: you cannot change them

The first task, project, or person to set a property fixes it for that build

Use datatype and properties

<project name="mybuild" default="compile"><property name="build.classes.dir" location="build/classes" /><path id="compile.classpath"> <pathelement location="lib/junit.jar" /></path><mkdir dir="${build.classes.dir}" /><target name="compile" > <javac

destdir="${build.classes.dir}"debug="true"srcdir="src">

<classpath refid="compile.classpath" /> </javac></target>

</project>

We set a property to the directory where we want to compile our source

We declare a datatype path for JARs to use in the compile

${…} denotes the use of the property

<classpath> element declares the classpath for the compiler

Setting a property to a filename or directory name

In our previous example, we use <property name="build.classes.dir" location="build/classes" />

You can actually use <property name="build.classes.dir" value="build/classes" />

It essential does the same thing in this example

Using attribute location sets the property to the absolute filename of the given file. If the value of this attribute is an absolute path, it is left unchanged (with / and \ characters converted to the current platforms conventions). Otherwise it is taken as a path relative to the project's basedir and expanded.

Using attribute value sets the property to be whatever value that is given. No automatic absolute path conversion involved.

Flexibility of Ant Propertiesant -Dapplication=fly

Buildfile: C:\j2ee_class\lab4-3\build.xml

init:

[mkdir] Created dir: C:\j2ee_class\lab4-3\classes

[mkdir] Created dir: c:\tomcat-6.0.26\webapps\fly

[mkdir] Created dir: c:\tomcat-6.0.26\webapps\fly\WEB-INF

[mkdir] Created dir: c:\tomcat-6.0.26\webapps\fly\WEB-INF\classes

compile:

[javac] C:\j2ee_class\lab4-3\build.xml:27: warning: 'includeantrunt

ds

deploy:

[copy] Copying 2 files to c:\tomcat-6.0.26\webapps\fly

[copy] Copying 1 file to c:\tomcat-6.0.26\webapps\fly\WEB-INF

BUILD SUCCESSFUL

Total time: 0 secondspplication=fly

Although I have a build.properties file having the following contents:

tomcat.home=c:/tomcat-6.0.26

application=lab4-3

• I can run

ant -Dapplication=fly

to make it deploy files to a different place without changing the build.properties file.

Flexibility of Ant Properties

The reason that Ant makes properties to be immutable (you cannot change them) is that

It let you control build files from the outsideCommand line

IDEs

Automated build systems

Whoever sets a property first fixes its value. This is the direct opposite of variables in a program, where the last assignment becomes its value

Ant’s property override rule turns every property into a place in which someone can deliberately override the build file to meet their needs