Skip to content

Instantly share code, notes, and snippets.

@davydany
Last active November 10, 2017 21:00
Show Gist options
  • Save davydany/bbfcdeb4c45585c95da3ac7217db6dfb to your computer and use it in GitHub Desktop.
Save davydany/bbfcdeb4c45585c95da3ac7217db6dfb to your computer and use it in GitHub Desktop.
For those times you need to edit a jar file which has only 1 XML file with the same name as the jar file (minus the extension), and you just don't want to extract the damn file to access 1 file. Use 'vijar' to simplify your life.

jarjar

Usage

jarjar <executable> <path-to-jar-file>

Example

Using VIM:

jarjar vim /tmp/path/to/jar/content.jar:src/config.xml

vijar

Usage

vijar <path-to-jar-file>

Example

Using Relative Paths:

vijar ./content.jar

Using Absolute Paths:

vijar /tmp/path/to/repository/artifacts.jar
# jarjar
# ------
# jarjar is a bash utility for working with individual files inside a jar file
# without having to deal with unzipping a file, and deleting the unzipped directory.
function jarjar {
# determine the name of the xml file name
executable=$1;
jar_name=$2;
if [ "$jar_name" -eq "" ]; then
echo "Usage:";
echo " REQUIRED | $0 <executable> <path-to-jar-file>";
echo " OPTIONAL | $0 <executable> <path-to-jar-file>:<path-to-file-inside-jar>";
else
jar_name=$2;
jar_name_length=${#jar_name};
if [[ ${jar_name} != *":"* ]]; then
substring_offset=0;
substring_length=$(expr $jar_name_length - 4);
internal_path="${jar_name:0:$substring_length}.xml";
else
internal_path_start=$(awk -v a="$jar_name" -v b=":" 'BEGIN{print index(a,b)}');
internal_path_end=$(expr $jar_name_length - $internal_path_start);
internal_path=${jar_name:$internal_path_start:$internal_path_end};
jar_name=${jar_name:0:$(expr $internal_path_start - 1)}
echo "internal_path_start: ${internal_path_start}"
echo "internal_path_end: ${internal_path_end}"
echo "internal_path: ${internal_path}"
fi
echo "INTERNAL PATH: ${internal_path}"
# determine the temp path to extract this jar to
temp_dir=$(mktemp -d);
exc unzip "$jar_name" -d $temp_dir
path_to_temp_xml="$temp_dir/$internal_path";
# edit the file
exc eval $executable $path_to_temp_xml;
ls -lah $(dirname $path_to_temp_xml)
ls -lah $path_to_temp_xml;
# delete the temp dir
rm -rf $temp_dir;
fi
}
# vijar
# -----
# vijar extracts a jar file that contains that contains a file inside with the
# same file name with a .xml file
function vijar {
path_to_jar=$1;
jarjar vim $path_to_jar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment