Download presentation
Presentation is loading. Please wait.
Published byTerence Goodwin Modified over 8 years ago
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 Simply compiles all *.java files in the current directory (calls javac)
18
Complex ant build file example
19
Complex example (cont.) <java classname="hello" classpath="${lib-dir}/hello.jar" fork="true" />
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 compiles Java source files OR 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 copies files deletes given file or dir or msg 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.orghttp://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:
26
Example compile only foo and bar:
27
Example Add a "clean" target
28
Example Add a "run" target:
29
Example Add a target to build a jarfile: <jar destfile='./foo.jar' manifest='man.mf'>
30
Example Run the jarfile:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.