--there should be no blank lines or whitespaces before the xml declaration.

--All buildfiles require the project element and at least one target element. --The XML element project has three attributes : Attributes Description 1. name -- The Name of the project. (Optional) 2. default -- The default target for the build script. A project may contain a ny number of targets. This attribute specifies which target should be considered as the default. (Mandatory) 3. basedir -- The base directory (or) the root folder for the project. (Option al) --A target is a collection of tasks that you want to run as one unit. In our example, we have a simple target to provide an informational message to the user. Targets can have dependencies on other targets. For example, a deploy target may have a dependency on the package target, and the package target may have a depe ndency on the compile target and so forth. Dependencies are denoted using the de pends attribute. For example: <target name="deploy" depends="pacakge"> .... </target> <target name="pacakge" depends="clean,compile"> .... </target> <target name="clean" > .... </target> <target name="compile" > .... </target> --The target element has the following attributes: Attributes Description 1. name -The name of the target (Required) 2. depends -Comma separated list of all targets that this target depends on. (Optional) 3. description -A short description of the target. (optional) 4. if -- Allows the execution of a target based on the trueness of a condition al attribute. (optional) 5. unless -- Adds the target to the dependency list of the specified Extension P oint. An Extension Point is similar to a target, but it does not have any tasks. (O ptional) Ant - Property Task --Ant uses the property element which allows you to specify properties. --This allows the properties to be changed from one build to another. or from on e environment to another. --By default, Ant provides the following pre-defined properties that can be used in the build file: 1. ant.file 2. ant.version 3. basdir : The full location of the build file. : The version of the Apache Ant installation. : The basedir of the build, as specified in the

ant. the user can define additional properties using the property element. Ant .home : The home directory of Ant installation.basedir attribute of the project element.lib : The full location of the ant jar file. An example is presented below which shows how to define a property called site name: <?xml version="1.test --The contents of the build property file are similar to the normal java propert y file.core.Property Files --Setting properties directly in the build file is okay if you are working with a handful of properties. .You are at ${sitename} </echo> </target> </project> Ant .Data Types --The following is a list of data types(set of services that are built into the product already) provided by Apache Ant: 1. ant. as specified in the name atrribut e of the project element.version} . You could create multiple build proper ties file based on the deployment environments . Comments are listed using the hash (#) character. ant. for a large project.such as build. They contain one property per line.project. 6. it makes sense to store the proper ties in a separate property file.library.ant. The name and value pair are separated by an equals sign.dir : The home directory for Ant library files . 8.tutorialspoint. ant.properties and is placed along side the build.File Set:--The Fileset data types represents a collection of files. It is highly recommen ded that the properties are annotated with proper comments.project. 4. 10.name : The name of the project. 5.However.default-target : The default target of the current project 7. 9.invoked-targets : Comma separated list of the targets that were i nvoked in the current pro ject.properties.propert ies. --There is no hard and fast rule. ant. Each property is represented by a name and a value pair .project. but typically the property file is named build . The Filese t data type is usually used as a filter to include and exclude files that match a particul ar pattern. --In addition to the above.java.0"?> <project name="Hello World Project" default="info"> <property name="sitename" value="www.version : The version of the JDK that is used by Ant. ant.xml file.typically A NT_HOME/lib folder.dev and build.com"/> <target name="info"> <echo>Apache Ant version is ${ant.

files.xml"/> </filelist> The webapp. ? .without.Matches zero or many characters ** .File List:--The File list data type is similar to the file set except that the File List contains explicitly named lists of files and do not support wild cards Another major difference between the file list and the file set data type is t hat the file list data type can be applied for files that may or may not exist y et.xml"/> <file name="faces-config. In the above example.java"/> <exclude name="**/*Stub*"/> </fileset> The src attribute in the above example points to the source folder of th e project. The casesensitive filter is applied to the fileset which means that a file with the name Samplestub.Matches zero or many directories recursively The following example should give an idea of the usage of a pattern set.xml"/> <file name="portlet.Filter Set:--Using a Filter Set data type with the copy task.java wi ll not be excluded from the fileset 2.Matches one character only * .src.stubs"/> </fileset> 3.src. Following is an example of the File list data type <filelist id="config. . <patternset id="java.folder}"> <file name="applicationConfig. Patterns can be created using the following meta charact ers. 4.stubs"> <include name="src/**/*.files. the fileset selects all java files in the source folde r except those that contain the word 'Stub' in them.xml"/> <file name="web.without.java"/> <exclude name="src/**/*Stub*"/> </patternset> The patternset can then be reused with a fileset as follows: <fileset dir="${src}" casesensitive="yes"> <patternset refid="java.files" dir="${webapp. you can replace certain text in all files that match the pattern with a replacement value.Pattern Set:--A pattern set is a pattern that allows to easily filter files or folders based on certain patterns.folder attribute in the above example points to the web a pplication's source folder of the project.<fileset dir="${src}" casesensitive="yes"> <include name="**/*.

dir" value="war"/> <property name="build. Most commonly.dir}"> <fileset dir="${releasenotes.Path:--The path data type is commonly used to represent a classpath.dir attribute in the above example points to the output folder of th e project.jar"> <pathelement path="${env.dir}/WEB-INF/classes"/> <property name="name" value="fax"/> <path id="master-classpath"> <fileset dir="${web.dir}/WEB-INF/lib"> <include name="*. these characters are replace d a the run time by the running system's path separator character.dir}" includes="**/*.dir}"/> </path> <target name="build" description="Compile source tree java files"> <mkdir dir="${build.dir" value="src"/> <property name="web.A common example is to append the version number to the release notes file.J2EE_HOME}/${j2ee. The releasenotes.jar"/> </fileset> </path> The env.0"?> <project name="fax" basedir=".txt"/> <filterset> <filter token="VERSION" value="${current.jar"/> </fileset> <pathelement path="${build.jar}"/> <fileset dir="lib"> <include name="**/*." default="build"> <property name="src. as shown in the example below: <path id="build. However. as shown in the example below <copy todir="${output. The copy task. The j2ee.dir" value="${web. Ant .Building Projects <?xml version="1.dir attribute in the above example points to the release notes folder of the project.jar attribute in the above example points to the name of the J2EE jar file in the J2EE base folder.J2EE_HOME attribute in the above example points to the environment vari able J2EE_HOME. The current.version attribute in the above example points to the current versio n folder of the project.dir}"/> . 5. as the name suggests is used to copy files from one location to another.version}"/> </filterset> </copy> The output.classpath. the classpath is set to the list of jar files and classes in the project. Entries i n the path are separated using a semicolon or colon.

