Building JAR Files
Problem
You want to build a JAR file.
Solution
Use Ant’s jar
task.
Discussion
The jar
task creates JAR files, as
expected. In its simplest form, you specify the name of the new JAR
file along with the directory containing files to add to the archive.
All files in the directory specified by basedir
along with subdirectories and files are added:
<jar jarfile="${dir.dist}/oreilly.jar" basedir="${dir.build}"/>
The jar
task tries to be as efficient as possible.
Before creating a new JAR, it checks for an existing archive and
compares file timestamps. In our example,
oreilly.jar
is only created if it does not
exist, or if any of the files in ${dir.build}
are
newer than oreilly.jar
.
This next example refines our operation by only including
.class
files, unless they are unit tests
matching the Test*.class
pattern:
<jar jarfile="${dir.dist}/oreilly.jar" basedir="${dir.build}" includes="**/*.class" excludes="**/Test*.class"/>
The includes
and excludes
attributes use the ** pattern to represent any subdirectory. Use
nested <fileset>
tags for more sophisticated
selections:
<jar jarfile="${dir.dist}/oreilly.jar"> <fileset dir="${dir.build}" includes="**/*.class" excludes="**/Test*.class"/> <fileset dir="${dir.src}" includes="**/*.properties"/> </jar>
This JAR file consists of all .class
files
(except for Test*.class
) in the build directory
tree. It also contains all .properties
files
under the source directory tree.
Warning
Ant makes it completely trivial to exclude test cases from a ...
Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.