-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_dependency.sh
executable file
·93 lines (82 loc) · 2.71 KB
/
check_dependency.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
#!/bin/bash
ignore_classes=("java" "sun" "com.alibaba.service" "com.alibaba.turbine" "com.alibaba.common" "com.alibaba.webx" "org.apache.commons" "com.alibaba.intl.commons" "org.junit" "org.apache.log4j" "com.alibaba.intl.risk.ruleengine")
#update bash to v4.2 http://zengrong.net/post/1830.htm
M2_REPO=$1
if [ ${M2_REPO:(-1)} == "/" ]; then
M2_REPO=${M2_REPO:0:${#M2_REPO}-1}
fi
echo "[info] maven repository is $M2_REPO"
echo "[info] generate dependency tree"
cd $2/all
mvn dependency:tree > dt
build=`cat dt|grep 'BUILD ERROR'`
if [[ -n $build ]]; then
echo "[error] build error,please check first"
exit
fi
echo "[info] get all direct dependency jar or car's absolutely path"
dep_jar_list=`cat dt |grep '] +-'|grep -v war$|awk -F ' ' '{print $3}'|awk -F ':' '{gsub(/\./,"/",$1);print "/Users/zxb/.m2/repository/"$1"/"$2"/"$4"/"$2"-"$4"."$3}'|sort|uniq`
echo "[info] get all classes of a jar"
declare -A jar_map
for dep_jar in $dep_jar_list
do
if [ -f $dep_jar ]; then
echo "[debug] $dep_jar"
#list all classes in jar
class_list=`jar -tvf $dep_jar|grep class$|awk -F ' ' '{print $8}'`
for class in $class_list
do
classpath=$class
#only care class file
if [ ${class:(-5)} == "class" ]; then
#if car,remove prefix 'WEB-INF/classes/'
if [ ${dep_jar:(-3)} == "car" ];then
classpath=${class:16:${#class}}
fi
jar_map[$classpath]="$dep_jar"
#echo "[debug] $classpath"
fi
done
fi
done
echo "[info] list all java files,parse import class,get it's jar path"
list=`find ..|grep src|grep -v java.test|grep -v Test.java$|grep java$`
for line in $list
do
#echo $line
if [ -d $line ]; then
continue
fi
#list all import
import_list=`grep ^import $line`
for import_line in $import_list
do
if [ ${import_line:(-1)} == ";" ];then
#remove last charactor,and replace all '.' to '/',and append '.class'
ref_class=${import_line:0:${#import_line}-1}
#ignore some common class
flag=0
for ignore in ${ignore_classes[@]}
do
ignored=`echo $ref_class|grep ^$ignore`
if [ ${#ignored} -gt 1 ]; then
flag=1
break
fi
done
if [ $flag -eq 1 ]; then
#echo "ignore:$ref_class"
continue
fi
ref_class=${ref_class//./\/}
ref_class="$ref_class"".class"
#echo $ref_class
jar_path=${jar_map[$ref_class]}
if [[ -z $jar_path ]]; then
echo "[warn] java:$line"
echo "[warn] import:$import_line"
echo "[warn] "
fi
fi
done
done