Ant & JarjarCreating a jarfileCreating a jarfile - exampleExecuting a jarfileAntWhy another build tool?Ant vs MakeSlide 9Who should use Ant?ResourcesLinksKey WordsAnt Detailsbuild.xmlHow to run antbuild.xml ExampleComplex ant build file exampleComplex example (cont.)Ant SetupCommon Ant TasksCommon Tasks (cont)Common Ant Tasks (cont.)Extra TasksExamplesExampleSlide 27Slide 28Slide 29Slide 30Ant & JarAnt – Java-based build toolJar – pkzip archive, that contains metadata (a manifest file) that the JRE understandsjarUse jar utility to create jar filesA compressed, tar'd collection of *.class filesCommand format like tarFile format pkzip, but includes a manifest file MANIFEST.MF (meta-data)Can be added to classpath; JRE searches a jarfile like a directory, for included packagesCreating a jarfileOptions much like tarc – createx – extractu – updateOthersv – verbosef – specify archive filenamem – include manifest info from named fileCreating a jarfile - exampleGiven class files:pokerSuit.class, pokerClient.classCreate the jarfile:$ jar cvf poker.jar poker*.classYou can use unzip -l to look at the contents. Notice the MANIFEST.MF:Archive: poker.jar Length Date Time Name -------- ---- ---- ---- 0 08-06-06 09:30 META-INF/ 120 08-06-06 09:30 META-INF/MANIFEST.MF 670 08-06-06 09:19 pokerClient.class 636 08-06-06 09:19 pokerSuit.class -------- ------- 1426 4 filesExecuting a jarfileJar files can be made executable by adding a Main-Class line to the manifest, and identifying the class that contains maincreate an mf file (poker.mf) w/these lines:Classpath: ./poker.jarMain-Class: pokerClientCreate the jarfile w/this info:$ jar cvfm poker.jar poker.mf *.classTo run:java –jar poker.jarAntA bit like MakeCross-platformExtended w/Java classes, rather than shell-dependent scriptsReads XML configuration filesJava-basedOpen sourceWhy another build tool?Make tools are shell basedDevelopment across multiple platformsAnt vs MakeAnt is Cross PlatformAnt will work on any platform that has a Java VM.Make relies on specific OS shell commandsSyntaxMake has a scary looking syntaxMake tab problemAnt uses XMLself documentingEfficiencyAntPROsFast CompilesPlatform IndependentEasier to use than makeCONsnon-Java projectsWho should use Ant?Use AntIf you don’t already know makeIf you are developing anything cross platformUse Make–If you are a make expert.–Developing applications in non-Java language.ResourcesAnt : the definitive guideISBN: 0596001843Java tools for eXtreme Programming ISBN: 047120708XElectronic resourceAnt add-ons:http://ant.apache.org/external.htmlLinkshttp://ant.apache.org/http://ant.apache.org/resources.htmlhttp://ant.apache.org/manual/index.htmlNice description of the various tags/tasksKey WordsBuild FileCollection of targets (hopefully logical)TargetCollection of tasksCan have dependencies (other targets, etc.)TaskSpecific commands for building, running, etcAnt DetailsAll Ant tasks are defined in a Java classAn example of a particular task would be JAVADOC (see common built-in tasks, later)Ant has a core set of tasks that come with it. Other optional tasks can be downloaded.Ant is very modularized.build.xmlThe default build file Ant looks for-f option allows to specify a different build fileHow to run antant [-f file] [target*]file – config file (build.xml if not supplied)target* - list of targets to be builtE.g.:$ ant test$ ant compile$ ant javadoc$ ant compile javadoc$ antIf you don’t specify, the default is executedbuild.xml Example<project default="compile"> <target name="compile"> <javac srcdir="." /> </target> </project>Simply compiles all *.java files in the current directory (calls javac)Complex ant build file example<project default="all"> <property name="obj-dir" location="obj" /> <property name="lib-dir" location="lib" /> <property name="src-dir" location="src" /><target name="init"> <mkdir dir="${obj-dir}" /> <mkdir dir="${lib-dir}" /> </target> <target name="clean-init"> <delete dir="${obj-dir}" /> <delete dir="${lib-dir}" /> </target><target name="compile" depends="init"> <javac srcdir="${src-dir}" destdir="${obj-dir}" /> </target> <target name="clean-compile"> <delete> <fileset dir="${obj-dir}" includes="**/*.class" /> </delete> </target>Complex example (cont.)<target name="jar" depends="compile"> <jar destfile="${lib-dir}/hello.jar" basedir="${obj-dir}" /> </target> <target name="clean-jar"> <delete file="${lib-dir}/hello.jar" /></target> <target name="run" depends="jar"> <java classname="hello" classpath="${lib-dir}/hello.jar" fork="true" /> </target><target name="all" depends="run"/> <target name="clean" depends="clean-init"/> </project>Ant SetupAnt is installed in /usr/local/bin/antJAVA_HOME variable should be setshould be set to prefix of /bin/javaOn the CS machines:/usr/localOR/opt/java5/jdk1.5.0_06/Common Ant Tasks<javac srcdir=dir [includes=fileList]/>compiles Java source files <java classname=class/> OR<java fork='yes' jar=jar/>runs a Java applicationjavadocgenerates javadoc HTML file from Java source filesmkdircreates a directory and any missing parent directoriesCommon Tasks (cont)movemoves files and directories to a new directory<copy file=src (tofile=dest | todir=destDir) />copies files<delete (file=file | dir=dir ) />deletes given file or dir<echo [file=file] message=msg/> or <echo [file=file]>msg</echo>outputs a message to System.out or fileCommon Ant Tasks (cont.)Gunzipunzips a GZIP fileGzipcreates a GZIP file from a fileJarcreates a JAR file from a set of filesMailsends email using SMTPGetcreates a copy of a remote file at a specified URLcan use http and ftp URLsExtra TasksJUnitruns JUnit testsrequires junit.jar from http://junit.orgFTPlists, gets, puts and deletes files on an FTP serverrequires NetComponents.jarExamplesGiven files foo.java, which extends bar.java,in the current directorycompile all java files in directory:<project default="compile"><target name="compile"><javac srcdir="." /></target></project>Examplecompile only foo and bar:<project default="compile"> <target name="compile" depends='foo,bar'/> <target name="foo" depends='bar'> <javac srcdir='./' includes='foo.java'/> </target> <target name="bar"> <javac srcdir='./'
View Full Document