Duplicate Package Finder
It’s pretty common to encounter a situation where you want the following question answered.
“Find me the java packages in classpath ‘X’ which are already present in ‘Y’”
where X = Classpath of an application;
and Y = System Classpath or some other classpath
I wrote a small command line utility which answers this question and groks the list of duplicate patterns. For WebLogic Server users, this tool finds the duplicate packages and also emits some xml which can be pasted into ‘META-INF/weblogic-application.xml’ to ensure that the version in the application always wins.
Here’s the usage:
~/code$ java -jar ./utils/dist/dupPkgfinder.jar Usage: net.nagiworld.utils.DupPackageFinder -appclasspath [String] Application Classpath -parentclasspath [String] Classpath to compare against -advice [flag] Emit Advice for use with WebLogic Server -verbose [flag] Show me what's going on
Example: Lets say I have a webapp which uses a specific version of log4j.jar. Since WebLogic already packages a version of log4j.jar in its System Classpath, using a different version requires an explicit descriptor element.
Given that the jar is in WEB-INF/lib, I ask the tool to help me out:
~/code/utils/test$ java -jar ../dist/dupPkgfinder.jar
-parentclasspath $CLASSPATH -appclasspath './WEB-INF/lib/*'
-advice
Duplicate Packages:
org/apache/log4j
org/apache/log4j/chainsaw
org/apache/log4j/config
org/apache/log4j/helpers
org/apache/log4j/jdbc
org/apache/log4j/jmx
org/apache/log4j/lf5
org/apache/log4j/lf5/util
org/apache/log4j/lf5/viewer
org/apache/log4j/lf5/viewer/categoryexplorer
org/apache/log4j/lf5/viewer/configure
org/apache/log4j/net
org/apache/log4j/nt
org/apache/log4j/or
org/apache/log4j/or/jms
org/apache/log4j/or/sax
org/apache/log4j/spi
org/apache/log4j/varia
org/apache/log4j/xml
Recommendation: Add the following snippet to 'META-INF/weblogic-application.xml'.
<prefer-application-packages>
<package-name>org.apache.log4j.*</package-name>
</prefer-application-packages>
If you’re familiar with FilteringClassLoader, then this tool is an excellent aid to creating the filter patterns that should be added to the weblogic-application.xml descriptor. See http://edocs.bea.com/wls/docs92/programming/classloading.html for more details on FilteringClassLoader.
NOTE: Duplicate packages in your application does not necessarily imply that FilteringClassLoader be used. Often times unused jars also end up in WEB-INF/lib
This tool is a thin wrapper our DepHandler from jarjar. In addition to that it also depends on cli-parser which is used for command line parsing. (using cool annotations!). The tool supports wildcards thanks to the jarjarlinks support for wildcards.
You can download the tool here - dupPkgFinder.jar
Enjoy! Please do share your comments and any other cool uses/improvements for the tool




Add New Comment
Viewing 4 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)