sixlegs.com / blog / java / classpath-wildcards.html

Root Beer Logo Root Beer

Chris Nokleberg's Fizzy Weblog

March 2006
Su M Tu W Th F Sa
      1 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 21 22 23 24 25
26 27 28 29 30 31
Previous  |  Next  |  More...
#  Jar Jar now supports Mustang-style classpath wildcards

Classpath wildcards are a long-awaited feature which are finally making their appearance in Mustang. You can read more about them here and here.

Now that the syntax is nailed down I've added the same capabilities to the latest release of the Jar Jar Links library. In particular the dependency finding feature is much easier to use now:

$ java -jar jarjar.jar --find --level=jar 'lib/*'

This will parse all of the jar files in the lib directory and print out all of the inter-jar dependencies. Note that the asterisk normally must be quoted in order to prevent expansion by the shell.

One good use I've found for the dependency code is in building WAR files. We have a number of web applications built from the same source tree, and each one depends on a slightly different set of libraries. The simple approach is to just dump every library into every WAR, but it is not too much harder to use Jar Jar to determine exactly which libraries should be included.

Because the format of the --find command to Jar Jar is not exactly what we need, we can use the programmatic API instead:

import com.tonicsystems.jarjar.*;
public class DepDump
{
    public static void main(String[] args) throws Exception {
        DepHandler handler = new AbstractDepHandler(DepHandler.LEVEL_JAR){
            public void handle(String from, String to) {
                System.out.println(new File(to).getName());
            }
        };
        new DepFind().run(args[0], args[1], handler);
    }
}

This will only print out the dependencies in the second classpath argument. For example:

$ java DepDump build/main 'lib/*' > includes.txt

Assuming all of your class files are in build/main and your libraries in lib, this will only print out the names of the libraries that your code actually uses. Redirecting the output to includes.txt is useful because in our Ant task we can use the includesfile option to only add those particular libraries to our WAR:

<zipfileset dir="lib" prefix="WEB-INF/lib" includesfile="includes.txt"/>

If you are using the JarJarTask to embed other libraries, using the DepFind API can also be useful to verify that the resulting library has zero dependencies.

[Powered By FreeMarker]  [Valid Atom 1.0]  [Weblog Commenting by HaloScan.com]