冗余jar包識別神器 - loose.jar
場景描述
項目迭代久了,會添加各類jar包,事實上很多jar包其實根本沒用到导犹。那如何快速識別冗余的jar,以方便從項目中清除掉呢羡忘?
比如:
該簡單的測試工程lib里有63個jar包谎痢,如何掃描出真正使用到的jar包?
操作步驟
1) install后卷雕,命令行運行該程序节猿,確保運行正常
D:\Insight\InsightWhys\target>java -jar InsightWhys-1.0.jar Main_MultiTask
2017-09-02 19:37:25:930
Main_MultiTask Done......
消耗177802開始: 2017-09-02 19:37:26:001
對于token:1000000156301_token已經(jīng)消耗過
2) 放置loosejar.jar
3) agent啟動跑程序
D:\Insight\InsightWhys\target>java -javaagent:loosejar-1.0.1.jar -jar InsightWhys-1.0.jar Main_MultiTask
[loosejar]: loosejar analysis is registered to run on JVM shutdown.
[loosejar]: Registering loosejar as a JMX service...
[loosejar]: Registered loosejar as a JMX service: [com.googlecode.loosejar:type=LooseJarMBean]
Main_MultiTask Done......
消耗156301開始: 2017-07-07 11:34:59:248
對于token:1000000156301_token已經(jīng)消耗過
[loosejar]: Found 2014 classes loaded in the JVM.
[loosejar]: Found 122 various ClassLoader(s) inside the JVM.
[loosejar]: Summary for [sun.misc.Launcher$AppClassLoader] classloader:
Jar: D:\Insight\InsightWhys\target\TcThriftClient-1.0-SNAPSHOT.jar
Utilization: 2.51% - loaded 15 of 597 classes.
Jar: D:\Insight\InsightWhys\target\lib\libthrift-0.5.0.jar
Utilization: 22.94% - loaded 25 of 109 classes.
Jar: D:\Insight\InsightWhys\target\lib\finagle-core_2.10-6.20.0.jar
Utilization: 0.00% - loaded 0 of 1843 classes.
Jar: D:\Insight\InsightWhys\target\lib\scala-library-2.10.4.jar
Utilization: 0.00% - loaded 0 of 4899 classes.
Jar: D:\Insight\InsightWhys\target\lib\netty-3.9.1.1.Final.jar
Utilization: 0.00% - loaded 0 of 833 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-app_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 87 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-core_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 911 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-collection_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 34 classes.
Jar: D:\Insight\InsightWhys\target\lib\jsr305-1.3.9.jar
Utilization: 0.00% - loaded 0 of 35 classes.
Jar: D:\Insight\InsightWhys\target\lib\commons-collections-3.2.1.jar
Utilization: 0.00% - loaded 0 of 458 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-hashing_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 40 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-jvm_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 130 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-logging_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 141 classes.
Jar: D:\Insight\InsightWhys\target\lib\scalatest_2.10-2.1.3.jar
Utilization: 0.00% - loaded 0 of 3929 classes.
Jar: D:\Insight\InsightWhys\target\lib\scala-reflect-2.10.3.jar
Utilization: 0.00% - loaded 0 of 2036 classes.
Jar: D:\Insight\InsightWhys\target\lib\finagle-thrift_2.10-6.20.0.jar
Utilization: 0.00% - loaded 0 of 209 classes.
Jar: D:\Insight\InsightWhys\target\lib\finagle-serversets_2.10-6.20.0.jar
Utilization: 0.00% - loaded 0 of 464 classes.
Jar: D:\Insight\InsightWhys\target\lib\commons-lang-2.6.jar
Utilization: 0.00% - loaded 0 of 133 classes.
Jar: D:\Insight\InsightWhys\target\lib\commons-io-2.1.jar
Utilization: 0.00% - loaded 0 of 104 classes.
Jar: D:\Insight\InsightWhys\target\lib\slf4j-api-1.6.1.jar
Utilization: 39.13% - loaded 9 of 23 classes.
Jar: D:\Insight\InsightWhys\target\lib\slf4j-jdk14-1.6.1.jar
Utilization: 60.00% - loaded 3 of 5 classes.
Jar: D:\Insight\InsightWhys\target\lib\guice-3.0.jar
Utilization: 0.00% - loaded 0 of 565 classes.
Jar: D:\Insight\InsightWhys\target\lib\aopalliance-1.0.jar
Utilization: 0.00% - loaded 0 of 9 classes.
Jar: D:\Insight\InsightWhys\target\lib\gson-2.2.4.jar
Utilization: 0.00% - loaded 0 of 153 classes.
Jar: D:\Insight\InsightWhys\target\lib\zookeeper-3.3.4.jar
Utilization: 0.00% - loaded 0 of 341 classes.
Jar: D:\Insight\InsightWhys\target\lib\log4j-1.2.15.jar
Utilization: 0.00% - loaded 0 of 259 classes.
Jar: D:\Insight\InsightWhys\target\lib\finagle-test_2.10-6.20.0.jar
Utilization: 0.00% - loaded 0 of 11 classes.
Jar: D:\Insight\InsightWhys\target\lib\commons-codec-1.6.jar
Utilization: 0.00% - loaded 0 of 76 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-zk-common_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 27 classes.
Jar: D:\Insight\InsightWhys\target\lib\util-zk_2.10-6.19.0.jar
Utilization: 0.00% - loaded 0 of 217 classes.
Jar: D:\Insight\InsightWhys\target\lib\guava-16.0.1.jar
Utilization: 0.00% - loaded 0 of 1678 classes.
Jar: D:\Insight\InsightWhys\target\lib\jackson-core-2.3.1.jar
Utilization: 0.00% - loaded 0 of 87 classes.
Jar: D:\Insight\InsightWhys\target\lib\jackson-databind-2.3.1.jar
Utilization: 0.00% - loaded 0 of 565 classes.
Jar: D:\Insight\InsightWhys\target\lib\jackson-annotations-2.3.0.jar
Utilization: 0.00% - loaded 0 of 49 classes.
Jar: D:\Insight\InsightWhys\target\lib\jackson-module-scala_2.10-2.3.1.jar
Utilization: 0.00% - loaded 0 of 404 classes.
Jar: D:\Insight\InsightWhys\target\lib\paranamer-2.6.jar
Utilization: 0.00% - loaded 0 of 20 classes.
4) 用jconsole查看使用到的jar(該步驟可以省略,日志已在第3步打印了)
5) 統(tǒng)計出實際使用到36個jar
aopalliance-1.0.jar
commons-codec-1.6.jar
commons-collections-3.2.1.jar
commons-io-2.1.jar
commons-lang-2.6.jar
finagle-core_2.10-6.20.0.jar
finagle-serversets_2.10-6.20.0.jar
finagle-test_2.10-6.20.0.jar
finagle-thrift_2.10-6.20.0.jar
gson-2.2.4.jar
guava-16.0.1.jar
guice-3.0.jar
jackson-annotations-2.3.0.jar
jackson-core-2.3.1.jar
jackson-databind-2.3.1.jar
jackson-module-scala_2.10-2.3.1.jar
jsr305-1.3.9.jar
libthrift-0.5.0.jar
log4j-1.2.15.jar
netty-3.9.1.1.Final.jar
paranamer-2.6.jar
scala-library-2.10.4.jar
scala-reflect-2.10.3.jar
scalatest_2.10-2.1.3.jar
slf4j-api-1.6.1.jar
slf4j-jdk14-1.6.1.jar
util-app_2.10-6.19.0.jar
util-collection_2.10-6.19.0.jar
util-core_2.10-6.19.0.jar
util-hashing_2.10-6.19.0.jar
util-jvm_2.10-6.19.0.jar
util-logging_2.10-6.19.0.jar
util-zk-common_2.10-6.19.0.jar
util-zk_2.10-6.19.0.jar
zookeeper-3.3.4.jar
InsightWhys-1.0.jar
6) 回歸驗證,任意刪除lib里這36個jar之外的jar滨嘱,跑程序峰鄙,正常
總結(jié)
該方法可以快速知道程序?qū)嶋H依賴的jar,雖然其他jar在不同case下可能會依賴太雨,但可以有目的去人工校驗或case覆蓋吟榴,結(jié)合IDEA show dependencies來用更佳