Top Banner
1 Introduction to ANT tten by James Duncan Davidson e GNU Make but specifically for Java d for bundling and delivery of groups of classes, j dles dependencies automatically tten in XML ks on Unix or Windows ilable from Apache.org
21

Introduction to ANT

Jan 06, 2016

Download

Documents

kylene

Introduction to ANT. Written by James Duncan Davidson Like GNU Make but specifically for Java Good for bundling and delivery of groups of classes, jars, wars Handles dependencies automatically Written in XML Works on Unix or Windows Available from Apache.org. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to ANT

1

Introduction to ANT

• Written by James Duncan Davidson• Like GNU Make but specifically for Java• Good for bundling and delivery of groups of classes, jars, wars• Handles dependencies automatically• Written in XML • Works on Unix or Windows• Available from Apache.org

Page 2: Introduction to ANT

2

Ant programs are XML documents

The meaning of the tags is not defined by XML.

XML documents can be programs (we’ll see this again with XSLT).

The software that reads the Ant documents does twothings: -- parses the document (already available to the creator of Ant) -- interprets and executes Ant tasks based on agreed upon semantics

Page 3: Introduction to ANT

3

Some Ant Notes

• Exactly on project element is required• There may be many properties, targets and tasks• At least on target is required• Targets describe broad goals• Tasks are nested within targets• Over 100 core tasks available (e.g. mkdir, javac)• Properties are name-value pairs• Ant interprets the build file with a breadth first traversal across the XML elements under project• Inside a target, Ant performs a depth first traversal• By default, Ant breaks at the first error

Page 4: Introduction to ANT

4

First Example build.xml<?xml version="1.0"?><project basedir="." default="run">

<target name="compile"> <javac srcdir="." destdir="." classpath="." /> </target> <target name="run" depends="compile"> <java classname="MyJava" /> </target></project>

Page 5: Introduction to ANT

5

MyJava.java

public class MyJava {

public static void main(String a[]) {

System.out.println("Hello world"); }}

Page 6: Introduction to ANT

6

Ant Execution

D:\McCarthy\www\95-733\examples\ant2>antBuildfile: build.xml

compile: [javac] Compiling 1 source file to D:\McCarthy\www\95-733 \examples\ant2

run: [java] Hello world

BUILD SUCCESSFULTotal time: 17 seconds

Page 7: Introduction to ANT

7

Second Example build.xml

examples | --- antdir | | | --- SomeCoolClass.class | --- SomeCoolClass.java --- ant2 | --- build.xml --- MyClass.java

Page 8: Introduction to ANT

8

SomeCoolClass.java

D:\McCarthy\www\95-733\examples\antdir>type SomeCoolClass.java

public class SomeCoolClass {

int x;

public SomeCoolClass() { x = 3; } public int getX() { return x; }

}

Page 9: Introduction to ANT

9

MyJava.java

D:\McCarthy\www\95-733\examples\ant2>type MyJava.javapublic class MyJava {

public static void main(String a[]) {

SomeCoolClass p = new SomeCoolClass();

System.out.println("Hello world x == " + p.getX());

}}

Page 10: Introduction to ANT

10

build.xmlD:\McCarthy\www\95-733\examples\ant2>type build.xml<?xml version="1.0"?><project basedir="." default="run"> <target name="compile"> <javac srcdir="." destdir="." > <classpath> <pathelement path="../antdir/"/> <pathelement path="."/> </classpath>

</javac> </target>

Page 11: Introduction to ANT

11

build.xml (Continued)

<target name="run" depends="compile">

<java classname="MyJava"> <classpath> <pathelement path="../antdir/"/> <pathelement path="."/> </classpath> </java> </target></project>

Page 12: Introduction to ANT

12

Ant Exceution

D:\McCarthy\www\95-733\examples\ant2>antBuildfile: build.xml

compile:

run: [java] Hello world x == 3

BUILD SUCCESSFULTotal time: 3 seconds

Page 13: Introduction to ANT

13

Same Example Different build.xml

<?xml version="1.0"?><project basedir="." default="run">

<path id="project.class.path"> <pathelement path="."/> <pathelement path="../antdir/"/> </path>

<target name="compile"> <javac srcdir="." destdir="." > <classpath refid="project.class.path"/> </javac> </target>

Page 14: Introduction to ANT

14

<target name="run" depends="compile">

<java classname="MyJava"> <classpath refid="project.class.path"/> </java> </target></project>

Page 15: Introduction to ANT

15

Ant Example from “Ant The Definitive Guide” O’reilly

Page 16: Introduction to ANT

16

Initial LayoutD:\McCarthy\www\95-733\examples\ant>tree /f

Directory PATH listing

Volume serial number is 0012FC94 486D:D392

D:.

│ build.xml

└───src

└───com

└───oreilly

└───sample

Account.java

Person.java

PersonTest.java

Page 17: Introduction to ANT

17

After ant all

D:.│ build.xml│├───build│ ├───classes│ │ └───com│ │ └───oreilly│ │ └───sample│ │ Account.class│ │ Person.class│ │ PersonTest.class│ ││ └───lib│ orielly.jar

Page 18: Introduction to ANT

18

After ant all (continued)

│└───src └───com └───oreilly └───sample Account.java Person.java PersonTest.java

Page 19: Introduction to ANT

19

build.xml<?xml version="1.0" ?><!-- build.xml -->

<project name = "Simple BuildFile" default="compile" basedir=".">

<!-- the directory containing source code -->

<property name = "src.dir" value = "src" />

<!-- Temporary build directories -->

<property name = "build.dir" value = "build" />

Page 20: Introduction to ANT

20

<property name = "build.classes" value = "${build.dir}/classes" />

<property name = "build.lib" value = "${build.dir}/lib" />

<!-- Target to create the build directories prior to compile -->

<target name = "prepare"> <mkdir dir= "${build.dir}" /> <mkdir dir= "${build.classes}" /> <mkdir dir= "${build.lib}" /> </target>

<target name = "clean" description = "Remove all generated files." > <delete dir ="${build.dir}" /> </target>

Page 21: Introduction to ANT

21

<target name = "compile" depends = "prepare" description = "Compiles all source code." >

<javac srcdir = "${src.dir}" destdir= "${build.classes}" /> </target>

<target name = "jar" depends = "compile" description = "Generates oreilly.jar in the 'dist' directory. "> <!-- exclude the test class from the jar file --> <jar jarfile="${build.lib}/orielly.jar" basedir="${build.classes}" excludes = "**/*PersonTest.class" /> </target>

<target name = "all" depends = "clean,jar" description = "Cleans, compiles, then builds the Jar file." /></project>