1 / 30

Ant & Jar

Ant & Jar. Ant – Java-based build tool Jar – pkzip archive, that contains metadata (a manifest file) that the JRE understands. jar. Use jar utility to create jar files A compressed, tar'd collection of *.class files Command format like tar

kristy
Download Presentation

Ant & Jar

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. Ant & Jar Ant – Java-based build tool Jar – pkzip archive, that contains metadata (a manifest file) that the JRE understands

  2. jar • Use jar utility to create jar files • A compressed, tar'd collection of *.class files • Command format like tar • File 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 packages

  3. Creating a jarfile • Options much like tar • c – create • x – extract • u – update • Others • v – verbose • f – specify archive filename • m – include manifest info from named file

  4. Creating a jarfile - example • Given class files: pokerSuit.class, pokerClient.class • Create the jarfile: $ jar cvf poker.jar poker*.class • You 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 files

  5. Executing a jarfile • Jar files can be made executable by adding a Main-Class line to the manifest, and identifying the class that contains main • create an mf file (poker.mf) w/these lines: Classpath: ./poker.jar Main-Class: pokerClient • Create the jarfile w/this info: $ jar cvfm poker.jar poker.mf *.class • To run: java –jar poker.jar

  6. Ant • A bit like Make • Cross-platform • Extended w/Java classes, rather than shell-dependent scripts • Reads XML configuration files • Java-based • Open source

  7. Why another build tool? • Make tools are shell based • Development across multiple platforms

  8. Ant vs Make • Ant is Cross Platform • Ant will work on any platform that has a Java VM. • Make relies on specific OS shell commands • Syntax • Make has a scary looking syntax • Make tab problem • Ant uses XML • self documenting • Efficiency

  9. Ant • PROs • Fast Compiles • Platform Independent • Easier to use than make • CONs • non-Java projects

  10. Who should use Ant? • Use Ant • If you don’t already know make • If you are developing anything cross platform • Use Make • If you are a make expert. • Developing applications in non-Java language.

  11. Resources • Ant : the definitive guide • ISBN: 0596001843 • Java tools for eXtreme Programming • ISBN: 047120708X • Electronic resource • Ant add-ons: • http://ant.apache.org/external.html

  12. Links • http://ant.apache.org/ • http://ant.apache.org/resources.html • http://ant.apache.org/manual/index.html • Nice description of the various tags/tasks

  13. Key Words • Build File • Collection of targets (hopefully logical) • Target • Collection of tasks • Can have dependencies (other targets, etc.) • Task • Specific commands for building, running, etc

  14. Ant Details • All Ant tasks are defined in a Java class • An 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.

  15. build.xml • The default build file Ant looks for • -f option allows to specify a different build file

  16. How to run ant ant [-f file] [target*] • file – config file (build.xml if not supplied) • target* - list of targets to be built • E.g.: $ ant test $ ant compile $ ant javadoc $ ant compile javadoc $ ant If you don’t specify, the default is executed

  17. build.xml Example <project default="compile"> <target name="compile"> <javac srcdir="." /> </target> </project> • Simply compiles all *.java files in the current directory (calls javac)

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

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

  20. Ant Setup • Ant is installed in /usr/local/bin/ant • JAVA_HOME variable should be set • should be set to prefix of /bin/java • On the CS machines: • /usr/local OR • /opt/java5/jdk1.5.0_06/

  21. Common Ant Tasks • <javac srcdir=dir [includes=fileList]/> • compiles Java source files • <java classname=class/> OR <java fork='yes' jar=jar/> • runs a Java application • javadoc • generates javadoc HTML file from Java source files • mkdir • creates a directory and any missing parent directories

  22. Common Tasks (cont) • move • moves 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 file

  23. Common Ant Tasks (cont.) • Gunzip • unzips a GZIP file • Gzip • creates a GZIP file from a file • Jar • creates a JAR file from a set of files • Mail • sends email using SMTP • Get • creates a copy of a remote file at a specified URL • can use http and ftp URLs

  24. Extra Tasks • JUnit • runs JUnit tests • requires junit.jar from http://junit.org • FTP • lists, gets, puts and deletes files on an FTP server • requires NetComponents.jar

  25. Examples Given files foo.java, which extends bar.java, in the current directory • compile all java files in directory: <project default="compile"> <target name="compile"> <javac srcdir="." /> </target> </project>

  26. Example • compile 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='./' includes='bar.java'/> </target> </project>

  27. Example • Add a "clean" target <target name="clean"> <delete> <fileset dir="./"> <include name='*.class'/> </fileset> </delete> </target>

  28. Example • Add a "run" target: <target name="run" depends='compile'> <java classname='foo'/> </target>

  29. Example • Add a target to build a jarfile: <target name="jar" depends='compile'> <jar destfile='./foo.jar' manifest='man.mf'> <fileset dir='./'> <include name='*.class'/> </fileset> </jar> </target>

  30. Example • Run the jarfile: <target name="run" depends='jar'> <java jar='foo.jar' fork='true'/> </target>

More Related