Skip to content

Instantly share code, notes, and snippets.

@nyxcalamity
Last active December 31, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyxcalamity/7916644 to your computer and use it in GitHub Desktop.
Save nyxcalamity/7916644 to your computer and use it in GitHub Desktop.
Removes old libs from a jar and adds new libs to it. Perfect for adding new eclipselink to glassfish-3.1.2.2-embedded jar.
#!/bin/bash
#DESCRIPTION:
#Generates a new jar in which:
#-Classes from all jars in $OLD_LIBS are removed
#-Files fron all jars in $NEW_LIBS are added (except META-INF dir, *.html, anything with "LICENCE")
#
#REQUIRES:
#-Installed jar and/or keytool
#
#HOW TO USE:
#1)Decide from which directory you'll run the script - that will be your $WORKSPACE (default to pwd)
#2)Copy there the jar you want to update - $JAR
#3)(optional)Create there an $OLD_LIBS directory. It will be used to remove libs from the jar.
#4)(optional)Create there a $NEW_LIBS directory. It will be used to add libs to the jar.
#5)Edit a section below of this script and set all the needed variables.
#5.1)(optional)Change $CERT_ALIAS if you want to remove that cert from the jar.
#5.2)(optional)Edit the $JAR_NEW_NAME to set preferred name to your jar.
#---------------------------------------------------------------------------------------------------
# CUSTOMIZATION SECTION START
#---------------------------------------------------------------------------------------------------
export WORKSPACE=/home/nyxcalamity/ws/ws-crx/gf
export NEW_LIBS=$WORKSPACE/libs/libs_new
export OLD_LIBS=$WORKSPACE/libs/libs_old
export JAR=$WORKSPACE/glassfish-embedded-all-3.1.2.2.jar
export REPORTING=$WORKSPACE/reporting
export REPORT_FILE=$REPORTING/report.txt
export LISTING_FILE=$REPORTING/listing.txt
export LISTING="META-INF/services"
export JAR_NEW_NAME=glassfish-embedded-all-3.1.2.2-crx07.jar
export CERT_ALIAS="gtecybertrust5ca"
#---------------------------------------------------------------------------------------------------
# CUSTOMIZATION SECTION END
#---------------------------------------------------------------------------------------------------
#STAGE 0: Init
#---------------------------------------------------------------------------------------------------
export OK="$(tput setaf 2)DONE$(tput sgr0)"
if [ -z "$WORKSPACE" ]; then
export WORKSPACE=$(pwd)
fi
#---------------------------------------------------------------------------------------------------
#STAGE 1: Processing the jar
#---------------------------------------------------------------------------------------------------
#Extracting jar contents
echo -n "Extracting jar contents: "
export JAR_CONTENTS=$WORKSPACE/$(basename $JAR .jar)
rm -rf $JAR_CONTENTS; mkdir -p $JAR_CONTENTS
cd $JAR_CONTENTS
jar xf $JAR
echo $OK
#---------------------------------------------------------------------------------------------------
#STAGE 2: Processing old libs
#---------------------------------------------------------------------------------------------------
#Processing contents of old libs
if [ -n "$OLD_LIBS" ]; then
echo "Removing old libs..."
export OLD_LIBS_CONTENTS=$OLD_LIBS/contents
for f in $OLD_LIBS/*.jar; do
#Extracting file contents
echo -ne "Processing file $f: "
rm -rf $OLD_LIBS_CONTENTS; mkdir -p $OLD_LIBS_CONTENTS
cd $OLD_LIBS_CONTENTS
jar xf $f
#Removeing classes from jar contents if they exist
for c in $(find -name '*.class'); do
if [ -e $c ]; then
rm -f $JAR_CONTENTS/$c
fi
done
#Cleanup
rm -rf $OLD_LIBS_CONTENTS
echo $OK
done
fi
#---------------------------------------------------------------------------------------------------
#STAGE 3: Processing new libs
#---------------------------------------------------------------------------------------------------
#Processing contents of new libs
if [ -n "$NEW_LIBS" ]; then
echo "Adding new libs..."
export NEW_LIBS_CONTENTS=$NEW_LIBS/contents
rm -rf $REPORTING; mkdir -p $REPORTING
for f in $NEW_LIBS/*.jar; do
#Extracting file contents
echo -en "Processing file $f: "
rm -rf $NEW_LIBS_CONTENTS; mkdir -p $NEW_LIBS_CONTENTS
cd $NEW_LIBS_CONTENTS
jar xf $f
#Listing dirs
echo $f >> $LISTING_FILE
unzip -l $f | grep $LISTING >> $LISTING_FILE
#Removing all the redundant stuff
for i in $(find . -name "META-INF" -or -name "*.html" -or -name "*LICENCE*"); do
rm -rf $i
done
#Checking what we'll overwrite
for fl in $(find $NEW_LIBS_CONTENT); do
if [ -f $JAR_CONTENTS/$fl ]; then
echo "Overwriting $JAR_CONTENTS/$fl" >> $REPORT_FILE
fi
done
#Copying files to jar contents
cp -r $NEW_LIBS_CONTENTS/* $JAR_CONTENTS
#Cleanup
rm -rf $NEW_LIBS_CONTENTS
echo $OK
done
fi
#---------------------------------------------------------------------------------------------------
#STAGE 3: Cleaning up the certs
#---------------------------------------------------------------------------------------------------
if [ -n "$CERT_ALIAS" ]; then
cd $JAR_CONTENTS
for i in $(find $JAR_CONTENTS -name "cacerts.jks"); do
echo "Removing $CERT_ALIAS certificate..."
keytool -delete -keystore $i -alias $CERT_ALIAS
done
fi
#---------------------------------------------------------------------------------------------------
#STAGE 4: Packaging new jar
#---------------------------------------------------------------------------------------------------
echo -n "Packaging new jar: "
rm -f $(dirname $JAR)/$JAR_NEW_NAME
cd $JAR_CONTENTS
jar cmf $JAR_CONTENTS/META-INF/MANIFEST.MF $(dirname $JAR)/$JAR_NEW_NAME ./*
rm -rf $JAR_CONTENTS
echo $OK
echo "$(tput setaf 4)Generation of new jar completed. Please find it here:$(tput sgr0)"
echo "$(tput setaf 4)$(dirname $JAR)/$JAR_NEW_NAME $(tput sgr0)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment