为什么“提供”Maven依赖关系是“传递性的”? [英] Why aren't 'provided' Maven dependencies 'transitive'?

查看:147
本文介绍了为什么“提供”Maven依赖关系是“传递性的”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况:

我有2个独立项目 A B

我不拥有项目 A

A B 使用一些相同的库:

My situation:
I have 2 independent projects A and B.
I don't own project A.
A and B use a some of the same libraries:


  • reflections-0.9.9-RC1 .jar

  • guava-11.0.2.jar

  • xml-apis-1.0.b2.jar

  • javassist-3.16.1-GA.jar

  • dom4j-1.6.1.jar

  • jsr305-1.3.9.jar

  • reflections-0.9.9-RC1.jar
  • guava-11.0.2.jar
  • xml-apis-1.0.b2.jar
  • javassist-3.16.1-GA.jar
  • dom4j-1.6.1.jar
  • jsr305-1.3.9.jar

我制作了项目 C ,这是项目 A 的插件,但也使用项目的即可。

I made project C, which is a plugin for project A, but also uses project B.

项目 C pom.xml:

Project C pom.xml:

<dependencies>
    <dependency>
        <groupId>com.a</groupId>
        <artifactId>a</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.b</groupId>
        <artifactId>b</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

现在我想为项目 C 做插件,但是我不能。

如果我创建具有依赖项目 C 的项目 C

它将不会继承对项目的依赖 A

如果我将范围设置为 compile ,那么将会将其隐藏到项目 C 这不是有用的,会导致重复。

Now I want to make plugins for project C but I can't.
If I create project D with a dependency to project C,
it won't inherit the dependency to project A.
It will if I set the scope to compile but that would shade it into project C which is not useful and would cause duplicates.

所以现在我必须添加依赖关系 A B 与我所做的每个插件。

So now I have to add dependency to both A and B with every plugin I make.


编译 - 这是默认范围,如果没有指定,则使用。编译依赖项可在项目的所有类路径中使用。此外,这些依赖关系传播到依赖项目。

Compile -This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

提供 - 这很像编译,但表示您期望JDK或容器在运行时提供依赖关系。例如,当为Java Enterprise Edition构建Web应用程序时,您将设置对Servlet API和相关Java EE API的依赖关系,因为Web容器提供了这些类。此范围仅适用于编译和测试类路径,并且不可传递。

Provided - This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

为什么不这样做?

推荐答案

该确切要求有一个开放的错误: MNG-2205 。目前正处于Maven版本3的积压状态,但我不会得到您的希望:它是在2006年4月创建的(!)。

There is an open bug for that exact requirement: MNG-2205. It is currently in the backlog for version 3 of Maven but I wouldn't get your hopes up: it was created in April 2006 (!).

引用 Jason van Zyl从该错误报告


我们不太可能改变提供范围的行为,但是如果我们真的想要这样做,就可以创建一个新的提供的传递。更改现有范围的定义将是有问题的。

It is unlikely we will change the behavior of the provided scope, but it would be possible to create a new 'provided-transitive' if we really wanted this. Changing the definition of existing scopes would be problematic.

另外引用安德鲁·威廉姆斯,仍然从该错误报告


如果C想使用Sybase JConnect,那么它必须将其声明为依赖关系。 A可以在任何时候改变它的依赖关系,并打破C的这个假设。

if C wants to use Sybase JConnect then it must declare this as a dependency. A could at any time change it's dependencies and "break" this assumption of C's.

使用不声明的依赖项是错误的。

It is wrong to use a dependency that you do not declare.

这个问题没有更好的答案:文档在这个主题上是非常清楚的:提供的依赖关系当前不是传递的。最初这样做的原因可能在于如果您打算使用它,您应该明确声明依赖关系。

There is no better answer to this question: the documentation is quite clear on the subject: provided dependencies are not currently transitive. The reason it was initially done this probably revolves around the fact that you should explicitely declare a dependency if you intend to use it.

这篇关于为什么“提供”Maven依赖关系是“传递性的”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