How to break a maven build when there is a dependency conflict?

后端 未结 2 871
無奈伤痛
無奈伤痛 2020-12-03 01:28

I ran mvn dependency:tree for a project and I saw output like the following:

[INFO] my:project:jar:1.0.0-SNAPSHOT
[INFO] +- some.other:library:jar:2.0.0:comp         


        
相关标签:
2条回答
  • 2020-12-03 02:09

    The maven-enforcer-plugin has a dependencyConvergence configuration which does what I want. Coincidentally, the example from the documentation uses slf4j.

    Configure it like this:

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

    This combination of dependencies will cause a build to fail:

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

    With this being logged during compilation:

    [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
    
    0 讨论(0)
  • 2020-12-03 02:27

    Though dated, I think this SO discussion is related.

    0 讨论(0)
提交回复
热议问题