Transcript - SlideBoom
Introduction to Ant
www.scmGalaxy.com
sG
Author: Rajesh Kumar
[email protected]
scmGalaxy
Agenda
www.scmGalaxy.com
What is ANT?
Installation
Sample build.xml
Executing ant script
Closer look at the structure of ANT file
Advantages of using ANT
Packaging – JAR, WAR, EAR
scmGalaxy
What is ANT?
www.scmGalaxy.com
Another Neat Tool
Java-based build tool
Like make, without make’s wrinkles
An open source Apache Jakarta project
http://ant.apache.org/
Implemented in Java, implemented for Java
scmGalaxy
Installation
www.scmGalaxy.com
Download the binaries from
http://jakarta.apache.org/ant/index.html
Unzip to a suitable directory
Set ANT_HOME = path_to_ant_folder
Append PATH=%PATH%;%ANT_HOME%\bin
environment variable
to
the
PATH
Append the .jar files in /path_to_ant/lib/ to the
CLASSPATH environment variable
Ensure that JAVA_HOME points to the location of the JDK
installation on your machine & /path_to_jdk/lib/* is a
part of the CLASSPATH environment variable
scmGalaxy
Working with ANT
www.scmGalaxy.com
Every project using ANT will have a build file – build.xml
<?xml version="1.0"?>
<project name="test" default="compile" basedir=".">
<property name="src" value="."/>
<property name="build" value="build"/>
<target name="init">
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init">
<!-- Compile the java code -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
</project>
scmGalaxy
A simple build.xml
www.scmGalaxy.com
<?xml version="1.0"?>
XML declaration specifying version of XML used.
All XML constraints hold good for build.xml
<project name=“test" default="compile" basedir=".">
project – root element of build.xml
It has 3 attributes
name – name of project
default – (mandatory) default target when no target is specified
basedir – directory from which any relative directories within build.xml
are referenced from
scmGalaxy
A simple build.xml
[Contd.]
www.scmGalaxy.com
<property name="src" value="."/>
<property name="build" value="build"/>
Property declaration is like user-defined variables to use within
the build file
It has 2 attributes
name – name of property
value – desired value of the property
To reference a property, ${property_name} - ${src}
Built in properties that ANT provides :
basedir
ant.file
ant.version
ant.project.name
ant.java.version
scmGalaxy
A simple build.xml
[Contd.]
www.scmGalaxy.com
<target name="init">
<mkdir dir="${build}"/>
</target>
target element is a wrapper for a sequence of actions
Has the following attributes
name – (mandatory) Name used to reference this target from within the
file or from command line
description – short description of this target
if – to conditionally execute contents of target based on the value of
a property
unless– converse of if
scmGalaxy
A simple build.xml
[Contd.]
www.scmGalaxy.com
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}"/>
</target>
depends – comma separated list of all the targets on which this
target depends, i.e, targets that must be executed prior to the
execution of this target
javac element is a task which is performed under the target
compile
By default, only those .java input files that have a more recent
timestamp than their corresponding .class output files will be
compiled
scmGalaxy
Executing ant script
www.scmGalaxy.com
ant [options] [target [target2 [target3] ...]]
Options:
-help
-projecthelp
-version
-diagnostics
-quiet, -q
-verbose, -v
-debug
-emacs
-logfile <file>
-l <file>
-logger <classname>
-listener <classname>
-buildfile <file>
-file <file>
-f <file>
-D<property>=<value>
-propertyfile
-inputhandler <class>
-find <file> <name>
print this message
print project help information
print the version information and exit
print information that might be helpful to diagnose
or report problems.
be extra quiet
be extra verbose
print debugging information
produce logging information without adornments
use given file for log
''
the class which is to perform logging
add an instance of class as a project listener
use given buildfile
''
''
use value for given property
taking precedence
the class which will handle input requests
load all properties from file with -D properties
search for buildfile towards the root of the
file system and use it
scmGalaxy
Path-like structures -
classpath
www.scmGalaxy.com
Wherever path-like values need to be specified, a nested
element can be used :
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>
<classpath path =“${classpath}”/>
location - specifies a single file or directory relative to the
project's base directory or an absolute filename
path - accepts “:” or “;” separated lists of locations
scmGalaxy
Path-like structures -
path
www.scmGalaxy.com
<path id="project.class.path">
<path refid="base.path"/>
<pathelement location="lib/"/>
</path>
<classpath refid="project.class.path"/>
To use the same path-like structure for several tasks, you
can define them with a <path> element at the same level as
targets, and reference them via their id attribute
scmGalaxy
Path-like structures –
dirset,
fileset,filelist
www.scmGalaxy.com
<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>
<filelist dir="${src.dir}" files="foo.xml,bar.xml"/>
</classpath>
scmGalaxy
Tasks
www.scmGalaxy.com
Piece of code that can be executed
Built-in tasks :
javac, java, javadoc, javah, junit
mkdir, copy, move, delete, fileset
jar, war, zip, unjar, unwar, unzip
echo, cvs, exec
ant, antcall
You can also write your own tasks
scmGalaxy
javac
There are many more attributes /
options available. Check Ant User
Manual for more info.
www.scmGalaxy.com
<javac debug="${debug}"
optimize="${optimize}"
deprecation="${deprecation}"
destdir="${build.dest}"
includes="com/**" excludes="**/*.xml">
<src path="${src.dir}" />
<classpath refid="classpath" />
</javac>
Compiles a java source tree
Source & destination directory recursively scanned for .java files to
compile
By default only check made for rebuild is existence / modification
time
To define any other java class dependencies use task <depend>
scmGalaxy
Building subprojects - ant
www.scmGalaxy.com
<ant antfile="subproject/subbuild.xml"
dir="subproject"
target="compile"/>
<ant inheritAll="false"
antfile="subproject/subbuild.xml">
<property name="output.type" value="html"/>
</ant>
Runs Ant on supplied build file
This task must not be used outside of a target if it invokes
the same build file that it is part of
scmGalaxy
Calling other targets - antcall
www.scmGalaxy.com
<target name="default">
<antcall target="doSomethingElse">
<param name="param1" value="value"/>
</antcall>
</target>
<target name="doSomethingElse">
<echo message="param1=${param1}"/>
</target>
Call another target within the same build-file (optionally
specifying properties)
This task must no be used outside of a target
scmGalaxy
Advantages
www.scmGalaxy.com
Ease of use
Configuration files are XML based
Same config file (build.xml) can be used across platforms
Platform independent
Special support for Java
Easy to create JavaDocs, WAR & JAR files
Built-in support for
JUnit
FTP
CVS
scmGalaxy
Advantages
[Contd.]
www.scmGalaxy.com
Particularly good for automating complicated repetitive tasks
which is what most build processes are all about!
ANT can be integrated with most Java IDEs
Eclipse
Jbuilder
IntelliJIdea
scmGalaxy
PACKAGING
sG
EJB Packaging – ejb-jar
www.scmGalaxy.com
ejb-jar file will contain :
Beans’ class files
Beans’ deployment descriptors
META-INF/ejb-jar.xml
Application server specific file (e.g. META-INF/jboss.xml)
Sample ejb-jar.xml
Sample application specific descriptor file
scmGalaxy
Web ARchive (WAR)
www.scmGalaxy.com
Has specific hierarchical directory structure
Top-level directory is the document root of application
WAR has the following folders (usually):
jsp – JSP files
images – images used in the JSPs
css – style sheet files
scripts – javascript files
WEB-INF
WEB-INF folder contains
Configuration files like web.xml, struts-config.xml
.tld files (if any)
lib : directory that contains jar archives of libraries
classes :
directory that contains the servlet classes andscmGalaxy
utility
Enterprise ARchive (EAR)
www.scmGalaxy.com
An EAR file can contain :
Web components (war)
Beans (ejb-jar)
Libraries (jar)
J2EE deployment descriptor (META-INF/application.xml)
Sample application.xml
scmGalaxy
References
www.scmGalaxy.com
Ant home page
http://ant.apache.org/
Ant manual
http://ant.apache.org/manual/index.html
Another beginner’s tutorial
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsyst
em/build/tutorials/ant/ant.html
scmGalaxy
www.scmGalaxy.com
sG
Author: Rajesh Kumar
[email protected]