dir}" source="1.5 as our target compilation).xml. the build. The web. As you know. the javadoc tool is highly flexible and allows a number of configuration options. Attributes: Source can be specified using sourcepath. Ant exposes these configuration options via the javadoc t ask. web. the src.dir}"> <include name="**/*. javascript and other web related files Finally.5"> <src path="${src. In this example.e.<javac destdir="${build. Java makes documentation easier by the use of the in-built javadoc tool. the src. src folder ) . Properties can refer to other properties. Documentation play a great role in the maintenance of a project. Sourcepath is used to point to the folder of source files(eg. Then we execute the javac command (specif ying jdk1. The default target of our project is the compile target. Ant makes it even easier by generating the documentat ion on demand. This is wher e you can find the JSPs. We supply the source folder and the clas spath to the javac task and ask it to drop the class files in the build folder.Build Documentation Documentation is a must for any project. it i ncludes the classes in the build folder and the jar files in the lib folder.5" target="1.dir refers to the web source folder of the project.dir refers to the output folder of the project compil ation. where the java source files can be found).class"/> </fileset> </delete> </target> </project> In this example.dir property.dir refers to the source folder of the project (i.dir}"/> <classpath refid="master-classpath"/> </javac> </target> <target name="clean" description="Clean output directories"> <delete> <fileset dir="${build. In this case.dir refers to the source folder of the project. as the name suggests deletes the files in the build fo lder. The master-classpath holds the classpath information. the build target to build the files. css. Finally.dir property makes a reference to the web. The clean target.sourcepathref or sourcefiles. As shown in the above example. the build. But first let u s look at the clean target. we create th e build directory if it doesn't exist. Ant . First of all.

duplicate: Advises Ant on what to do when duplicate files are found. preserve or fail the duplicate files. excludes: Advises Ant to not include these comma separated list of files in the package. the keepcompression attribute does the same thing.g build. And sourcefiles is used when you want to specify the individual files as a comma separated list. You could add. Below are the extension attributes that are sp ecify to the WAR task: . Creating JAR files with Ant is quite easy with the jar task. this is set to the base directory of the project. i. excludesfile: Same as above.dir}/classes" includes="faxapp/util/**" excludes="**/Test.Source path is used to reference a path that is referenced by path attri bute. Since the WAR task is an extension of the JAR task.jar" basedir="${build.dir}/lib/util. but it applies t o the entire archive.dir) Creating JAR Files: The next logical step after compiling your java source files. destfile: The name of the output JAR file. but it has some nice addit ions to manipulate what goes into the WEB-INF/classes folder. Destination path is specified using the destdir folder (e. includes: Inverse of excludes includesfile: Inverse of excludesfile. By default. Example snippet: <target name="build-jar"> <jar destfile="${web.e the JAR file. Presented below are the commonly used attributes of the jar t ask basedir: The base directory for the output JAR file. and generating the web. all attributes of th e JAR task apply to the WAR task. is to buil d the java archive. compress: Advises ant to compress the file as it creates the JAR file. keepcompression: While the compress attribute is applicable to the indiv idual files. The WAR task is useful to specify a particular layout of the WAR file.Creating WAR files: The WAR task is an extension to the JAR task.xml file.class"> </jar> </target> Ant . update: Advises ant to overwrite files in the already built JAR file. except the exclude files are specified usin g a pattern.

war" webxml="${web.dir}/web. The build.xml file. the folder th at contains the JSP.MF file.This is where the c lasses for the WAR package can be found.dir}/WebContent"> <include name="**/*.*"/> </fileset> <lib dir="thirdpartyjars"> <exclude name="portlet.jar"/> </lib> <classes dir="${build.war. the classes will be bundled into the WEB-INF/classes folder of the WAR file. we are excluding the portlet.dir}/web"/> </war> </target> .e. css. Finally. we are creating a war file called fax. Wrap the war task inside an Ant target (usually package) and run it. The WEB-INF/lib folder is populated with the jar files from the thirdpar tyjars folder.dir variable refers to the output folder . However. In this example. lib-A grouping to specify what goes into the WEB-INF\lib folder. classes-A grouping to specify what goes into the WEB-INF\classes folder. Typically. The WEB. we are copying all classes from the build directory's web folder and putting into the WEB-INF/classes folde r.dir variable refers to the source web folder. Thi s will create the WAR file in the specified location. All files from the 'WebContent' fol der under web are copied into the WAR file.webxml-Path to the web.XML file is obtained from the web source folder.jar as this is already pres ent in the application server's lib folder. <target name="build-war"> <war destfile="fax. the web.javascript files etc.xml"> <fileset dir="${web. metainf-Specifies the instructions for generating the MANIFEST. i.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times