java 使用 Maven 检测循环依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1608110/
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
Detecting cyclic dependencies with Maven
提问by Lóránt Pintér
In my application I'm using an external library (Batik 1.7) that is made up of several modules. The modules have multiple cyclic dependencies between themselves. This doesn't influence the build, but some tools (e.g. the M2Eclipse Dependency Graph, or the Dependencies report) will not work anymore.
在我的应用程序中,我使用了一个由多个模块组成的外部库 (Batik 1.7)。模块之间有多个循环依赖关系。这不会影响构建,但某些工具(例如 M2Eclipse 依赖关系图或依赖关系报告)将不再起作用。
Is there a good way to diagnose what cycles are there, and an easy way to get rid of them?
有没有一种很好的方法来诊断存在哪些周期,以及一种摆脱它们的简单方法?
Update:The problem is with the POMs, as e.g. batik-bridgeis dependent on batik-gvt, which is in turn depend upon batik-bridge.
更新:问题出在 POM 上,例如batik-bridge依赖于batik-gvt,而后者又依赖于batik-bridge。
I think I can solve this by excluding some of the dependencies manually, but I'm not sure what to exclude. What I'd like to have is a clear overview of the cycles in the graph.
我想我可以通过手动排除一些依赖项来解决这个问题,但我不确定要排除什么。我想要的是图表中周期的清晰概览。
采纳答案by serg10
Try running this from the command line in the root of your topmost project:
尝试从最顶层项目根目录的命令行运行:
mvn dependency:tree
回答by Pascal Thivent
I'm not sure if this is maven related (you can't have cyclic dependencies between modules with maven) but maybe I didn't get something. That said, you can use JDependto analyze a piece of code and find cyclic dependencies (see Interpreting Dependency Cycles). If you prefer to use JDepend from Eclipse, there is the JDepend4Eclipseplugin.
我不确定这是否与 maven 相关(你不能在带有 maven 的模块之间建立循环依赖关系)但也许我没有得到一些东西。也就是说,您可以使用JDepend来分析一段代码并找到循环依赖(请参阅解释依赖循环)。如果您更喜欢从 Eclipse 使用 JDepend,则可以使用JDepend4Eclipse插件。
Check out Batik from its subversion repository, run JDepend on its sources and see if you find something (I guess you will). But, to be honest, that was the easy part. Getting rid of cyclic dependencies is another story and might not be that easy. This might involve tasks like moving classes from one package to another, repackaging modules, understanding how Batik's build work (note that its Ant build script has 2220 lines), etc. In other words, this will require some hard work on a library that you initially just want to use (and unless you contribute these changes, you might have to apply them again with a later release). My advice: think about it twice before starting digging in that direction.
从它的subversion 存储库中查看 Batik ,在它的源代码上运行 JDepend,看看你是否找到了一些东西(我猜你会找到的)。但是,老实说,那是容易的部分。摆脱循环依赖是另一回事,可能没那么容易。这可能涉及诸如将类从一个包移动到另一个包、重新打包模块、了解 Batik 的构建如何工作(注意它的 Ant 构建脚本有 2220 行)等任务。换句话说,这将需要您对库进行一些艰苦的工作最初只是想使用(除非您提供这些更改,否则您可能必须在以后的版本中再次应用它们)。我的建议是:在开始朝那个方向挖掘之前,请三思。
Just for your information, there is also a jdepend-maven-pluginwhich is only useful if you want to run JDepend on your project (i.e. your code) which is not your request.
仅供参考,还有一个jdepend-maven-plugin仅当您想在您的项目(即您的代码)上运行 JDepend 而不是您的要求时才有用。
回答by cetnar
Try using UCDetectorit helps find dependencies cycles in class level while developing. Another useful tool is Tattletale.
尝试使用UCDetector它有助于在开发时找到类级别的依赖循环。另一个有用的工具是Tattletale。
It provide you with reports that can help you
它为您提供可以帮助您的报告
- Identify dependencies between JAR files
- Find missing classes from the classpath
- Spot if a class is located in multiple JAR files
- Spot if the same JAR file is located in multiple locations
- With a list of what each JAR file requires and provides
- Verify the SerialVersionUID of a class
- Find similar JAR files that have different version numbers
- Find JAR files without a version number
- Locate a class in a JAR file
- Get the OSGi status of your project
- Remove black listed API usage
- 识别 JAR 文件之间的依赖关系
- 从类路径中查找缺失的类
- 发现一个类是否位于多个 JAR 文件中
- 发现同一个 JAR 文件是否位于多个位置
- 列出每个 JAR 文件需要和提供的内容
- 验证类的 SerialVersionUID
- 查找具有不同版本号的类似 JAR 文件
- 查找没有版本号的 JAR 文件
- 在 JAR 文件中定位一个类
- 获取项目的 OSGi 状态
- 移除黑名单 API 使用
I intentionally skiped maven solutions to not double other responses.
我故意跳过了 Maven 解决方案,以免其他响应加倍。

