15

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.

trebor
  • 903
  • 1
  • 12
  • 23

3 Answers3

15

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 4
    Your last two paragraphs make me think I'm not understanding how I can use a parent pom. Though I use common dependencies that are shared across all child modules in it, I really want a single dependency that I can add to other projects that will pull in undependent but related "children." I want to add a single dependency that will pull in both _child1_ and _child2_. – trebor Oct 16 '13 at 12:29
  • 1
    @trebor Parent POM isn't for that. To create a meta-project, create a regular Maven project with a name like `common-libraries` and reference all of your other projects as dependencies. Parent POM is to build several libraries in one step, and automatically use the correct build order. – Aleksandr Dubinsky Nov 18 '16 at 13:11
0

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

MikeW
  • 4,749
  • 9
  • 42
  • 83
0

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.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • That's what I'm trying to achieve with the parent pom...I want a single dependency I can add that will pull in other related but undependent artifacts. Meaning, I want both _child1_ and _child2_ in a separate project but don't want to need to define both as dependencies. – trebor Oct 16 '13 at 12:24
  • 1
    It sounds to me trying to define a kind of include file which does not exist in Maven. Every dependency you are using should be declared explicit. On the other hand the dependencyManagement part has it's intention to define dependencies with its version to be used in larger organizations as a suggestions. But if you really like to use a dependency you have to write it into the pom of your project (except the version). – khmarbaise Oct 16 '13 at 17:11