1 / 22

Lecture 2A Introduction to ANT

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.

margiet
Download Presentation

Lecture 2A Introduction to ANT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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.

  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.

  3. “Hello World” In Ant <?xml version="1.0" encoding="UTF-8"?> <projectname="Hello OCT"default="Both"basedir="."> <propertyname="HelloText"value="Hello"/> <propertyname="HelloOCT"value="OCT"/> <targetname="Hello"> <echo>${HelloText}</echo> </target> <targetname="OCT"> <echo>${HelloOCT}</echo> </target> <targetname="Both"depends="Hello,OCT"/> </project>

  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.

  5. Java 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>

  6. MyJava.java public class MyJava { public static void main(String a[]) { System.out.println("Hello world"); } }

  7. Ant Execution D:\McCarthy\www\95-733\examples\ant2>ant Buildfile: build.xml compile: [javac] Compiling 1 source file to D:\McCarthy\www\95-733 \examples\ant2 run: [java] Hello world BUILD SUCCESSFUL Total time: 17 seconds

  8. Another Java Example build.xml The build file needs to compile MyClass.java and needs SomeCoolClass in its classpath. examples | --- antdir | | | --- SomeCoolClass.class | --- SomeCoolClass.java --- ant2 | --- build.xml --- MyClass.java

  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; } }

  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()); } }

  11. build.xml D:\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>

  12. build.xml (Continued) <target name="run" depends="compile"> <java classname="MyJava"> <classpath> <pathelement path="../antdir/"/> <pathelement path="."/> </classpath> </java> </target> </project>

  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

  14. 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>

  15. <target name="run" depends="compile"> <java classname="MyJava"> <classpath refid="project.class.path"/> </java> </target> </project>

  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.

  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

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

  19. After ant all (continued) │ └───src └───com └───oreilly └───sample Account.java Person.java PersonTest.java

  20. 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" />

  21. <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>

  22. <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>

More Related