冗余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已经消耗过
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已经消耗过
[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) 统计出实际使用到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,跑程序,正常
总结
该方法可以快速知道程序实际依赖的jar,虽然其他jar在不同case下可能会依赖,但可以有目的去人工校验或case覆盖,结合IDEA show dependencies来用更佳