Related Entries

Simple MongoDB+Java example
Lightweight JSP development environments
jEdit with jGoodies
Jython is great
Fake J2EE is not that bad!

« Quick Ref: Linux Mint 11 #1
» Bash infinite loop script

Remove duplicate jars

Quick hack to get rid of a small pain

Often you will find that you’ve multiple versions of the same dependency jars in your WEB-INF/lib or your classpath directories when you are merging multiple modules. This small shell script can be run on those directories to get rid of older versions of the same jar.

#in a directory full of jar files, it is common to have multiple
#versions of the same jars. If versions are named with only periods
#and alphanumerics, then this groups the jars by the name, deletes
#all the lesser versions

#get a list of jars like x-1.0.0.RELEASE.jar, get only the jar name
#without version or .jar and get only non unique lines (ie., duplicate
#jars are found)
for PREFIX in `ls *.jar|sed  ’s/-[0-9\.a-zA-Z]*\.jar//g'|uniq -d`; do
    echo $PREFIX
    #now do a reverse sorted listing with the jar name and remove the
    #top line so that non latest versions are returned
    for FILE in `ls -r ${PREFIX}*|sed '1d'`; do
        echo "  $FILE"
        rm -f $FILE