Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get maven to warn about transative dependency version mismatches?

Tags:

maven

maven-3

In the example Maven dependencies below, the slf4j dependency wants to pull in log4j 1.2.17 and the log4j explicit dependency wants to pull in 1.2.15. Maven resolves log4j to version 1.2.15 however, there are no warnings that Maven prints out that sl4j wants a later version of log4j.

How can I get Maven to warn about these types of conflicts, rather than just silently take the 1.2.15 version?

<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>
like image 494
ams Avatar asked Oct 21 '12 05:10

ams


1 Answers

In short, Maven-enforcer-plugin should be used to handle this.

You'd just need to configure the enforcer plugin like

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

In more detail, as told in the documentation page, something like this that has a transative dependency mismatch:

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

will silently "work" without the enforcer rule, but with the rule set up, it'll fail the build with

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

thus, when user gets the error message about the failed build, she can fix it by doing an exclusion, like

<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>
like image 170
eis Avatar answered Oct 13 '22 22:10

eis