Search code examples
mavenjunitmaven-enforcer-plugin

Why won't Maven exclude test dependency even when in exclusion?


I have the following...

<dependency>
        <groupId>com.github.stefanbirkner</groupId>
        <artifactId>system-rules</artifactId>
        <version>[1.19.0,)</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
</dependency>
<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
</dependency>

But when I run mvn clean package I get...

Dependency convergence error for junit:junit:4.11 paths to dependency are:
+-my.pkg:project:0.0.3-SNAPSHOT
  +-com.github.stefanbirkner:system-rules:1.19.0
    +-junit:junit:4.11
and
+-my.pkg:project:0.0.3-SNAPSHOT
  +-junit:junit:4.13.2

Why is it not ignoring the dep?


Solution

  • The real way to "repair" a dependency convergence error is to use <dependencyManagement>.

    Put the entry

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>4.13.2</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
    </dependencyManagement>
    
    

    into the POM. This will set the version for all transitive dependencies. Not exclusions needed anymore.