How to detect duplicate JARs in the classpath?

旧城冷巷雨未停 提交于 2019-12-02 18:07:15

JBoss Tattletale might help you with this.

It's a free tool which scans the JAR files used by your project and gives you a report about them.

Amongst its feature are:

  • Spot if a class is located in multiple JAR files
  • Spot if the same JAR file is located in multiple locations
  • Find similar JAR files that have different version numbers

There is a Maven plugin to do just that: maven-duplicate-finder-plugin

[EDIT] If you want to do it in unit tests yourself, use

getClass().getClassLoader().getResources( "com/pany/package/Java.class" )

If this returns more than one URL, you have duplicates on the classpath.

The drawback is that this only works for cases where you had conflicts in the past. On the positive side, it's just a few lines of code (or one line when you write a helper method) and it works on your build server.

System.getProperty("java.class.path"), split it, sort it, look at it with the human eye :-).

It will not include the classpath derived from manifests inside other jars thou :-(.

Or use http://www.jboss.org/tattletale as one of the posters suggested.

I think the simplest way is to simply trash the target directory first. Hopefully copying all the .jar files in isn't going to be time-consuming.

Otherwise you're going to have to somehow compare sizable files (whether directly, via computed checksum or similar). Which doesn't sound very nice at all.

You can write a simple script to compare the md5 sum of every jar file to every other jar file and deleting duplicates along the way.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!