我有一个相当大的遗留项目,我正在向其中添加一个组件。该组件使用 HtmlUnit 。我可以用 Maven 编译它,但是当我运行它时,我得到:
java.lang.NoSuchMethodError:
org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>
(Ljavax/net/ssl/SSLContext;[Ljava/lang/String;[Ljava/lang/String;Ljavax/net/ssl/HostnameVerifier;)
所以它缺少正确的构造函数。我认为这几乎肯定是 httpclient
中的版本冲突,但我不确定如何解决它。这是我的 pom.xml
的相关部分(请注意我尝试使用排除和依赖项管理玩的所有游戏):
<dependencies>
<dependency>
<groupId>com.mycompany.mine</groupId>
<artifactId>my-base-project</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>base-project</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
</dependencyManagement>
有什么想法吗?
编辑:有人建议此问题与 this one 重复。 ,但事实并非如此,因为本例中的依赖类型不是 war
。
最佳答案
为了识别冲突的依赖项,请使用mvn dependency:tree
。我喜欢将其通过管道传输到文本文件以便于使用:
mvn dependency:tree > tree.txt
然后,使用您最喜欢的文本编辑器来查找依赖项的多个版本。
或者,如果您正在查找特定的 groupId 或 artifactId,请使用 -Dincludes
标志:
mvn dependency:tree -Dincludes=<groupId>:<artifactId>:<version>:<packaging>
mvn dependency:tree -Dincludes=org.springframework <<< get all dependencies with by groupId
mvn dependency:tree -Dincludes=:spring-web <<< get all dependencies by artifactId
您可能还想在此处添加 -Dverbose
标志。
要解决依赖冲突,有两种方法:
1) 排除您不想要的
<depdency>
<groupId>some.stuff</groupId>
<artifactId>with.transitive.depdency</artifactId>
<exclusions>
<exclusion>
<groupId>something</groupId>
<artifactId>unwanted</artifactId>
<exclusion>
<exclusions>
<depdency>
通过这种方式,您将必须排除所有引入传递依赖项的依赖项。因此我更喜欢另一个。
2)明确添加您想要的版本
<dependency>
<groupId>something</groupId>
<artifactId>with.version.conflict</artifactId>
<version>what I want</version>
</dependency>
这将确保任何传递依赖项都将与此确切版本进行交换。如果某些框架实际上需要旧版本,这也可能会导致错误。为了安全地使用此策略,您的依赖项需要非常接近最新的可用版本(或同时发布的版本)。
关于java - 如何解决 Maven 中的依赖冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37594922/