2012-10-21 88 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