Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: adding a reference to a parent pom project

I'm not sure if I'm understanding how to use a parent pom project correctly. I have the following parent pom defined:

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

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

And then the children pom reference the parent (each child has their own set of dependencies which are not shown):

<parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../Parent/pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>child1</artifactId>

This setup works fine and resolves correctly in eclipse (m2eclipse). I can deploy these to my local repository and end up with the following structure, which should be correct:

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

My issue is that I now want to reference the parent project in a different project (not parent, child1, or child2) and thus pull in all of the parent's children. I can add a reference to it in my other project:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
</dependencies>

When doing this the project shows no errors in eclipse but no artifacts are being added to my classpath: not child1, child2, or any of their dependencies.

I keep thinking there must be a way to have a "master" pom project which isn't a jar in itself but has only references to other jars and then be able to reference that "master" somewhere, but I cannot find out how this is accomplished.

like image 445
trebor Avatar asked Oct 16 '13 05:10

trebor


1 Answers

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.

like image 52
khmarbaise Avatar answered Oct 22 '22 17:10

khmarbaise