Cra*_*lin 18 maven-2 dependency-management maven-3 maven
我为项目运行了mvn依赖:tree,我看到如下输出:
[INFO] my:project:jar:1.0.0-SNAPSHOT
[INFO] +- some.other:library:jar:2.0.0:compile
[INFO] | \- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.0:compile
[INFO] | \- (org.slf4j:slf4j-api:jar:1.6.0:compile - omitted for conflict with 1.6.1)
Run Code Online (Sandbox Code Playgroud)
这是一个糟糕的状态,因为我的项目直接依赖于slf4j 1.6.0,而我们依赖的某些库依赖于slf4j 1.6.1.这两个版本恰好是二进制兼容的,因此构建过程没有任何警告.有没有办法让Maven对其依赖项解析更加严格,以便我可以配置一个在这种情况下会失败的新构建?在这种情况下,解决方案是将我们的依赖关系更新为更新版本的slf4j.
Cra*_*lin 22
maven-enforcer-plugin有一个dependencyConvergence配置,可以满足我的需求.巧合的是,文档中的示例使用了slf4j.
像这样配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<DependencyConvergence />
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
这种依赖关系的组合将导致构建失败:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
在编译期间记录此内容:
[ERROR]
Dependency convergence error for org.slf4j:slf4j-api:1.6.1 paths to dependency are:
+-org.myorg:my-project:1.0.0-SNAPSHOT
+-org.slf4j:slf4j-jdk14:1.6.1
+-org.slf4j:slf4j-api:1.6.1
and
+-org.myorg:my-project:1.0.0-SNAPSHOT
+-org.slf4j:slf4j-nop:1.6.0
+-org.slf4j:slf4j-api:1.6.0
Run Code Online (Sandbox Code Playgroud)