Maven2共享父节点和子节点之间的依赖关系(不重新声明子节点中的依赖关系)

Gui*_*ume 4 dependencies maven-2 dependency-management

使用maven1,我使用extend标签告诉我的孩子项目使用他们的父配置.

在父项中声明的所有依赖项都可用于扩展(子项)项目.

现在使用maven2我正在使用继承/组合功能,我必须在每个子项目中重新声明我的依赖项(减去版本号).(参见how-to-share-common-properties-among-maven-projects)

有没有办法告诉maven我想在所有孩子中分享我的一些依赖?

Pas*_*ent 13

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

不,你没有.在父pom中声明的依赖项是继承的.

有没有办法告诉maven我想在所有孩子中分享我的一些依赖?

只需<parent>在子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>
Run Code Online (Sandbox Code Playgroud)

这个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>
Run Code Online (Sandbox Code Playgroud)

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] ------------------------------------------------------------------------
...

我怀疑你是在声明依赖<dependencyManagement>部分(有其他目的).