David V
David V

Reputation: 11699

Specify different groupId for parent POM than children

I would like to create a Maven Parent POM file with a groupId of com.company.maven, which gives its children a default groupId of com.company. Is this possible? If so, how can I accomplish this?

Upvotes: 2

Views: 4760

Answers (4)

Trinimon
Trinimon

Reputation: 13967

In contradiction to the accepted anser it's in fact possible, that the parent POM has a different groupId than the child POMs. This is a useful feature in Maven that allows building a hierarchical project structure with centrally managed configurations (such as dependencies, plugins, and other common settings) while allowing individual modules or projects to maintain their own groupIds.

Example of Parent-Child Configuration in Maven

Here's a simple example to illustrate how a parent POM and child POMs can be configured with different groupIds:

Parent POM (pom.xml of the parent)

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Child POM (pom.xml of a child module)

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>com.example.child</groupId>
    <artifactId>child-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Key Points

  • Group ID: The groupId of the parent POM and the child POM can be different, which facilitates the organization of large projects where different teams or subprojects might use different naming conventions or repository structures.
  • Inheritance: Child POMs inherit settings from the parent POM, including dependencies, plugin configurations, and other specific settings defined in the dependencyManagement section.
  • Overriding: Child POMs can override certain inheritances (e.g., dependencies and plugins) to meet specific requirements.

This flexibility in configuration allows Maven to be effectively used in diverse and complex project environments.

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97477

Just create the parent pom like:

<project...>

  <groupId>com.company.maven</groupId>
  <artifactId>parent</artifactId>
  <version>..</version>
...
</project>

and define in the child pom:

<project...>
  <parent>
    <groupId>com.company.maven</groupId>
    <artifactId>parent</artifactId>
    <version>..</version>
  </parent>

  <groupId>com.company</groupId>

</project>

In other words it's possible to do so but i wouldn't do it cause i would have named the parent "com.company" whereas the child "com.company.maven". I would compare the groupId with the java package name which represent folders so your idea of setting the groupId doesn't make sense.

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114817

Like a java class, that doesn't know "its" subclasses, a parent pom does not know its child poms. So the parent pom will not be able to set or overwrite attributes of it's children.

Upvotes: 0

Duncan Jones
Duncan Jones

Reputation: 69389

I don't think this is possible. Also, it flies in the face of the Maven Guide to Naming Conventions, which recommends that (if anything), the child modules should append to the parent groupId, not vice versa.

Upvotes: 0

Related Questions