简体   繁体   English

为什么没有“提供”Maven依赖“传递”?

[英]Why aren't 'provided' Maven dependencies 'transitive'?

Why doesn't Maven inherit provided dependencies? 为什么Maven不继承提供的依赖项?

My situation: 我的情况:
I have 2 independent projects A and B . 我有2个独立的项目AB.
I don't own project A . 我不拥有项目A.
A and B use a some of the same libraries: AB使用一些相同的库:

  • reflections-0.9.9-RC1.jar 反射-0.9.9-RC1.jar
  • guava-11.0.2.jar 番石榴11.0.2.jar
  • xml-apis-1.0.b2.jar XML的API-1.0.b2.jar
  • javassist-3.16.1-GA.jar Javassist进行-3.16.1-GA.jar
  • dom4j-1.6.1.jar dom4j的-1.6.1.jar
  • jsr305-1.3.9.jar jsr305-1.3.9.jar

I made project C , which is a plugin for project A , but also uses project B . 我制作了项目C ,它是项目A的插件,但也使用了项目B.

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

Now I want to make plugins for project C but I can't. 现在我想为项目C制作插件,但我不能。
If I create project D with a dependency to project C , 如果我创建项目D与项目C的依赖项,
it won't inherit the dependency to project A . 它不会继承项目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. 如果我将范围设置为compile但是它会将其变为项目C ,这是无用的并且会导致重复。

So now I have to add dependency to both A and B with every plugin I make. 所以现在我必须使用我制作的每个插件为AB添加依赖。

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. 此外,这些依赖项将传播到依赖项目。

Provided - This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. 提供 - 这很像compile,但表示您希望JDK或容器在运行时提供依赖性。 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. 例如,在为Java Enterprise Edition构建Web应用程序时,您可以将Servlet API和相关Java EE API的依赖关系设置为提供的范围,因为Web容器提供了这些类。 This scope is only available on the compilation and test classpath, and is not transitive. 此范围仅在编译和测试类路径中可用, 并且不可传递。

Why not? 为什么不?

There is an open bug for that exact requirement: MNG-2205 . 有一个确切要求的漏洞: 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 (!). 它目前在Maven版本3的积压中,但我不会满怀希望:它创建于2006年4月(!)。

Quoting Jason van Zyl from that bug report : 引用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. 改变现有范围的定义会有问题。

Also, quoting Andrew Williams, still from that bug report : 此外,引用安德鲁·威廉姆斯,仍然从那个错误报告

if C wants to use Sybase JConnect then it must declare this as a dependency. 如果C想要使用Sybase JConnect,则必须将其声明为依赖项。 A could at any time change it's dependencies and "break" this assumption of C's. A可以在任何时候改变它的依赖性并“打破”这种C的假设。

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. 它最初完成的原因可能围绕这样一个事实:如果你打算使用它,你应该明确地声明一个依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM