Automatic Bash Script for Unpackage/Repackage of Java WAR/JAR files

Had a need today to modify the contents of a WAR file and replace/update a specific XML file.

So you guessed it. I wrote a quick script for it.

Below are some usage screenshots:

Unpackaging a WAR:

Repackaging a WAR:


Here is the code:

unpackWar() {
    echo "Type Full Java JAR/WAR Path: "
	read FILE_PATH_FULL

    #echo "FILE_PATH_FULL:" 
    #echo $FILE_PATH_FULL

    FILE_EXTENSION=$(echo ${FILE_PATH_FULL##*.})

    #echo "FILE_EXTENSION:"
    #echo $FILE_EXTENSION

    FILE_NAME_WITHOUT_EXTENSION=$(basename "$FILE_PATH_FULL" ".$FILE_EXTENSION")

    #echo "FILE_NAME_WITHOUT_EXTENSION:"
    #echo $FILE_NAME_WITHOUT_EXTENSION

    FILE_NAME_WITH_EXTENSION="$FILE_NAME_WITHOUT_EXTENSION"
    FILE_NAME_WITH_EXTENSION+="."
    FILE_NAME_WITH_EXTENSION+="$FILE_EXTENSION"
    
    #echo "FILE_NAME_WITH_EXTENSION:"
    #echo $FILE_NAME_WITH_EXTENSION

    FILE_PATH_DIRECTORY=$(printf '%s\n' "${FILE_PATH_FULL//$FILE_NAME_WITH_EXTENSION/}")
    echo $FILE_PATH_DIRECTORY

    #echo "FILE_PATH_DIRECTORY:"
    #echo $FILE_PATH_DIRECTORY

    OUTPUT_DIRECTORY="$FILE_PATH_DIRECTORY/$FILE_NAME_WITHOUT_EXTENSION"

    #echo "OUTPUT_DIRECTORY:"
    #echo $OUTPUT_DIRECTORY

    OUTPUT_DIRECTORY_TEMP_FILE=$OUTPUT_DIRECTORY
    OUTPUT_DIRECTORY_TEMP_FILE+="/"
    OUTPUT_DIRECTORY_TEMP_FILE+=$FILE_NAME_WITH_EXTENSION

    #echo "OUTPUT_DIRECTORY_TEMP_FILE:"
    #echo $OUTPUT_DIRECTORY_TEMP_FILE

    mkdir "$OUTPUT_DIRECTORY"
    cp "$FILE_PATH_FULL" "$OUTPUT_DIRECTORY_TEMP_FILE"
    
    cd "$OUTPUT_DIRECTORY"
    jar -xf "$OUTPUT_DIRECTORY_TEMP_FILE" 
    rm "$OUTPUT_DIRECTORY_TEMP_FILE"
}

repackWar() {
    echo "Type Path To Package: "
	read OUTPUT_DIRECTORY

    echo "Type The Final File Extension (Without Period): "
	read FILE_EXTENSION

    FILE_NAME_WITH_EXTENSION=$(basename "$OUTPUT_DIRECTORY")
    FILE_NAME_WITH_EXTENSION+="."
    FILE_NAME_WITH_EXTENSION+=$FILE_EXTENSION
    
    cd "$OUTPUT_DIRECTORY"
    jar -cf "$FILE_NAME_WITH_EXTENSION" .
}

# Bash Menu Script Example
choiceFunction() {
	SUCCESS="false"

	while [ $SUCCESS == "false" ]
	do
		clear
		echo "Please enter your choice: "
		echo "1 - Unpackage War" 
		echo "2 - Repackage War" 
		echo "3 - Quit"
		read CHOICE

		case $CHOICE in
		   "1") SUCCESS="true";echo "Unpacking War ..."; unpackWar;;
		   "2") SUCCESS="true";echo "Packing War ..."; repackWar;;
		   "3") SUCCESS="true";echo "Exiting ..."; exit;;
		   *) read -p "Invalid choice, Press any key to continue..." fakeVariableIgnoreMe;;
		esac
	done
}

choiceFunction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s