java 如何在类路径中检测重复的 JAR?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/878003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:12:12  来源:igfitidea点击:

How to detect duplicate JARs in the classpath?

javajarclasspath

提问by Aaron Digulla

Does anyone have code to detect duplicate JARs in the classpath?

有没有人有代码来检测类路径中的重复 JAR?

Background: When there are two versions of the same JAR in the classpath, really strange things can happen. This can even happen when using tools like Maven: Change a dependency and build the WAR without cleaning first. Since target/webapp/WEB-INF/libwasn't cleaned, the dependency will be in there twice.

背景:当类路径中有同一个 JAR 的两个版本时,可能会发生非常奇怪的事情。这甚至可能在使用 Maven 等工具时发生:更改依赖项并构建 WAR,而无需先清理。由于target/webapp/WEB-INF/lib没有清理,依赖将在那里两次。

Is there a safety-net for this?

这有安全网吗?

采纳答案by Dave Webb

JBoss Tattletalemight help you with this.

JBoss Tattletale可能会帮助您解决这个问题。

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

它是一个免费工具,可扫描您的项目使用的 JAR 文件并为您提供有关它们的报告。

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
  • 发现一个类是否位于多个 JAR 文件中
  • 发现同一个 JAR 文件是否位于多个位置
  • 查找具有不同版本号的类似 JAR 文件

回答by Aaron Digulla

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

有一个 Maven 插件可以做到这一点: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.

如果这返回多个URL,则类路径上有重复项。

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.

缺点是这仅适用于您过去发生冲突的情况。从积极的方面来说,它只是几行代码(或者当您编写辅助方法时为一行)并且它可以在您的构建服务器上运行。

回答by Mihai Toader

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

System.getProperty("java.class.path"),拆分它,排序它,用人眼看它:-)。

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

它不会包括从其他 jar 中的清单派生的类路径:-(。

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

或者使用http://www.jboss.org/tattletale作为建议的海报之一。

回答by Brian Agnew

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

我认为最简单的方法是先简单地删除目标目录。希望复制所有 .jar 文件不会很耗时。

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.

否则你将不得不以某种方式比较相当大的文件(无论是直接的,通过计算的校验和或类似的)。这听起来不太好听。

回答by neesh

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.

您可以编写一个简单的脚本来比较每个 jar 文件的 md5 总和与其他每个 jar 文件,并在此过程中删除重复项。