1 / 43

Introduction to Ant

Introduction to Ant. James Brucker. What is Ant?. Ant is a build tool -- it builds software according to a set of rules. Ant can determine which products depend on which sources, and only build the parts that are out-of-date. An Apache Jakarta project

pbeecher
Download Presentation

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. Introduction to Ant James Brucker

  2. What is Ant? • Ant is a build tool -- it builds software according to a set of rules. • Ant can determine which products depend on which sources, and only build the parts that are out-of-date. • An Apache Jakarta project • Jakarta is an umbrella heading for all the Java related Apache projects • Open source, Apache License • Project Home: http://ant.apache.org/

  3. Example of Using Ant (1) Here is a project with a "build.xml" file it the project directory. First we try asking for help on the build tasks: cmd> ant help [echo] Usage: ant target [echo] [echo] target is one of: [echo] clean - delete files from the build directory [echo] build - compile java source code [echo] buildall - recompile everything (clean first) [echo] dist - create a distribution in dist directory [echo] javadoc - create Javadoc in doc/api dir [echo] test - run unit tests Note: many build.xml files don't have a "help" task, so they may not print this message.

  4. Example of Using Ant (2) We see there is "build" target, so we use it to compile the source: cmd> ant build init: [mkdir] Created dir: world/build [copy] Copying 1 file to world/build build: [javac] Compiling 8 source files to build/classes [javac] Compiling 3 source files to build/test-classes BUILD SUCCESSFUL Ant performs the "init" target Ant performs the "build" The build.xml file told Ant that it must run the "init" target before the "build" target, so we see output from two tasks.

  5. Example of Using Ant (3) Since "build" succeeded, we can try creating a jar file (dist). cmd> ant dist init: build: dist: [mkdir] Created dir: dist [jar] Building jar: dist/world-1.0.jar [copy] Copying 2 files to world/dist BUILD SUCCESSFUL "dist" requires that the "init" and "build" task be performed, but Ant sees that these targets are already up-to-date, so it does nothing The build.xml file told Ant that "dist" depends on "init" and "build", but the output from these targets exist and are up-to-date.

  6. Typical Development Cycle The typical work cycle for building a project with Ant is: build compile sources dist create runnable product test run unit tests test-report generate reports

  7. A Little History • For years (decades) developers have used a program called "make" to automate project builds. • Make gets information from a Makefile. Example: Apache web server • download the source code for Apache httpd • unzip or untar the source code • in the top source directory type: configure • then type: make • wait (it may take several minutes) • type: make install

  8. Ant: a modern "make" • Makefile designed for humans to read and edit. Not optimal for processing by software. • Make is written in terms of processing instructions for types of files, plus application-specific "tasks". Example: # build object (*.o) files from C (*.c) file %.o: %.c ${CC} -c ${CFLAGS} $< • Ant allows developers to define there own rules, and has a more expressive syntax. • Ant uses XML for rules file: easier for software to read and write.

  9. Installing Ant Windows: • Download from http://ant.apache.org/ • Unzip to a convenient directory (no spaces preferred).I use: D:\lib\ant • Add ant-dir\bin to the PATH. I use: ANT_HOME=D:\lib\ant PATH=%PATH%;%ANT_HOME%\bin Ubuntu Linux: "apt-get install ant" will install the GNU Java and lots of other packages. Don't do it! Download Ant from ant.apache.org, unzip (e.g. /opt), and add to your path. This way you can use Sun's JRE.

  10. Test the Installation DOS-Never-Dies>ant -help ant [options] [target [target2 [target3] ...]] Options: -help, -h print this message -version print the version and exit -quiet, -q be extra quiet -verbose, -v be extra verbose -logfile <file> use given file for log -buildfile <file> use given buildfile -f <file> | -file <file> '' -D<property>=<value> use value for given property -keep-going, -k execute all targets that do not dependon failed target(s) -propertyfile <name> load all properties from file If you get a "command not found" message, then ant/bin isn't on your PATH. If java is not found, then the JDK "bin" directory isn't on your PATH.

  11. Sample Application World/ project folder build.xmlAnt build file src/ source code world/ domain/ City.java ... test/ test source world/ domain/ CityTest.java build/ build products classes/ java classes dist/ final products world-1.0.jar lib/ dependent jars mysql-connector-bin.jar

  12. A Simple Ant Build file • The default build file name is: build.xml <projectname="World"basedir="."> <description> World country facts using JDBC </description> <!-- classpath for required jar files --> <pathid="classpath"> <filesetdir="lib"> <includename="**/*.jar"/> </fileset> <pathelementlocation="build/classes"/> </path>

  13. A Simple Ant Build file (2) • The actual work is defined in "targets": <projectname="World"basedir="."> ... <targetname="init"> instructions for "init" job </target> <targetname="build" depends="init"> instructions for "build" job </target> <targetname="dist" depends="build"> instructions for "dist" job </target>

  14. Define a "build" task • This task tells Ant how to compile our program <!-- Compile the java code --> <targetname="build"depends="init" description="compile the source"> <javacdestdir="build/classes"> <srcpath="src"/> <classpathrefid="classpath"/> </javac> <!-- compile JUnit tests --> <javacdebug="true"destdir="build/test"> <srcpath="test"/> <classpathrefid="classpath"/> </javac> </target>

  15. "build" depends on "init" task • Most projects have an "init" task to create output directories. <!-- initialize build environment --> <targetname="init"description="create dirs"> <mkdirdir="build"/> (this is not required) <mkdirdir="build/classes"/> <mkdirdir="build/test"/> <!-- copy property files and xml files except java files --> <copyincludeemptydirs="false" todir="build/classes"> <filesetdir="src" excludes="**/*.launch, **/*.java" /> </copy> </target> Ant wildcards

  16. Test Your Build File Cmd> ant build if your build file is "mybuild.xml" then... Cmd> ant -f mybuild.xml build Buildfile: mybuild.xml init: [mkdir] Created dir: workspace/World/build/test [copy] Copying 2 files to ... build: [echo] World: workspace/World/mybuild.xml [javac] Compiling 6 source files to build/classes BUILD SUCCESSFUL

  17. Create a Distribution • Define a "dist" task to create the project jar file. <targetname="dist"depends="build" description="create distribution"> <mkdirdir="dist"/> <!-- Put build/classes into the jar file --> <jarjarfile="dist/worldfacts-1.0.jar" basedir="build/classes"> </jar> <copyincludeemptydirs="false"todir="dist"> <filesetdir="lib"includes="*.jar"/> </copy> <copyincludeemptydirs="false"todir="dist"> <filesetdir="src"includes="*.properties"/> </copy> </target>

  18. Test the "dist" target Cmd> ant build if your build file is "mybuild.xml"... Cmd> ant -f mybuild.xml build Buildfile: mybuild.xml init: build: dist: [mkdir] Created dir: dist [jar] Building jar: dist/worldfacts-1.0.jar [copy] Copying 2 files to world/dist

  19. Use properties instead of strings • We have used "build/classes", "src", ... many times in the build file. • Difficult to maintain and potential for typing errors. src.dir = location of source code build.dir = build output base directory build.classes.dir =location of build classes <!-- directory for distribution products --> dist.dir =location of distribution lib.dir =location of required libraries doc.dir =documentation base directory <!-- used to configure compiler compliance --> source =source compliance level (1.5, 1.6 ...) target =output compliance level (1.5, 1.6 ...)

  20. Use properties instead of strings <!-- directory for main source code --> <propertyname="src.dir"location="src"/> <!-- directories for build output --> <propertyname="build.dir"location="build"/> <propertyname="build.classes.dir" location="${build.dir}/classes"/> <!-- directory for distribution products --> <propertyname="dist.dir"location="dist"/> <propertyname="lib.dir"location="lib"/> <propertyname="doc.dir"location="doc"/> <!-- directories for test classes --> <propertyname="test.src.dir"location="test"/> <propertyname="test.classes.dir" location="${build.dir}/test-classes"/> <propertyname="test.reports.dir" location="${build.dir}/test-reports"/>

  21. Substitute properties for strings Substitute property names in the build rules: <!-- initialize environment --> <targetname="init"description="initialize dirs"> <mkdirdir="${build.classes.dir}"/> <mkdirdir="${test.classes.dir}"/> <mkdirdir="${test.reports.dir}"/> <mkdirdir="${lib.dir}"/> <!-- copy the source files except java files --> <copyincludeemptydirs="false" todir="${build.classes.dir}"> <filesetdir="${src.dir}" excludes="**/*.launch, **/*.java"/> </copy> </target>

  22. Test the updated build.xml file • Delete the "build" directory. • Run "ant build" again. • Expect some errors: • typing errors (forgot "." in name) • Ant may create a file named "${build.xxx.dir}" if you have a misspelled property name in the build file

  23. Create a "test" task <targetname="test"depends="build"> <junitprintsummary="true" haltonfailure="false"> <classpath> <pathrefid="classpath"/> <pathelement location="${test.classes.dir}"/> </classpath> <formattertype="brief"usefile="false"/> <formattertype="xml"/> <batchtesttodir="${test.reports.dir}"> <filesetdir="{test.classes.dir}" includes="**/*Test.class"/> </batchtest> </junit> </target>

  24. What Are Tasks? Ant has a large set of built-in tasks, such as: <echo ...> output a message <mkdir ...> create a directory (if it doesn't exist) <copy ...> copy a file, directory, or tree <javac ...> compile files using java compiler <jar ...> create a jar file <junit ...> run JUnit tests

  25. <property name="src" value="..."> • Defines a variable ("property") that can be used throughout a build script. • To access value of a property use: ${propertyname}. • Useful for defining names of locations and files used repeatedly. Example: <property name="src.dir" value="src/java"/> <javac ...> <src path="${src.dir}"/> </javac>

  26. <property ...> (2) • Read all the properties from a file. The file is a plain text file with lines of the form "name=value", like Java properties files <property file="build.properties"/> • Properties can be imported from system environment! • Prefix environment properties with a "env." <property environment="env"/> <echo message= "CLASSPATH is ${env.CLASSPATH}"/> <echo message= "JAVA_HOME is ${env.JAVA_HOME}"/>

  27. <copy file="pattern" tofile="..."/> • Copies a file or set of files to another location. • Does not overwrite existing files if they are newer than the source file (unless you specify that you want it to overwrite). Copy a single file. <copy file="${src.dir}/myfile.txt" tofile="${target.dir}/mycopy.txt"/>

  28. <copy todir="...">: copy sets of files • Copy files from one directory to another, omit any java source files. <copy todir="${dest.dir}" > <fileset dir="src"> <exclude name="**/*.java"/> </fileset> </copy> • Copy all files from the directory “../backup/” to “src_dir”. Replace occurrences of "@TITLE@" in the files with "Foo". <copy todir="../backup"> <fileset dir="src_dir"/> <filterset> <filter token="TITLE" value="Foo Bar"/> </filterset> </copy>

  29. <delete> • Deletes files, directories, or sets of files. • Delete a single file. <delete file="/lib/ant.jar"/> • Delete all *.bak files from this directory and sub-directories. <delete> <fileset dir="." includes="**/*.bak"/></delete> • Delete the build directory and everything in it. <delete includeEmptyDirs="true"> <fileset dir="build"/> </delete>

  30. <echo> Display a message on terminal. It has 2 forms: • Display a one-line message: <echo message="Hello Ants" /> [echo] Hello Ants • Display many lines of text: <echo> Usage: ant target clean - delete compiler output files build - compile source code dist - create a distribution </echo>

  31. Using <echo> A good build.xml file should have a help or usage target: <project name="myapp" default="help"> <target name="help"> <echo> Usage: ant target where 'target' is one of: clean - delete compiler output files build - compile source code dist - create a distribution </echo> </target>

  32. <mkdir dir="..."/> • Create a directory. <mkdir dir="${dist.dir}"/> • Creates a subdirectory named "jars" in the location specified by the "dist.dir" property. <mkdir dir="${dist.dir}/jars"/>

  33. <javac> • Compiles Java source code. • Attempts to analyze source such that up to date .class file are not recompiled. Example: Compile all java source files under ${src.dir} and put the .class files in the ${build.classes} directory. Include debugging information in the .class files. <javac srcdir="${src}" destdir="${build.classes}" classpath="mylib.jar" debug="true"/>

  34. <javac ...> (2) • You can specify additional source directories and further restrict which files are compiled using include and exclude . <javac destdir="${build}" classpath="xyz.jar" debug="on"> <src path="${src}"/> <src path="${src2}"/> <include name="package/p1/**"/> <include name="package/p2/**"/> <exclude name="package/p1/test/**"/> </javac>

  35. <jar ...> • Creates a JAR file from a set of files or updates an existing JAR. • Will automatically supply a manifest file for the JAR or use one you specify. Example: make a jar file including all files in build/classes <jar jarfile="${dist}/lib/myapp.jar" basedir="${build}/classes"/>

  36. <jar ...> • Create a JAR file from all the files in ${build}/classes and ${src}/resources. (two sets of files) • Any files named *Test.class in the build directory are not included in the JAR. <jar jarfile="${dist}/lib/myapp.jar"> <fileset dir="${build}/classes" excludes="**/*Test.class" /> <fileset dir="${src}/resources"/> </jar>

  37. <javadoc> • Creates Javadocs from Java source code files. Example: Build Javadoc only for the packages beginning with "org.ske..." in the ${src}\ directory. <javadoc packagenames="org.ske.*" sourcepath="${src}" destdir="${doc}/api"/> This command will search all subdirectories of ${src} for *.java files.

  38. <java> • Invoke a Java program from within an Ant build file. • Can fork a separate process so that a System.exit() does not kill the Ant build. <java classname="test.Main"> <arg value="some-arg-to-main"/> <classpath> <pathelement location="test.jar"/> <pathelement path="${java.class.path}"/> </classpath></java>

  39. <java> Invoke a class named test.Main in a separate Java VM. The Java VM is invoked using the options: -Xrunhprof:cpu=samples,file=log.txt,depth=3 to request profiling. <java classname="test.Main" fork="yes"> <sysproperty key="DEBUG" value="true"/> <arg value="-h"/> <jvmarg value= "-Xrunhprof:cpu=samples,file=log.txt,depth=3"/></java>

  40. More Ant Tasks • The Apache Ant distribution includes more than 50 core tasks and many optional tasks. • Examples: zip, gzip, war (create a war file), • Many tasks correspond to standard Linux commands, like mkdir, copy, move. • You can write your own Ant tasks using <taskdef />. • See Ant manual (ant/docs directory) for how to use each task.

  41. Tools • List of Ant tools: http://ant.apache.org/external.html • NetBeans creates build.xml files for NetBeans projects. • Eclipse can "export" an Ant build file, but it contains a lot of Eclipse-specific references that make the build file not portable. • Ivy (http://ant.apache.org/ivy) is a dependency manager for Ant. Automatically downloads dependencies, similar to what Maven does (Ivy can use Maven repositories).

  42. Resources • Ant Home: http://ant.apache.org • Apache Ant Manual. Installed with ant, in the ant/docs directory. The Ant Manual documents all Ant tasks. • Ant: The Definitive Guide. O'Reilly. Terse, but lots of info.

More Related