Professional Documents
Culture Documents
Build Tools
Creating a product from source may take several steps:
Compile Link Copy files to various directories Remove intermediate files Generate documentation
It becomes problematic to do all these steps manually, first of all because its boring, second because it is errorprone. The objective should be an automated tool that does all the work for you. Type or click one command and create a final product.
Copyright 2004, Java CoE, All Rights Reserved 2
Build Tools
There are a couple ways this can be done:
Write a batch file or script
The scripts tend to be hard to maintain
What is Ant?
Ant is a cross-platform, XML-based system for creating software products from source code. Open Source (development coordinated by the Apache Jakarta project)
Why Ant?
Platform independent
Requires only a JDK 1.1 or later JVM
Easy to use
Built-in tasks accomplish all typical build functions User contributed tasks cover most other needs
Easy to extend
Creating a new Ant task is similar in scope to writing a servlet
Copyright 2004, Java CoE, All Rights Reserved 5
Structure of Ant
Project
a top level collection of targets
Property
an Ant variable
Target
a collection of tasks executed to achieve a particular purpose (a goal)
Task
a unit of Ant execution (a step)
Copyright 2004, Java CoE, All Rights Reserved 6
Each Target is executed only once Each Target will contain one or more Tasks Some Tasks are executed conditionally Task are implemented as Java classes
7
Properties
You can define variables so things like directory names arent hardcoded through your ant file.
<property description="Source directory" location="${basedir}/source" name="dir.src"/>
Project
<project name="MyProject" default="dist" basedir="."> ... </project> The default attribute is required The name and basedir attributes are optional
Targets
A target is something that needs to be donecreate initial directories, compile source, create javadoc, create jar files, etc. Targets can depend on other targets
The compile task can depend on an init task that creates directories for the products to land in
10
Target
<target name="dist" depends="init" description="Makes a distribution" if="code-present" unless="time-is-short"> ... </target> The name attribute is required The depends, description, if and unless attributes are optional
11
Target Dependencies
You can add a comma-delimited list of other targets that depend on this target. If the dependent target depends on other things, those will run, too.
<target name=init description=Create directories> </target> <target name="compile depends="init description=Compile Java sources> </target> <target name=jar depends=compile description=Create jar files> </target>
Copyright 2004, Java CoE, All Rights Reserved 12
Filesets
Often you want to specify a whole series of files, as with the classpath example, when many jar files needed to be specified Rather than individually name every jar file, you can specify a set of files, in this case in the lib directory and all subdirectories of the lib directory.
<classpath> <fileset dir="${dir.lib}" includes="**/*.jar"/> </classpath>
13
File-sets
<fileset dir="./source"> <patternset refid="my.pattern/> </fileset>
14
Path-like Structures
Path
<path id="project.class.path"> <pathelement location="./lib"/> <pathelement path="${classpath}"/> </path>
Classpath
<classpath refid="project.class.path">
15
16
This file will compile all .java files in the current directory and all its subdirectories that dont have corresponding .class files that are newer.
Copyright 2004, Java CoE, All Rights Reserved 17
Built-in Tasks
Ant AntCall AntStructure Apply Available Chmod Copy Cvs Delete Echo Exec ExecOn Fail Filter FixCRLF GenKey Get GUnzip
18
20
Optional Tasks
ANTLR Cab Depend FTP JavaCC Javah JJTree Jlink JUnit JUnitReport Native2Ascii PropertyFile RenameExtensions Script Sound Stylebook Telnet Test
21
Plan the attributes, text and child elements that your task element will use For each attribute, add a set method
public void setAttrName(type attrName)
compile the class insure that it can be found using the CLASSPATH environment variable
Copyright 2004, Java CoE, All Rights Reserved 23
Resources
http://jakarta.apache.org/ant/
Ant home page
http://jakarta.apache.org/ant/manual/index. html
On-line Ant User Manual
http://jakarta.apache.org/ant/resources.ht ml
Links to articles, presentations, and FAQs
Copyright 2004, Java CoE, All Rights Reserved 24