-
Notifications
You must be signed in to change notification settings - Fork 6
/
extract-jars.sh
97 lines (70 loc) · 2.42 KB
/
extract-jars.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
HOME_DIRECTORY=`pwd`
cd ~
USER_DIRECTORY=`pwd`
cd $HOME_DIRECTORY
LIB_DIRECTORY="$HOME_DIRECTORY"/lib
cfrFile="$LIB_DIRECTORY"/cfr_0_110.jar
foundJava=`type -p java`
if [ "`type -p java`" = "" ]; then
echo "Command \"java\" not found. You may not install JDK on your machine."
exit
fi
if [ "`type -p jar`" = "" ]; then
echo "Command \"jar\" not found. You may not install JDK on your machine."
exit
fi
if [ "$MODE" != "test" ]; then
echo "-----"
echo "Tool for extracting all .war and .jar files"
read -p "1. Enter the full path of source directory (ex: $USER_DIRECTORY/.m2/repository): " sourceDirectory
if [ ! -d "$sourceDirectory" ]; then
echo "\""$sourceDirectory"\" is not a valid directory"
exit
fi
read -p "2. Enter the full path of destination directory (ex: $USER_DIRECTORY/code-collection/target): " destinationDirectory
if [ ! -d "$destinationDirectory" ]; then
echo "\""$destinationDirectory"\" is not a valid directory"
exit
fi
fi
echo "Begin scan for jar files and extract them to the destination directory"
echo ">> Source: " $sourceDirectory
echo ">> Destination: " $destinationDirectory
totalJarFiles=0
startTime=`date`
cd $sourceDirectory
for jarFile in `find . -type f -name "*.jar" -o -name "*.war"`; do
#Extract this jar file
jarFileWithExtension=`basename $jarFile`
#Directory
jarFileRegex="(.*).(jar|war)"
[[ $jarFileWithExtension =~ $jarFileRegex ]]
extractDirectory="${BASH_REMATCH[1]}"
fileExtension="${BASH_REMATCH[2]}"
targetDirectory=$destinationDirectory/$extractDirectory
#Extract
echo "Begin extracting file: " $jarFileWithExtension " to directory " $targetDirectory
if [ ! -d "$targetDirectory" ]; then
mkdir -p $targetDirectory
fi
cd $sourceDirectory
cp $jarFile $targetDirectory
cd $targetDirectory
jar xf $jarFileWithExtension
rm $jarFileWithExtension
#decompile
if [ "$fileExtension" = "jar" ]; then
#use decompile tool
echo "Begin decomplining jar file: " $jarFile
cd $sourceDirectory
java -jar $cfrFile $jarFile --outputdir $targetDirectory --comments false
fi
#remove .class files
cd $targetDirectory
find . -type f -name "*.class" -exec rm {} +
totalJarFiles=$((totalJarFiles+1))
done
endTime=`date`
echo "-----"
echo "Finished extract "$totalJarFiles" jar file(s) to destination directory. Start Time: " $startTime ". End Time: " $endTime
echo "-----"