Ant Build Tools. Creating a product from source may take several steps: Compile Link Copy files to...

22
Ant Build Tools

Transcript of Ant Build Tools. Creating a product from source may take several steps: Compile Link Copy files to...

Page 1: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Ant

Build Tools

Page 2: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

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 it’s boring, second because it is error-prone.

The objective should be an automated tool that does all the work for you. Type or click one command and create a final product.

Page 3: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

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 Use a tool designed for the task

Make Ant

Page 4: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Make Make is the original tool for this sort of thing.

Roots in the Unix world The stuff to the left of the colon is the “target”,

the stuff to the right the “dependents”, and the lines below that the actions to take

If a target is newer than its dependents, the actions are performed

lib.a: x.o y.o z.o ar rvu lib.a x.o y.o z.o ranlib lib.a

Page 5: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Make

Make has some well-known pathologies The action lines MUST start with a tab,

which is impossible to see The action lines have platform-

dependent scripting dependencies

Page 6: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Ant The platform restrictions weren’t so

bad in the Unix world, but Java was intended to be cross-platform. A build file using Make couldn’t transition from a Unix to a windows box to a Mac box. Make tends to be C- and Unix-centric.

So ant was developed as a cross-platform build tool

Page 7: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

What File Format to use? If you’re developing a new tool, what

should the syntax of the file be? The world needs fewer file formats. If you

write your own unique syntax, like Make files, you wind up having to parse that syntax. That usually means a trip to yacc, lex, and friends.

Ant choose to use XML rather than develop its own syntax. This lets people leverage their existing knowledge of XML rather than learn a bunch of new rules

Page 8: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Ant

Ant is a cross-platform, XML-based system for creating software products from source code.

It is NOT a scripting language. But that doesn’t stop some people.

Open source, available at www.apache.org

Page 9: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Installing Ant

Get the binary release and set ANT_HOME and JAVA_HOME environment variables. Put the ant bin directory on the PATH environment variable.

Page 10: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Example Fragment

Creates some directories to hold the output of the build process. Matching <target> and </target> tags Solo tags ended with “/>” Uses XML tag attributes

<target description="Preparatory actions for compilation and documentation targets" name="init">

<mkdir dir="build"/><mkdir dir=“build/lib"/><mkdir dir= “build/classes"/><mkdir dir=“build/doc"/>

</target>

Page 11: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Targets

A “target” is something that needs to be done—create 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

Page 12: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

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>

Page 13: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Properties

You can define variables so things like directory names aren’t hardcoded through your ant file.

<property description="Source directory" location="${basedir}/source" name="dir.src"/>

Page 14: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

A Simple Compile Task Compile the source directory, put the results in

the build/classes directory, and use the specified jar file

<target depends="init" name="compile"> <!--Compile everything in the source directory -> <javac destdir="${dir.build.classes}"> <classpath> <pathelement location=“${lib.dir}/somelib.jar” </classpath> <src path="${dir.src}"/> </javac></target>

Page 15: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Ant Files Often you need to specify lists of files (as in

classpaths). It is brittle to do this by using a long quoted series of files Path=“foo.jar,baz.jar,xml.jar,…”

Ant can handle this by using lists of XML tags

<Classpath> can have multiple <pathelement> tags embedded.

This is somewhat more maintainable than a single quoted list.

Page 16: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

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. You can also use regexps to select files in the fileset.<classpath>

<fileset dir="${dir.lib}" includes="**/*.jar"/></classpath>

Page 17: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Popular Ant Tasks Javac Javadoc jar Mkdir Copy Delete Junit (automated tests) FTP (FTP a result to a server)

Page 18: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

User-Written tasks

You can also write your own tasks with fairly low quantities of drama

This has been used to do things like automatically deploy servlets to a tomcat server as part of the build process

Page 19: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Ant files

Ant files are usually named “build.xml” and usually reside in the root of your project directory.

To run, type “ant <targetname>”.

Page 20: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Simple Example

Write an ant file for a simple project Source directory, compile to a build

directory, do jar and javadoc Tasks for init, compile, jar, javadoc,

cleanFoo

build.xmlsource

a.javab.java

buildclassesdocproject.jar

Page 21: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Simple Example

See http://www.movesinstitute.org/~mcgredo/ant/example.tar.gz for the example with a simple project

Page 22: Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.

Parting thoughts

Don’t depend on manual build steps if at all possible

The objective should be typing or clicking “ant” which leads to a functioning product from source

It’s possible, but probably a bad idea, to use ant for C++/C