Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Understanding Ant datatypes and properties.

Similar presentations


Presentation on theme: "Chapter 3 Understanding Ant datatypes and properties."— Presentation transcript:

1 Chapter 3 Understanding Ant datatypes and properties

2 Ant datatype An Ant datatype is equivalent to a Java class Behind the scenes they are actually implemented as such Behind the scenes they are actually implemented as such Datatypes store complex pieces of information used in the build Datatypes store complex pieces of information used in the build For example For example Datatypes can store a list of files to compile Datatypes can store a list of files to compile Datatypes can store a set of directories to delete Datatypes can store a set of directories to delete

3 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

4 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 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 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: For example, copying our source code to another directory:

5 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 above, or it may just have the simple name It may have a task-specific name, such as above, or it may just have the simple name

6 Datatype path Example<path> </path> This definition contains one element, whose location attribute can specify a single file or directory

7 Datatype path If you need to specify multiple files or directories, you may use path instead of location For example <path> </path> This path attribute separates its parameters into individual elements, using either a semicolor (;) or colon (:) to split them This path attribute separates its parameters into individual elements, using either a semicolor (;) or colon (:) to split them

8 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 A build file should not have to care about what system it runs on

9 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. 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). 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).

10 Datatype path For example, following build.properties file has the correct path. ---------build.properties------------- tomcat.home=c:/apache-tomcat-6.0.18 application=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.

11 Datatype path If a path structure consists of only a single path or location, it can be specified using a shortcut form as in or with multiple files separated by either the : or ; path separator or with multiple files separated by either the : or ; path separator

12 Datatype path Paths can include a set of files

13 Datatype path How to use a path? A standalone path declaration can be given a name via its id attribute A standalone path declaration can be given a name via its id attribute The name can be referenced whenever a path is needed The other way to use a path is inline The other way to use a path is inline For example, javac task has the element For example, javac task has the element

14 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 This includes all the files in the directory tree, including files in all subdirectories recursively For example For example </copy> All files from the web dir (including sub-directories) are copied to the newweb directory

15 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> 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 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

16 Datatype fileset Files can also be excluded using attribute exclude For example

17 Ant property An Ant property represents any string- specified item Ant properties are essential not just to share information around the build, but to control build files from the outside Example After defining this property, if you want to use the property later, you can use ${host} After defining this property, if you want to use the property later, you can use ${host} For example For example host=${host} host=${host}

18 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

19 Use datatype and properties </path> <javac <javacdestdir="${build.classes.dir}"debug="true"srcdir="src"> </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 element declares the classpath for the compiler

20 Setting a property to a filename or directory name In our previous example, we use You can actually use You can actually use It essential does the same thing in this example 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.

21 Flexibility of Ant Properties ant -Dapplication=fly Buildfile: C:\j2ee_class\lab4-3\build.xml init: [mkdir] Created dir: C:\j2ee_class\lab4-3\classes [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 [mkdir] Created dir: c:\tomcat-6.0.26\webapps\fly\WEB-INF [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 [mkdir] Created dir: c:\tomcat-6.0.26\webapps\fly\WEB- INF\classescompile: [javac] C:\j2ee_class\lab4-3\build.xml:27: warning: 'includeantrunt [javac] C:\j2ee_class\lab4-3\build.xml:27: warning: 'includeantruntdsdeploy: [copy] Copying 2 files to c:\tomcat-6.0.26\webapps\fly [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 [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.26application=lab4-3 I can runI can run ant -Dapplication=fly ant -Dapplication=fly to make it deploy files to a different place without changing the build.properties file. to make it deploy files to a different place without changing the build.properties file.

22 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 outside It let you control build files from the outside Command line IDEs 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


Download ppt "Chapter 3 Understanding Ant datatypes and properties."

Similar presentations


Ads by Google