Maven: adding a reference to a parent pom project

折月煮酒 提交于 2019-11-29 03:34:46
khmarbaise

The first thing I would suggest to change your structure of the projects.

--com
   --example
      --parent
        --0.0.1-SNAPSHOT
          --parent-0.0.1-SNAPSHOT.pom
      --child1
        --0.0.1-SNAPSHOT
          --child1-0.0.1-SNAPSHOT.jar
          --child1-0.0.1-SNAPSHOT.pom
      --child2
        --0.0.1-SNAPSHOT
          --child2-0.0.1-SNAPSHOT.jar
          --child2-0.0.1-SNAPSHOT.pom

I would suggest to go the following way:

  --parent (pom.xml)
      +--child1
      +--child2

The parent can be checked in into a version control (Git, SVN) either into the master or into the trunk. Apart from that all childs have to reference the parent like this:

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>child1</artifactId>

and in your parent you can use things like this:

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

Furthermore you can use a company pom just simply by adding a parent reference to your project parent:

<parent>
    <groupId>com.example</groupId>
    <artifactId>master-pom</artifactId>
    <version>0.0.1</version>
</parent>

<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

In the company pom you can (and should) pin different plugin versions and define company defaults and suggestions for dependencies with their appropriate versions.

If you like having a company super-pom you simple create a separate maven project which contains only pom file which contains your configurations like plugins, dependencies etc. and of course check it into a version control (Git, SVN etc.). separately from the other project.

If you want to reference a jar file which means in other words an artifact of your multi-module build you have to use the correct groupId, artifactId and version. If you like to use child1 in an other project you need to give the following:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>child1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

It is important to understand that the parent of a multi module build is not a an artifact which is usually used, cause based on the packaging type pom it does not really create an artifact (jar file).

Update: You can do that in a limited way only for the dependencyManagement area with the scope import but this is only intended for using in the depdencyManagement and not for dependencies. An other solution might be to create a separate dummy project which has the dependencies too two childs as transitive deps. But this is not really beauty.

So the solution is simply to add the two or more dependencies directly which you are using in you project and that's it.

Try creating a super pom with same packaging type and add your current parent as a dependency.

As @khmarbaise pointed out, the parent of a multi-module build is not usually used as a dependency elsewhere.

You should be able to add each of your child modules as dependencies and achieve what you wanted to do by adding the parent pom.

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