Transcript
Page 1: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

Using Java AntUsing Java Ant

Andy VerkeynAndy VerkeynDepartment of Information TechnologyDepartment of Information Technology

Ghent UniversityGhent University

April 2003April 2003Version 1.0Version 1.0

Page 2: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

2Andy Verkeyn, Ghent University UGent-INTEC

ContentsContents

�� IntroductionIntroduction� What is ant?� How to use ant?

� Project skeleton, Running ant� Basics

� Targets, Properties, Built-in tasks� Example

�� Advanced topicsAdvanced topics� Specifying directories and files� Sample tasks� Using own tasks

Page 3: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

3Andy Verkeyn, Ghent University UGent-INTEC

ContentsContents

�� IntroductionIntroduction� What is ant?� How to use ant?

� Project skeleton, Running ant� Basics

� Targets, Properties, Built-in tasks� Example

�� Advanced topicsAdvanced topics� Specifying directories and files� Sample tasks� Using own tasks

Page 4: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

4Andy Verkeyn, Ghent University UGent-INTEC

What is ant ?What is ant ?

�� AntAnt� A free, java based build tool (cfr. make)� Does not rely on shell commands� Executes java classes

� Platform independent� Uses XML configuration files� ANT = Another Neat Tool (or ants = good at building things)� More info: http://ant.apache.org

�� HistoryHistory� Created by James Duncan Davidson as part of the Tomcat

(webserver) project� First official stand alone release in Jul. 2000 (version 1.1)

Page 5: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

5Andy Verkeyn, Ghent University UGent-INTEC

What is XML ?What is XML ?

�� XML = Extensible Markup LanguageXML = Extensible Markup Language� Standardized by W3C (World Wide Web Consortium)� Looks a lot like HTML... but better structured!

�� Syntax rulesSyntax rules� Elements are formed by a start tag and a matching end tag

<element> Text </element>

Empty element : <element/>� There must be a single root element� All elements are properly nested in the root (tree-structure)� A start tag can contain attributes with double quoted values

<element att1=�val1� att2=�val2�> Text </element>

� Case sensitive

Page 6: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

6Andy Verkeyn, Ghent University UGent-INTEC

Ant project XML skeletonAnt project XML skeleton

�� SkeletonSkeleton<project name=�myproj� default=�mytarget� basedir=�.�><description>A simple example</description>

<target name=�mytarget� description=�Sample�><taskname id=�taskid� attrib1=�value1�></taskname>

</target></project>

�� RemarksRemarks� Only the project-default attribute is required.� The project-description element is optional.

Page 7: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

7Andy Verkeyn, Ghent University UGent-INTEC

Running antRunning ant

�� Running antRunning ant� ant [options] [target]

� Default: searches for build.xml in the current directory� If no target is specified, the default project target is used.

�� OptionsOptions� -buildfile filename: uses filename instead of build.xml� -f filename

� -find filename: searches for filename from the current directory towards the root until it is found

� -Dproperty=value: sets or overwrites a property

Page 8: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

8Andy Verkeyn, Ghent University UGent-INTEC

Basics: Target elementBasics: Target element

�� SyntaxSyntax<target name=�C� depends=�B,A� description=�Sample�>

�� RemarksRemarks� Only the name attribute is required.� The depends targets are executed from left to right.

�� Optional attributesOptional attributes� if=�property�

� unless=�property�

� The target is only executed �if� or �unless� the property is set. The value of the property is not important, only that is has been set.

Page 9: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

9Andy Verkeyn, Ghent University UGent-INTEC

Basics: Using propertiesBasics: Using properties

�� Property elementProperty element<property name=�version� value=�1.0� /><property name=�build� location=�build� /><property name=�class� location=�${build}/classes� /><property file=�foo.properties� />

�� RemarkRemark� The attribute location always resolves to an absolute path.� Ant properties are immutable!

�� BuiltBuilt--in propertiesin properties� Java system properties (E.g.: os.name, user.home,...)� basedir: see project tag� ant.file: absolute path of the build file� ant.version, ant.project.name, ant.java.version

Page 10: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

10Andy Verkeyn, Ghent University UGent-INTEC

Basics: BuiltBasics: Built--in tasksin tasks

�� Compile tasksCompile tasks� javac� rmic

�� Archive tasksArchive tasks� jar, zip, unzip

