Maven2 在父级和子级之间共享依赖关系(无需在子级中重新声明依赖关系)

发布于 2024-09-08 11:08:28 字数 370 浏览 6 评论 0原文

在 maven1 中,我使用 extend 标签来告诉我的子项目使用其父配置。

父级中声明的所有依赖项均可在扩展(子级)项目中使用。

现在,在 maven2 中,我使用继承/组合功能,并且必须在每个子项目中重新声明我的依赖项(减去版本号)。 (请参阅如何共享-common-properties-among-几个-maven-projects

有没有办法告诉maven我想在我的所有孩子之间共享一些依赖项?

With maven1 I was using the extend tag to tell my children project to use their parent configuration.

All dependencies declared in the parent were available in extending (children) projects.

Now with maven2 I'm using the inheritance/composition feature and I have to redeclare my dependencies (minus the version number) in every child project.
(see how-to-share-common-properties-among-several-maven-projects)

Is there a way to tell maven that I want to share some of my dependencies among all my children ?

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

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

发布评论

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

评论(1

如果没结果 2024-09-15 11:08:28

现在,在 maven2 中,我使用继承/组合功能,并且必须在每个子项目中重新声明我的依赖项(减去版本号)

不,你不需要。父 pom 中声明的依赖项会被继承。

有没有办法告诉 Maven 我想在我的所有孩子之间共享一些依赖项?

只需在子 POM 中声明 元素即可。例如,使用此父 POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.group.id</groupId>
  <artifactId>parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Demo - Parent</name>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
    <module>child</module>
  </modules>
</project>

以及子模块的 POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>my.group.id</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <name>Demo - Child</name>
  <artifactId>child</artifactId>
  <packaging>jar</packaging>
</project>

junit 依赖项按预期继承:

$ mvn dependency:tree 
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Demo - Child
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] my.group.id:child:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

我怀疑您在 部分(还有另一个目的)。

Now with maven2 I'm using the inheritance/composition feature and I have to redeclare my dependencies (minus the version number) in every child project

No, you don't. Dependencies declared in the parent pom are inherited.

Is there a way to tell maven that I want to share some of my dependencies among all my children ?

Just declare the <parent> element in child POMs. For example, with this parent POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.group.id</groupId>
  <artifactId>parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Demo - Parent</name>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
    <module>child</module>
  </modules>
</project>

And this POM for the child module:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>my.group.id</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <name>Demo - Child</name>
  <artifactId>child</artifactId>
  <packaging>jar</packaging>
</project>

The junit dependency gets inherited as expected:

$ mvn dependency:tree 
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Demo - Child
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] my.group.id:child:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

I suspect that you are declaring dependencies in the <dependencyManagement> section (which has another purpose).

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