2012-10-21 84 views
5

在下面的Maven依赖关系示例中,slf4j依赖关系想要引入log4j 1.2.17,并且log4j显式依赖关系想要引入1.2.15。 Maven将log4j解析为版本1.2.15,但是,没有警告Maven会打印出sl4j需要更高版本的log4j。如何让maven警告翻译依赖版本不匹配?

我怎样才能让Maven警告这些类型的冲突,而不是仅仅默默地采取 1.2.15版本?

<dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-log4j12</artifactId> 
    <version>1.7.2</version> 
</dependency> 
<dependency> 
    <groupId>log4j</groupId> 
    <artifactId>log4j</artifactId> 
    <version>1.2.15</version> 
</dependency> 

回答

9

总之,应该用Maven-enforcer-plugin来解决这个问题。

你只需要配置Enforcer插件像

<project> 
    ... 
    <build> 
    <plugins> 
     ... 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-enforcer-plugin</artifactId> 
     <version>1.1.1</version> 
     <executions> 
      <execution> 
      <id>enforce</id> 
      <configuration> 
       <rules> 
       <DependencyConvergence/> 
       </rules> 
      </configuration> 
      <goals> 
       <goal>enforce</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
     ... 
    </plugins> 
    </build> 
    ... 
</project> 

更详细,如documentation page告诉记者,这样的事情,有一个transative依赖不匹配:

<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> 

会在没有执行者规则的情况下默默“工作”,但是随着规则的设置,它将通过

Dependency convergence error for org.slf4j:slf4j-api1.6.1 paths to dependency are: 

[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 

因此,当用户得到关于构建失败的错误消息,她可以做一个排除修复它,就像

<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> 
    <exclusions> 
    <exclusion> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
    </exclusion> 
    </exclusions> 
</dependency> 
+0

这工作得很好,谢谢。 – ams