ant & jar

30
Ant & Jar Ant – Java-based build tool Jar – pkzip archive, that contains metadata (a manifest file) that the JRE understands

Upload: kristy

Post on 03-Feb-2016

74 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Ant & Jar

Ant & JarAnt – Java-based build toolJar – pkzip archive, that contains

metadata (a manifest file) that the JRE understands

Page 2: Ant & Jar

jarUse 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 packages

Page 3: Ant & Jar

Creating a jarfileOptions much like tar c – create x – extract u – updateOthers v – verbose f – specify archive filename m – include manifest info from named

file

Page 4: Ant & Jar

Creating a jarfile - exampleGiven class files:pokerSuit.class, pokerClient.class

Create 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 files

Page 5: Ant & Jar

Executing a jarfileJar 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.jarMain-Class: pokerClient Create the jarfile w/this info:$ jar cvfm poker.jar poker.mf *.class

To run:java –jar poker.jar

Page 6: Ant & Jar

AntA bit like MakeCross-platform Extended w/Java classes, rather than

shell-dependent scriptsReads XML configuration filesJava-basedOpen source

Page 7: Ant & Jar

Why another build tool?Make tools are shell basedDevelopment across multiple platforms

Page 8: Ant & Jar

Ant vs MakeAnt is Cross Platform Ant will work on any platform that has a Java

VM.Make relies on specific OS shell commandsSyntax Make has a scary looking syntax Make tab problem Ant uses XML

self documentingEfficiency

Page 9: Ant & Jar

AntPROs Fast Compiles Platform

Independent Easier to use than

make

CONsnon-Java projects

Page 10: Ant & Jar

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.

Page 11: Ant & Jar

ResourcesAnt : the definitive guide ISBN: 0596001843Java tools for eXtreme Programming ISBN: 047120708X Electronic resourceAnt add-ons: http://ant.apache.org/

external.html

Page 12: Ant & Jar

Linkshttp://ant.apache.org/http://ant.apache.org/resources.htmlhttp://ant.apache.org/manual/index.html Nice description of the various

tags/tasks

Page 13: Ant & Jar

Key WordsBuild File Collection of targets (hopefully logical)Target Collection of tasks Can have dependencies (other targets,

etc.)Task Specific commands for building, running,

etc

Page 14: Ant & Jar

Ant DetailsAll 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.

Page 15: Ant & Jar

build.xmlThe default build file Ant looks for-f option allows to specify a different build file

Page 16: Ant & Jar

How 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

$ ant

If you don’t specify, the default is executed

Page 17: Ant & Jar

build.xml Example<project default="compile"> <target name="compile">

<javac srcdir="." /> </target>

</project> Simply compiles all *.java files in the

current directory (calls javac)

Page 18: Ant & Jar

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>

Page 19: Ant & Jar

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>

Page 20: Ant & Jar

Ant SetupAnt is installed in /usr/local/bin/antJAVA_HOME variable should be set should be set to prefix of /bin/java On the CS machines:

/usr/localOR /opt/java5/jdk1.5.0_06/

Page 21: Ant & Jar

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

filesmkdir creates a directory and any missing parent

directories

Page 22: Ant & Jar

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

Page 23: Ant & Jar

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

Page 24: Ant & Jar

Extra TasksJUnit runs JUnit tests requires junit.jar from http://junit.org

FTP lists, gets, puts and deletes files on an FTP server requires NetComponents.jar

Page 25: Ant & Jar

ExamplesGiven 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>

Page 26: Ant & Jar

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

Page 27: Ant & Jar

ExampleAdd a "clean" target

<target name="clean"> <delete> <fileset dir="./"> <include name='*.class'/> </fileset> </delete></target>

Page 28: Ant & Jar

ExampleAdd a "run" target:

<target name="run" depends='compile'> <java classname='foo'/></target>

Page 29: Ant & Jar

ExampleAdd 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>

Page 30: Ant & Jar

ExampleRun the jarfile:

<target name="run" depends='jar'> <java jar='foo.jar' fork='true'/>

</target>