user1589188
user1589188

Reputation: 5736

Moving common groupId of modules under the same parent pom to a single place in Maven

say I have a parent project (pom only) and several modules. How do you not to repeat the same groupId inside the module poms knowing that the groupId can be inherited from their parent but I also want to add the parent artifactId to the modules' groupId end.

e.g. Inside parent pom, I have

<groupId>my.proj</groupId>
<artifactId>xyz</artifactId>
<version>1</version>

Then in each modules, right now, I need to write

<parent>
    <groupId>my.proj</groupId>
    <artifactId>xyz</artifactId>
    <version>1</version>
</parent>
<groupId>my.proj.xyz</groupId>
<artifactId>mod-a</artifactId>

for each module. But what I want is to get rid of the common groupId (best if parent can be removed as well, but I know that is not possible in current Maven), and just write

<parent>
    <groupId>my.proj</groupId>
    <artifactId>xyz</artifactId>
    <version>1</version>
</parent>
<artifactId>mod-a</artifactId>

to automatically receive a groupId of my.proj.xyz instead of just my.proj, because that makes more sense to keep the modules under the parent as oppose to keeping them side by side.

Thank you.

Upvotes: 0

Views: 734

Answers (3)

khmarbaise
khmarbaise

Reputation: 97487

Based on what you have written your structure looks like this:

   +-- groupId: my.proj artifactId: xyz
        +--- groupId: my.proj.abc artifactId: mod-a
        +--- groupId: my.proj.abc artifactId: mod-b
        +--- groupId: my.proj.abc artifactId: mod-c
        +--- groupId: my.proj.xyz artifactId: mod-a
        +--- groupId: my.proj.xyz artifactId: mod-b
        +--- groupId: my.proj.xyz artifactId: mod-c

which is an indicator for changing your structure to something like this:

   +-- groupId: my.proj artifactId: xyz
        +--- groupId: my.proj.abc artifactId:abc-parent
        !    +--- groupId: my.proj.abc artifactId: mod-a
        !    +--- groupId: my.proj.abc artifactId: mod-b
        !    +--- groupId: my.proj.abc artifactId: mod-c
        !
        +--- groupId: my.proj.xyz artifactId:xyz-parent
             +--- groupId: my.proj.xyz artifactId: mod-a
             +--- groupId: my.proj.xyz artifactId: mod-b
             +--- groupId: my.proj.xyz artifactId: mod-c

which will result in having the poms look like this:

parent pom.xml:

<groupId>my.proj</groupId>
<artifactId>xyz</artifactId>
<version>1-SNAPSHOT</version>

<modules>
  <module>abc-parent</module>
  <module>xyz-parent</module>
</modules>

your abc-parent:

<parent>
    <groupId>my.proj</groupId>
    <artifactId>xyz</artifactId>
    <version>1-SNAPSHOT</version>
</parent>

<groupId>my.proj.abc</groupId>
<artifactId>abc-root</artifactId>

your abc-mod-a:

<parent>
    <groupId>my.proj.abc</groupId>
    <artifactId>abc-root</artifactId>
    <version>1-SNAPSHOT</version>
</parent>

<artifactId>mod-a</artifactId>

your xyz-parent:

<parent>
    <groupId>my.proj</groupId>
    <artifactId>xyz</artifactId>
    <version>1-SNAPSHOT</version>
</parent>

<groupId>my.proj.xyz</groupId>
<artifactId>xyz-root</artifactId>

your xyz-mod-a:

<parent>
    <groupId>my.proj.xyz</groupId>
    <artifactId>xyz-root</artifactId>
    <version>1-SNAPSHOT</version>
</parent>

<artifactId>mod-a</artifactId>

Usually changing the groupId in every module is an indicator for having a different module structure.

Upvotes: 0

Sander Verhagen
Sander Verhagen

Reputation: 9118

It is not advised to parametrize the groupId. Also, that's not what you asked for, because you just don't want to list that entry altogether. It would require changing the way Maven works, and personally I find it unlikely that you're ever going to get your way, as the impact would be too big.

Now, you can already leave out the groupId if it's the same as the parent.

Perhaps you can change from calling your parent com.proj:xyz to com.proj.xyz:parent to fix his problem.

Upvotes: 2

reap
reap

Reputation: 202

Obviously you cannot inherit the group from parent and in same time change it. You could use the parents groupId and artifactId as property in modules groupId

<parent>
    <groupId>my.proj</groupId>
    <artifactId>xyz</artifactId>
    <version>1</version>
</parent>
<groupId>${project.parent.groupId}.${project.parent.artifactId}</groupId>
<artifactId>mod-a</artifactId>

but you still need to add the groupId-element to pom.xml. Also maven complains that groupId should not contain expression but a constant.

I would suggest naming the parent my.proj.xyz:xyz-parent so the modules can directly inherit parents groupId

Upvotes: 1

Related Questions