�� Documentation tasksDocumentation tasks� javadoc

�� Execution tasksExecution tasks� ant, antcall� java

�� File tasksFile tasks� copy, delete, move� mkdir

�� Property tasksProperty tasks� basename, dirname� condition� property

�� Miscellaneous tasksMiscellaneous tasks� echo� input

�� It is also possible to write your own tasks in javaIt is also possible to write your own tasks in java

Page 11: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

11Andy Verkeyn, Ghent University UGent-INTEC

ExampleExample

<project name="MyProject" default="dist" basedir="."><description>Simple example build file </description><property name="src" location="src"/><property name="build" location="build"/><property name="dist" location="dist"/><target name="compile" description="compile source"><mkdir dir="${build}"/><javac srcdir="${src}" destdir="${build}"/>

</target><target name="dist" depends="compile�><mkdir dir="${dist}/lib"/><jar destfile="${dist}/lib/MyProject.jar�

basedir="${build}"/></target>

</project>

Page 12: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

12Andy Verkeyn, Ghent University UGent-INTEC

ContentsContents

�� IntroductionIntroduction� What is ant?� How to use ant?

� Project skeleton, Running ant� Basics

� Targets, Properties, Built-in tasks� Example

�� Advanced topicsAdvanced topics� Specifying directories and files� Sample tasks� Using own tasks

Page 13: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

13Andy Verkeyn, Ghent University UGent-INTEC

Specifying directories and filesSpecifying directories and files

�� Specifying filesSpecifying files<fileset dir=�basedir�

includes=�*.class,**/*.gif�>

excludes=�*.java�></fileset>

�� RemarkRemark� Only the dir attribute is required.

�� WildcardsWildcards� * and ? : as usual� ** : matches zero or more subdirectories� shorthand: �subdir/� is expanded to �subdir/**�.

Page 14: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

14Andy Verkeyn, Ghent University UGent-INTEC

Specifying directories and filesSpecifying directories and files

�� Using elementsUsing elements<fileset dir=�basedir�><include name=�*.class� />

<include name=�**/*.gif� /><exclude name=�*.java� />

</fileset>

�� RemarksRemarks� Only one pattern can be specified with these elements.� Additionally, they accept the if and unless attributes.

�� Specifying directoriesSpecifying directories� Same syntax with dirset element instead of fileset.

Page 15: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

15Andy Verkeyn, Ghent University UGent-INTEC

Specifying directories and filesSpecifying directories and files

�� Specifying path structuresSpecifying path structures<path id=�base.path"><pathelement path=�.;myApp� />

<pathelement location=�lib/helper.jar� /></path><path id=�test.path">

<pathelement refid=�base.path� /><pathelement location=�testclasses� />

</path>

�� AttributesAttributes� path: a list of �:� or �;� separated paths.� location: single directory or jar file.� refid: includes a previously defined path structure

Page 16: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

16Andy Verkeyn, Ghent University UGent-INTEC

Specifying directories and filesSpecifying directories and files

�� Examples of path structure elements supporting Examples of path structure elements supporting the same syntaxthe same syntax� classpath, sourcepath, ...

�� Notational shortcutNotational shortcut� The path and classpath elements also accept the attributes

path and location directly� <path id="base.path" path="${classpath}"/>

�� Combined elementsCombined elements� Dirsets and filesets can be specified as nested elements

Page 17: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

17Andy Verkeyn, Ghent University UGent-INTEC

Specifying directories and filesSpecifying directories and files

�� ExampleExample<classpath><pathelement path="${classpath}"/>

<fileset dir="lib"><include name="**/*.jar"/>

</fileset>

<pathelement location="classes"/><dirset dir="${build.dir}"><include name="apps/**/classes"/>

<exclude name="apps/**/*Test*"/></dirset>

</classpath>

Page 18: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

18Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: javacSample tasks: javac

�� SyntaxSyntax<javac srcdir=�sourcedir� includes=�...� excludes=�...�

destdir=�build� classpath=�.:my.jar�

deprecation=�on� listfiles=�on�></javac>

�� RemarksRemarks� It is also allowed to specify only srcdir as attribute and use

the include and exclude fileset elements.� srcdir and classpath are path-like structures and can be

defined using nested elements src and classpath.� Only java files without a class file or class files that are older

than their java file will be compiled.

Page 19: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

19Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: rmicSample tasks: rmic

