Maven transit dependency has a compilation scope when the dependency provides a scope

In my project, I have an openejb-core dependency with a scope provided . However, it has a transitive dependency on slf4j and its scope is compile (see. Screenshot). All other transitive dependencies are provided as expected.

Question : Is this a mistake or is something missing?

enter image description here

+6
source share
1 answer

In the pom example, I added:

 <dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>openejb-core</artifactId> <version>4.7.0</version> <scope>provided</scope> </dependency> </dependencies> 

Then runs:

 mvn dependency:tree -Dincludes=org.slf4j 

Output:

 [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-junit --- [INFO] com.sample:test-sample:jar:0.0.1-SNAPSHOT [INFO] \- org.apache.openejb:openejb-core:jar:4.7.0:provided [INFO] +- org.slf4j:slf4j-jdk14:jar:1.7.7:provided [INFO] \- org.slf4j:slf4j-api:jar:1.7.7:provided 

So, as you can see, Maven is consistent with the official documentation. The problem with your screenshot is probably related to your IDE.

The table is the key point in this thread: enter image description here

It can be seen that transitively in the compile or runtime goes in the provided area, what is in the provided or test is ignored.

However, if I changed my pom example to:

 <dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>openejb-core</artifactId> <version>4.7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> </dependencies> 

And re-run the dependency tree command, the output will be as follows:

 [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-junit --- [INFO] com.sample:test-sample:jar:0.0.1-SNAPSHOT [INFO] +- org.apache.openejb:openejb-core:jar:4.7.0:provided [INFO] | \- org.slf4j:slf4j-jdk14:jar:1.7.7:provided [INFO] \- org.slf4j:slf4j-api:jar:1.7.7:compile 

Look now: it comes not as a child of the provided dependency, but at the same level.

Go on.

If in my pom example, I sl4f-api dependency, but add the following to pom:

 <dependencyManagement> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> <scope>compile</scope> </dependency> </dependencies> </dependencyManagement> 

And re-run the dependency tree command again, I finally get the same as your screenshot:

 [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-junit --- [INFO] com.sample:test-sample:jar:0.0.1-SNAPSHOT [INFO] \- org.apache.openejb:openejb-core:jar:4.7.0:provided [INFO] +- org.slf4j:slf4j-jdk14:jar:1.7.7:provided [INFO] \- org.slf4j:slf4j-api:jar:1.7.7:compile 

Bingo : The dependencyManagement section redefines the scope of the transitive dependency, also affecting mediation in the provided scope. This is not a mistake, it is by design, because in this section you determine the type of control in relation to your dependencies. The diagram in this case is also correct and not misleading, since the dependency is introduced only by openejb-core , which then depends on the dependencyManagement solution to put sl4f-api in the compile area.

+10
source

All Articles