问题描述
在我的项目中,我有 openejb-core
依赖,范围提供
。但是它具有 slf4j
的传递依赖性,其范围是 compile
(参见屏幕截图)。所有其他传递依赖项都按预期提供。
In my project I have openejb-core
dependency with scope provided
. However it has transitive dependency of slf4j
and its scope is compile
(see screenshot). All other transitive dependencies are provided as expected.
问题:是错误还是我错过了什么?
Question: Is it bug or am I missing something?
推荐答案
在我添加的示例pom中:
In a sample pom I added:
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-core</artifactId>
<version>4.7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
然后运行:
mvn dependency:tree -Dincludes=org.slf4j
输出为:
[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
因此,您可以看到Maven与其。屏幕截图中的问题可能在您的IDE上。
So as you can see Maven is coherent with its official documentation. The issue from your screenshot is probably on your IDE.
The table is the key point on this topic:
从中我们可以看到范围编译
或运行时的传递内容
进入范围,范围提供
或 test
将被忽略。
From it, we can see that what is transitively in scope compile
or runtime
goes in scope provided, what is in scope provided
or test
is ignored.
但是,如果我将我的样本pom更改为:
However, if I change my sample pom 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 is as following:
[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.
让我们继续。
如果在我的样本pom上我删除 sl4f-api
依赖但我向pom添加以下内容:
If on my sample pom I remove the sl4f-api
dependency but I add to the pom the following:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
重新运行依赖树命令,我终于得到了截图: / p>
And re-re-run the dependency tree command, 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 : dependencyManagement
section覆盖了传递依赖的范围,也影响了提供的
范围的中介。这不是一个错误,它是设计的,因为在本节中,您定义了有关您的依赖关系的治理类型。这种情况下的图表也是正确的,没有误导性,因为依赖性仅由 openejb-core
引入,然后受 dependencyManagement的影响
决定将 sl4f-api
放入范围编译
。
Bingo: the dependencyManagement
section is overriding the scope of the transitive dependency, affecting also the mediation on provided
scope. This is not a bug, it's by design as in this section you define kind of governance concerning your dependencies. The diagram in this case is also correct and not misleading, since the dependency is only introduced by openejb-core
which is then affected by the dependencyManagement
decision to put sl4f-api
in scope compile
.
这篇关于当依赖提供范围时,Maven传递依赖具有范围编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!