�� SyntaxSyntax<rmic base=�classes� includes=�**/Remote*.class�

excludes=�...� classpath=�.:my.jar� /><rmic base=�classes� classname=�package.MyClass�

classpath=�.:my.jar� />

�� RemarksRemarks� base: the directory to put the compiled files (required)� classname: name of a single class to compile.� Only base is required.� It is also allowed to specify only base as attribute and use

the include and exclude fileset elements.� classpath is a path-like structure and can be defined using

the nested element classpath.

Page 20: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

20Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: jarSample tasks: jar

�� SyntaxSyntax<jar destfile=�test.jar� basedir=�class�

includes=�*.class,*.gif� excludes=�*.java,*.txt�

manifest=�test.mf�></jar>

�� Specifying source filesSpecifying source files� One or more fileset elements can be used instead of the basedir, includes and excludes attributes.

� It is also allowed to specify only basedir as attribute and use the include and exclude elements.

Page 21: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

21Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: jarSample tasks: jar

�� The manifest file can also be included inlineThe manifest file can also be included inline<jar destfile=�test.jar�><fileset dir=�class�>

<include name=�*.class /><include name=�.gif� /><exclude name=�*.java />

<exclude name=�*.txt� /></fileset><manifest>

<attribute name=�Manifest-Version� value=�1.0� /><attribute name=�Built-By� value=�${user.name}� />

</manifest>

</jar>

Page 22: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

22Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: javadocSample tasks: javadoc

