简体   繁体   English

如何查找我是否需要排除 Maven Java 项目中的依赖项?

[英]How to find if I need to exclude dependencies in a maven java project?

I use both Intellij IDEA (2018.3.5) & Eclipse IDEs, but I prefer Intellij.我同时使用 Intellij IDEA (2018.3.5) 和 Eclipse IDE,但我更喜欢 Intellij。 I have a maven based Java project with multiple poms.我有一个基于 maven 的 Java 项目,其中包含多个 poms。 I added some dependencies to one of the pom files.我向其中一个 pom 文件添加了一些依赖项。 I need to find out if there are any dependency conflicts which could prevent the build from running when its deployed, and then exclude them.我需要找出是否有任何依赖冲突会阻止构建在部署时运行,然后排除它们。 I tried the steps given below to find conflicts which could cause problems.我尝试了下面给出的步骤来查找可能导致问题的冲突。 Are they enough or do I need to do more ?它们足够了吗,还是我需要做更多?

  • Check if there are any compile time dependency conflicts with mvn clean install -DskipTests .检查是否与mvn clean install -DskipTests存在任何编译时依赖冲突。 Build was successful with no errors.构建成功,没有错误。

  • Check if Intellij shows no problems under File > Project Structure > Problems.检查 Intellij 在文件 > 项目结构 > 问题下是否显示没有问题。 There are no problems.没有问题。

I also saw the dependency tree with mvn dependency:tree -Dverbose .我还看到了mvn dependency:tree -Dverbose It has a lot of "omitted for duplicate" and "omitted for conflict with" items, but the build was successful.它有很多“因重复而省略”和“因冲突而省略”的项目,但构建成功。 I don't see any errors though.虽然我没有看到任何错误。 Does this mean that everything is okay or do I have to do something more about these conflicts ?这是否意味着一切正常,还是我必须对这些冲突做更多的事情?

The best way to tell if everything is fine with your application is to have good tests.判断您的应用程序是否一切正常的最佳方法是进行良好的测试。

However normally one doesn't exclude transitive dependencies from project's <dependency> libraries.然而,通常不会从项目的<dependency>库中排除传递依赖项。 Doing it can potentially break the dependency in a subtle and hard to notice way.这样做可能会以一种微妙且难以察觉的方式打破依赖关系。 It's usually safer to remove the whole <dependency> .删除整个<dependency>通常更安全。

There are few scenario when one should use <exclude> :应该使用<exclude>情况很少:

  1. Dealing with incompatible transitive dependencies between different libraries eg A requires library C-1.0 but library B requires library C-2.0 while C-1.0 and C-2.0 can't coexist on the classpath.处理不同库之间不兼容的传递依赖关系,例如 A 需要库 C-1.0,而库 B 需要库 C-2.0,而 C-1.0 和 C-2.0 不能在类路径上共存。

  2. Having transitive dependencies already provided by system eg deploying to Tomcat with additional JARs in the TOMCAT_HOME/lib directory.具有系统已经提供的传递依赖项,例如部署到 Tomcat 和TOMCAT_HOME/lib目录中的附加 JAR。

If you decide to exclude a dependency it's important that you check the final artifact because sometimes plugins do weird things eg there were versions of maven-assembly-plugin affected by a bug that resulted in different dependencies being resolved during shaded JAR creation than maven-dependency-plugin used for compilation.如果您决定排除依赖项,请务必检查最终工件,因为有时插件会做一些奇怪的事情,例如, maven-assembly-plugin版本受 bug 影响,导致在着色 JAR 创建期间解决不同的依赖项而不是maven-dependency-plugin用于编译的maven-dependency-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ IDEA中的java Maven项目,如何找到不需要的依赖项? - java Maven Project in IntelliJ IDEA, How do I find unneeded dependencies? 如何找到任何给定项目的 Maven 依赖项? - How to find maven dependencies of any given project? 如何使用 Maven 将 Clojure 依赖项包含到 Java 项目中 - How to include Clojure dependencies to a Java project with Maven Maven:如何找到依赖项 - Maven: how to find dependencies 如何导出将在另一个Java项目中使用的jar以及jar使用的整个maven /外部依赖关系? - How do I export a jar that will be used in another Java project, along with the entire maven/external dependencies that the jar uses? Maven-如何从生成的JAR中排除依赖项 - Maven - How to exclude dependencies from generated JAR 如何解决在eclipse中通过maven导入的java项目中的依赖项? - How to resolve the dependencies in the java project which is imported through maven in eclipse? 如何为具有外部依赖性的Java项目配置Maven构建? - How to configure Maven build for Java project with external dependencies? 如何在java类中获取Maven项目的依赖项列表 - How to get list of dependencies of Maven project in java class Maven通过存储库定义从项目依赖项中排除插件 - maven exclude plugin from project dependencies by repository definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM