Can maven projects have multiple parents?

Even though maven projects have single parent, they can import any number of other pom’s like this:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>my-shared-dependencies</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

This has two important differences compared to a parent:

  1. Plugins defined in the imported pom won’t be imported
  2. Dependencies defined in the imported pom won’t be added to the current pom, it will only import dependencies into the dependency management section

However, if your parent pom has a <dependencies> section and you want to include those into your dependencies, then you can add the parent to your <dependencies> section just like a regular dependency:

<dependency>
    <groupId>org.example</groupId>
    <artifactId>my-shared-dependencies</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Even though the same dependency is already imported, the version tag has to be specified again. To reduce duplication, it can be stored in a property

Leave a Comment