OCT Information Systems Management 1 Lecture 2A 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. Built in to Eclipse.
OCT Information Systems Management 2 Ant Concepts 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.
OCT Information Systems Management 3 “Hello World” In Ant ${HelloText} ${HelloOCT}
OCT Information Systems Management 4 Using Ant with Eclipse Create a Workspace and a Project. Right click the project and select new file. Enter text for build.xml. Save. Right click the file and run as Ant build.
OCT Information Systems Management 5 Java Example build.xml <javac srcdir="." destdir="." classpath=".” />
OCT Information Systems Management 6 MyJava.java public class MyJava { public static void main(String a[]) { System.out.println("Hello world"); }
OCT Information Systems Management 7 Ant Execution D:\McCarthy\www\95-733\examples\ant2>ant Buildfile: build.xml compile: [javac] Compiling 1 source file to D:\McCarthy\www\ \examples\ant2 run: [java] Hello world BUILD SUCCESSFUL Total time: 17 seconds
OCT Information Systems Management 8 Another Java Example build.xml examples | --- antdir | | | --- SomeCoolClass.class | --- SomeCoolClass.java --- ant2 | --- build.xml --- MyClass.java The build file needs to compile MyClass.java and needs SomeCoolClass in its classpath.
OCT Information Systems Management 9 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; } }
OCT Information Systems Management 10 MyJava.java D:\McCarthy\www\95-733\examples\ant2>type MyJava.java public class MyJava { public static void main(String a[]) { SomeCoolClass p = new SomeCoolClass(); System.out.println("Hello world x == " + p.getX()); }
OCT Information Systems Management 11 build.xml D:\McCarthy\www\95-733\examples\ant2>type build.xml <javac srcdir="." destdir="." >
OCT Information Systems Management 12 build.xml (Continued)
OCT Information Systems Management 13 Ant Execution D:\McCarthy\www\95-733\examples\ant2>ant Buildfile: build.xml compile: run: [java] Hello world x == 3 BUILD SUCCESSFUL Total time: 3 seconds
OCT Information Systems Management 14 Same Example Different build.xml
OCT Information Systems Management 15
OCT Information Systems Management 16 Ant Example from “Ant The Definitive Guide” O’reilly Problem: We have source code in a Java package. We want to create a build directory with class files. We want to place the build directory in a Java archive.
OCT Information Systems Management 17 Initial Layout D:\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
OCT Information Systems Management 18 After ant all D:. │ build.xml │ ├───build │ ├───classes │ │ └───com │ │ └───oreilly │ │ └───sample │ │ Account.class │ │ Person.class │ │ PersonTest.class │ │ └───lib │ orielly.jar
OCT Information Systems Management 19 After ant all (continued) │ └───src └───com └───oreilly └───sample Account.java Person.java PersonTest.java
OCT Information Systems Management 20 build.xml
OCT Information Systems Management 21
OCT Information Systems Management 22 <target name = "compile" depends = "prepare" description = "Compiles all source code." > <target name = "jar" depends = "compile" description = "Generates oreilly.jar in the 'dist' directory. "> <jar jarfile="${build.lib}/orielly.jar" basedir="${build.classes}" excludes = "**/*PersonTest.class" /> <target name = "all" depends = "clean,jar" description = "Cleans, compiles, then builds the Jar file." />