You are on page 1of 35

Ant

Pralhad Vernekar
Introduction
• Ant : Primarily a Build tool
• Junit :Used for testing

• Ant and junit are very closely linked with


Extreme Programming
Ant
• Another Neat Tool
• Why Ant?
• A Build tool for Java was needed.
• Ant is similar to make.
• Ant is platform independent, because
– Ant uses java
– Config file is stored as an xml file.
Ant : Need
• Different IDEs
– Eclipse, …
– Each IDE has own style, sometimes own jvm.

– Working on my machine – syndrome.


– There is another person in a relatively bigger project.
– Config manager/Release Manager
Source Control
• Basics of Source Control
– checkin
– checkout
– get
– uncheckout
Source Control
• Concurrency problems using diagrams
Ant : Need
• Works on my machine!
– Can be built by anyone
– Can be packaged by anyone
– Can be deployed by anyone
– Can be tested by anyone.

– Feature : Environment Independent.


Ant : Need
• Repetitive Tasks
• Should be able to run in background. Should
have command line interface.

• Should be able to define dependencies.


• Can we have conditional compiling/packaging
Ant is the tool
• Ant is a
– Command line tool
– Can have dependencies among tasks
– Can be used for compiling
– Can be used by packaging
– Can be used for automated testing
– Tasks can be executed conditionally
– Can be extended by writing new specific tasks.
XP : Refactoring
• What is refactoring
• Break something working and Fix it.
• Or rather try to fix it.
• If it doesn’t work – establish it and go back.
• Results must come out real quick.
• Use Ant!
Ant : Core Concepts
• Project
– E.g. ecomm project
• Targets
– Compile, build
• Tasks
– Javac, copy
• Properties
– In built, local properties
Ant : Some more types
• Paths
– To be used for path, classpath
• Filesets
– Easy way to include all jar files
– Exclude possible
• References
– One variable can used as reference
– E.g. Additional classpath
Build.xml : Picture

Project

props

Paths

Target Compile
Uses javac, classpath taken from paths

Target Package : Depends on Compile


Uses Jar
Type of Tasks
• Compiling/Linking
• javadoc
• Packaging
• Deployment
• Run/Testing
• Get Source from Source Control
• *Ant Manual – “Core tasks”
Additional Tasks
• Checkstyle
• PMD
Revision – So far
• Ant is primarily ..
• Ant is different from make ..
• Ant Core concepts are
– Project
– Target
– Task
– Properties
Ant : Installation
• Fairly Easy
• JDK required.
Build.xml
• <project default="compile">
• <target name="clean">
• <echo> cleaning folder </echo>
• <delete file="Hello.class" />
• <delete file="hello.jar" />
• <delete>
• <fileset dir="." includes="**/*.bak" />
• </delete>
• </target>

• <target name="compile" depends="clean">


• <javac srcdir="." />
• </target>
Ant in Action
• C:\training\ant\example1>ant
• Buildfile: build.xml

• clean:
• [echo] cleaning folder
• [delete] Deleting 1 files from C:\training\ant\example1

• compile:
• [javac] Compiling 1 source file

• BUILD SUCCESSFUL
• Total time: 1 second
Ant Exercise – One
• Write a build.xml
- clean compilation
- Create a jar, with .class and properties files
- Copy .jar to dest
- Run the java program using .jar in the classpath
Few More Tasks
• Java
• Javac
• Jar
• Copy
• Condition
• In detail
Exercise - Two
• Go through Ant Core tasks and write a simple
build file to demo an Ant Task not related to
JAVA
• Java, javac, javadoc, jar
Typical Project
• Properties - property
• Paths - path
• Init - delete
• Compile (javac)
• Packaging (jar)
• Deployment – copy, copy jsps etc
• Testing - junit
Advanced Concepts
• Filesets
• <fileset dir="${server.src}" casesensitive="yes">
– <filename name="**/*.java"/>
– <not>
– <filename name="**/*Test*"/> </not>
• </fileset>
Advanced Concepts
• FileList, Id
• <filelist
– id="docfiles"
– dir="${doc.src}"
– files="foo.xml,bar.xml"/>
Advanced Concepts
• Permissions
• <permissions>
– <grant class="java.net.SocketPermission" name="foo.bar.com"
action="connect"/>
– <grant class="java.util.PropertyPermission" name="user.home"
action="read,write"/>
• </permissions>
Advanced Concepts
• Conditions
• <condition property="javamail.complete">
– <and>
– <available classname="javax.activation.DataHandler"/>
– <available classname="javax.mail.Transport"/>
– </and>
• </condition>
• <target name=“compile” if=“javamail.complete”>
– …
• </target>
Type of Tasks
• Property
• Compile
• Archive
• Documentation
• Deployment
• Logging, Mailing
• Testing
Exercise - 3
• Compare Contents of same file in different
directories
– First
– Second
Optional Tasks
• Typically tasks which are provided by other
vendors/freeware writer.
• Junit
• JspC
• ftp, telnet
• Clearcase, pvcs
• rpm
Extending Task
• Org.apache.tools.ant.Task
• Tutorial : Writing task
• C:\pvinstalled\apache-ant-
1.6.0\docs\manual\tutorial-writing-tasks.html
Extending Task
• Write execute function
• Setting Attributes
• Nested Elements
Exercise : later
• Write a task
• Input fromdir, todir
• Compares contents of two directories
• Optimize as much as possible.
Build.xml + local.props
Exercise
1. com.pspl.batch1.PersonBean.java
2. showperson.jsp
 create instance of personbean and show details

3. webappname : personsinfo
4. build.xml
1. compile .java files
2. copy *.jsp to the right webapp – webapp exists (create a directory in tomcat
before running ant)

5. Invoke jsp thru IE

You might also like