Introduction To Ant1

17
sG Introduction to Introduction to ANT ANT www.scmGalaxy.com scmGalaxy Author: Rajesh Kumar [email protected]

description

Ant is a Java library and command-line tool. Ant's mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks. Ant is written in Java. Users of Ant can develop their own "antlibs" containing Ant tasks and types, and are offered a large number of ready-made commercial or open-source "antlibs". Ant is extremely flexible and does not impose coding conventions or directory layouts to the Java projects which adopt it as a build tool. Software development projects looking for a solution combining build tool and dependency management can use Ant in combination with Ivy.

Transcript of Introduction To Ant1

Page 1: Introduction To  Ant1

sG

Introduction to Introduction to ANTANT

www.scmGalaxy.com

scmGalaxy

Author: Rajesh [email protected]

Page 2: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Agenda

What is Ant? Why is it called ANT? Advantages of ANT How does ANT work? Sample Build file Executing An Ant Script Some Simple Ant Commands References

Page 3: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

What is Ant?

• A build tool

• Open Source

• Part of Apache’s Jakarta project.

• Implemented in Java

• Written for Java

• Used to build many open-source Java projects

Page 4: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

According to the author (James Duncan Davidson)…

•Because ants do an extremely good job of building things.

•Stands for “Another Neat Tool”

Why is it called ANT?

Page 5: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Advantages of ANT

• Only requires a Java VM to use.

• It is extended using Java classes, which makes it almost like fun for Java developers.

• Easier to do Java-specific tasks, such as JavaDocs, WAR and JAR files, and Enterprise JavaBeans.

• Instead of writing cryptic shell commands, the configuration files are easy to read XML.

• Each “task” is defined in a separate XML block, making adding or removing additional tasks easier.

Page 6: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Advantages Continued

• It’s FAST! (Uses the same VM for entire process)

• Because it is Java based, it is easily expandable, and inherently cross-platform.

• All of the cool Java IDEs have built-in support for Ant.

• It can easily operate recursively, so only one Ant build script is required for most projects.

• Ant has built-in CVS support.

• Ant has built-in FTP support.

• Ant has built-in JUnit support.

Page 7: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

How does ANT work?

Ant commands (or tasks) are implemented by Java classes many tasks are in-built or come in JAR files custom commands can be easily created

Each project using Ant will have a build file called build.xml – Ant’s default

Each build file is composed of any number of targets corresponding to common activities like

compiling and running code

Page 8: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

How does ANT work? (contd)

Each target is composed of tasks executed in sequence when the target is

executed Ant targets can have dependencies

for example, the build file may have a task to run file C, and can specify that before the file C can be run, package B needs to be compiled, and before package B is compiled, package A must be compiled.

Page 9: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Build file: example

<project name="MyProject" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/>

<target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory used by compile --> <mkdir dir="${build}"/> </target>

Page 10: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Build file: example

<target name="compile" depends="init" description="compile the source " > <!-- Compile code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target>

<target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/>

<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->

<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>

</target>

Page 11: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Build file: example

<target name="clean"

description="clean up" >

<!-- Delete ${build} and ${dist} dirs -->

<delete dir="${build}"/>

<delete dir="${dist}"/>

</target>

</project>

Page 12: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Executing An Ant Script

In its basic form, an Ant script can be run by simply typing “Ant”

Command-line option summary:ant [options] [target [target2 [target3] ...]]

Options (trimmed to fit page):-help print this message -version print the version information and

exit -logfile file use given file for log output -logger classname the class that performs logging-buildfile file use specified buildfile

Page 13: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Some Ant Commands

<ant/>calls a target in another build file

useful to build subprojects<ant antfile="subproject/subbuild.xml"

dir="subproject" target="compile"/>

<cvs/> executes any CVS command<cvs

cvsRoot=":pserver:[email protected]:/home/cvspublic" package="ant" dest="${ws.dir}" />

<exec/> executes a system command<exec dir="${src}" executable="cmd.exe"

os="Windows 2000" output="dir.txt"> <arg line="/c dir"/> </exec>

Page 14: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Some Ant Commands

<javadoc/>generates javadoc HTML files from Java source files<javadoc packagenames="com.dummy.test.*" sourcepath="src" excludepackagenames="com.dummy.test.doc-

files.*" destdir="docs/api" author="true"

version="true" /> <mail/> sends email using SMTP<mail from="me" tolist="you" subject="Results of

nightly build" files="build.log"/> <mkdir/>creates a directory and any missing parent directories<mkdir dir="${dist}"/>

Page 15: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

Some Ant Commands

<sql/>executes a sequence of SQL statements specified in

the build file or an external text file, output can be written to a file

<sql driver="org.database.jdbcDriver" url="jdbc:database-url" userid="sa" password="pass" src="data.sql" >

insert into table some_table values(1,2,3,4);

</sql>

<unjar/> expands a JAR file <untar/> expands a TAR file

Page 16: Introduction To  Ant1

www.scmGalaxy.com

scmGalaxy

References

The ANT manual at ant.apache.org

http://ant.apache.org/manual/

Page 17: Introduction To  Ant1

sGwww.scmGalaxy.com

Author: Rajesh [email protected]