Document 552433
Download
Report
Transcript Document 552433
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
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
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
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
-------------0 08-06-06 09:30
120 08-06-06 09:30
670 08-06-06 09:19
636 08-06-06 09:19
-------1426
Name
---META-INF/
META-INF/MANIFEST.MF
pokerClient.class
pokerSuit.class
------4 files
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
Ant
A bit like Make
Cross-platform
Extended w/Java classes, rather than shelldependent scripts
Reads XML configuration files
Java-based
Open source
Why another build tool?
Make tools are shell based
Development across multiple platforms
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
Ant
PROs
Fast Compiles
Platform
Independent
Easier to use than
make
CONs
non-Java projects
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 nonJava language.
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
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
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
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.
build.xml
The default build file Ant looks for
-f option allows to specify a different build
file
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
$ ant compile
If you don’t specify,
the default is
executed
$ ant javadoc
$ ant compile javadoc
build.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="${objdir}" />
</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 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/
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
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
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
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
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>
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>
Example
Add a "clean" target
<target name="clean">
<delete>
<fileset dir="./">
<include name='*.class'/>
</fileset>
</delete>
</target>
Example
Add a "run" target:
<target name="run" depends='compile'>
<java classname='foo'/>
</target>
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>
Example
Run the jarfile:
<target name="run" depends='jar'>
<java jar='foo.jar'
fork='true'/>
</target>