Maven - 依赖继承 - 提供

发布于 2024-09-16 04:39:44 字数 167 浏览 3 评论 0原文

我使用依赖项 POM,然后将其作为依赖项包含到另一个项目中。我遇到的问题是,当它聚合 POM 与这些依赖项时,当我声明范围的依赖项时,它就会出现,前提是这些依赖项不包括在内。

是否可以在依赖项 POM 中包含提供的依赖项,其范围为提供的?我经常声明我需要什么 API,然后将实现作为运行时依赖项包含在内。

I make use of dependency POMs which I will then go and include into another projects as as dependency. The problem I am having is while it aggregates the POM with those dependencies, it appears when I declare dependencies of scope, provided, those aren't included.

Is it possible to include provided dependencies in dependency POMs with a scope of provided? I often declare what APIs I need and then include the implementation as a runtime dependency.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-09-23 04:39:44

如果提供了依赖项,为什么不能以相同的范围继承该依赖项,这样我就不必声明它?

以相同的范围继承。给定以下父级 pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.Q3597684</groupId>
  <artifactId>root</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Q3597684 - Root</name>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

以及以下从根工件继承的 pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>root</artifactId>
    <groupId>com.stackoverflow.Q3597684</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>child</artifactId>
  <packaging>war</packaging>
  <name>Q3597684 - Child</name>
  <dependencies/>
</project>

从子级运行 mvn dependency:tree给出以下输出:

$ mvn dependency:tree[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3597684 - Child
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] com.stackoverflow.Q3597684:child:war:1.0-SNAPSHOT
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

正如预期的那样,提供的 servlet-api 就在那里。

您可能(错误)使用了 dependencyManagement 部分吗?

If a dependency is provided why can't that dependency be inherited with the same scope so I don't have to declare it?

It is inherited with the same scope. Given the following parent pom.xml:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow.Q3597684</groupId>
  <artifactId>root</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Q3597684 - Root</name>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

And the following pom.xml that inherits from the root artifact:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>root</artifactId>
    <groupId>com.stackoverflow.Q3597684</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>child</artifactId>
  <packaging>war</packaging>
  <name>Q3597684 - Child</name>
  <dependencies/>
</project>

Running mvn dependency:tree from the child gives the following output:

$ mvn dependency:tree[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3597684 - Child
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] com.stackoverflow.Q3597684:child:war:1.0-SNAPSHOT
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

The provided servlet-api is there, as expected.

Are you maybe (mis)using the dependencyManagement section?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文