Maven2 sharing dependencies across parent and children (without redeclaring dependencies in children)

橙三吉。 提交于 2020-01-01 12:16:34

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/3211643/maven2-sharing-dependencies-across-parent-and-children-without-redeclaring-depe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!