�� SyntaxSyntax<javadoc sourcepath=�.� sourcefiles=�f1.java,f2/*.java�

destdir=�docs�

windowtitle=�...� doctitle=�...�overview=�true� version=�true� author=�true�nodeprecated=�true� />

�� RemarksRemarks� At least one of the sourcepath and sourcefiles attributes

or a nested fileset element is required.� Several attributes are also allowed as nested elements, e.g.

doctitle.� sourcepath is a path-like structure and can be defined using

the nested element sourcepath.

Page 23: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

23Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: javadocSample tasks: javadoc

�� ExampleExample<javadoc destdir="docs/api" windowtitle="Test API"

version="true" author="true�>

<fileset dir="src"><include name="com/dummy/test/**" /><exclude name="com/dummy/test/doc-files/**"/>

</fileset><doctitle>Test</doctitle>

</javadoc>

Page 24: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

24Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: antSample tasks: ant

�� Runs ant on a supplied (subproject) file.Runs ant on a supplied (subproject) file.�� SyntaxSyntax<ant antfile=�subproject/sub.xml" dir=�.� target=�tgt�><property name=�passProperty� value=�propVal�/>

</ant>

�� RemarksRemarks� No attributes are required, defaults

� antfile = �build.xml� (relative to dir directory)� dir = project�s base directory� target = default target of the buildfile.

� By default all properties will be inherited by the subproject� inheritAll=�false�

Page 25: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

25Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: antcallSample tasks: antcall

�� Calls a target within the same buildfileCalls a target within the same buildfile�� SyntaxSyntax<antcall target=�otherTarget�><param name=�passParam� value=�paramVal�/>

</antcall>

�� RemarksRemarks� The target attribute is required.� Properties can be passed using the param element.� After executing the child target, control returns to the caller.

Page 26: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

26Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: javaSample tasks: java

�� Runs a java classRuns a java class�� SyntaxSyntax<java classname=�ClassFile� jar=�JarFile� fork=�true�

classpath=�.:myjar.jar�>

<arg name=�argValue� /></java>

�� RemarksRemarks� One of the attributes classname or jar is required. If a jarfile

is specified, fork must be set true.� fork: launches the Java class in a new JVM.� classpath is a path-like structure and can be defined using

the nested element classpath.

Page 27: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

27Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: copySample tasks: copy

�� Copies one or more filesCopies one or more files�� SyntaxSyntax<copy file=�myfile.java� tofile=�yourfile.java� /><copy todir=�targetdir� overwrite=�true�>

<fileset dir=�sourcedir� /></copy>

�� RemarksRemarks� If the file attribute is used, either tofile or todir must be

set.� If multiple files are specified using one ore more fileset

elements, only the todir attribute is allowed (and required).� Default for overwrite attribute is false.

Page 28: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

28Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: moveSample tasks: move

�� Move one or more files (same syntax as copy)Move one or more files (same syntax as copy)�� SyntaxSyntax<move file=�myfile.java� tofile=�yourfile.java� /><move todir=�targetdir� overwrite=�true�>

<fileset dir=�sourcedir� /></move>

�� RemarksRemarks� If the file attribute is used, either tofile or todir must be

set.� If multiple files are specified using one ore more fileset

elements, only the todir attribute is allowed (and required).� Default for overwrite attribute is false.

Page 29: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

29Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: deleteSample tasks: delete

�� Deletes files and/or directoriesDeletes files and/or directories�� SyntaxSyntax<delete file=�myfile.java� /><delete dir=�toremove� />

<delete><fileset dir=�toremove� />

</delete>

�� RemarkRemark� Either the file attribute, the dir attribute or one or more

fileset elements must be provided.

Page 30: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

30Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: mkdirSample tasks: mkdir

�� Creates a directory. Creates a directory. �� SyntaxSyntax<mkdir dir=�bin/classes� />

�� RemarksRemarks� Parent directories are also created if necessary.

Page 31: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

31Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: basename/dirnameSample tasks: basename/dirname

�� Sets the property to the base file nameSets the property to the base file name<basename property=�jar.file� file=�lib/my.jar�/>

� Result: jar.file=�my.jar�

�� Sets the property to the absolute directory nameSets the property to the absolute directory name<dirname property=�jar.dir� file=�lib/my.jar�/>

� Result: jar.dir=�base_project_directory/lib�

Page 32: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

32Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: conditionSample tasks: condition

�� Sets a property if a condition is trueSets a property if a condition is true�� SyntaxSyntax<condition property=�propName� value=�trueValue�><nested-conditions />

</condition>

�� Nested conditionsNested conditions� <not> <and> <or>� <istrue/isfalse value=�${p}�> <isset property=�p�>

� <available>: true if a class, file or resource exists� <uptodate>: true if a target file is more up to date than a

source file� <equals>: true if two given argument strings are equal

Page 33: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

33Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: conditionSample tasks: condition

�� IfIf--thenthen--else simulationelse simulation<target name=�if-part�><condition property=�if-cond�>

<available file=�name.jar� /></condition><antcall target=�then-part� />

<antcall target=�else-part� /></target>

<target name=�then-part� if=�if-cond�></target>

<target name=�else-part� unless=�if-cond�>

</target>

IFIF

THENTHEN

ELSEELSE

Page 34: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

34Andy Verkeyn, Ghent University UGent-INTEC

Sample tasks: echo/inputSample tasks: echo/input

�� Show messages and ask inputShow messages and ask input<echo message=�Text to show� />

<input message=�Prompt:� validargs=�y,n�addproperty=�answer� />

�� RemarksRemarks� validargs: list of acceptable input (case sensitive)� addproperty: property containing the given input

Page 35: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

35Andy Verkeyn, Ghent University UGent-INTEC

Using own tasksUsing own tasks

�� Writing own tasks in JavaWriting own tasks in Java� Derive a java class from org.apache.tools.ant.Task.� Write a setter method for each attribute, e.g. message:

public void setMessage(String);

� Ant does simple type conversions (e.g. primitives or String)� To support character data in your element:

public void addText(String);

� To support nested elements, e.g. inner:public InnerImplementation createInner();

� Implement the task:public void execute() throws org.apache.tools.ant.BuildException;

Page 36: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

36Andy Verkeyn, Ghent University UGent-INTEC

Using own tasksUsing own tasks

�� ExampleExampleimport org.apache.tools.ant.BuildException;import org.apache.tools.ant.Task;

public class MyOwnTask extends Task {private String msg;public void execute() throws BuildException {

System.out.println(msg);}public void setMessage(String msg) {

this.msg = msg;}

}

Page 37: Using Java Ant - verkeyn.net · Using Java Ant Andy Verkeyn. Department of Information Technology. Ghent University. ... is specified, fork must be set true. Œfork: launches the

37Andy Verkeyn, Ghent University UGent-INTEC

Using own tasksUsing own tasks

�� DeployingDeploying� Include your class in the classpath before invoking ant� Add a taskdef element to the project� Use the task as other ant tasks

�� ExampleExample<project name="OwnTaskExample" default="main�

basedir="."><taskdef name="mytask� classname="MyOwnTask"/><target name="main">

<mytask message="Hello World!"/></target>

</project>


Top Related