Fork me on GitHub

Publishing release-specific POMs from local parent templates

A local parent can be more than a place to share static configuration. It can be a build-time template whose dependencies, imported BOMs, plugins, compiler settings, and source roots have valid defaults. Thin leaf POMs record the values for their release line, while model-critical values that differ from the defaults are also supplied to the whole Maven invocation. Different lines can select different Java and library generations while the common configuration remains in one place. Set flattenRelativePathParent to bake each selection into its installed or deployed POM.

The Maven reactor still builds the original hierarchy from disk. For publication, the plugin merges the raw local parent chain into each child and retains the first external parent. The consumer receives a conventional, self-contained POM with concrete line-specific properties and no references to the repository's build-only structure.

BUILD TIME                                  INSTALL / DEPLOY

external published parent                  external published parent
            ^                                           ^
repository parent (local template)                      |
  uses leaf version properties              published release-line artifact
            ^                               + selected Java/framework properties
module parent (local template)              + inherited dependencies and plugins
  shared dependencies/plugins
            ^
thin release-line leaf                    Maven invocation
  records Java, framework and library versions  -D selects differing model-critical values

The source hierarchy and reactor do not change. Only each published child POM is collapsed.

Three framework lines across two JDKs

A parent POM or BOM often needs parallel release lines for consumers on different framework generations. Keep the dependency and plugin declarations in one local template. Each thin leaf supplies a compatible version set: for example framework 4 on Java 21, framework 5.0 on Java 25, and framework 5.1 on Java 25. Flattening publishes the same artifact identity in disjoint version namespaces.

common-parent.xml (local template)
  imports framework-bom:${framework.version}
  configures compiler for ${java.version}
  configures framework plugin ${framework.plugin.version}
               |
       +-------+----------------+----------------+
       |                        |                |
pom.xml                    pom-v5.xml       pom-v51.xml
Java 21                    Java 25          Java 25
framework 4                framework 5.0    framework 5.1
version 1.4                version v5-1.4   version v51-1.4
       |                        |                |
       +---------------- flatten each -----------+
                                |
              three self-contained published POMs
              with concrete, mutually compatible versions

The template must resolve as an independent Maven model because Maven constructs parent projects before the flatten goal runs. Give every model-critical property a compatible default in the template. For a non-default line, pass each differing model-critical value as a Maven user property for the complete invocation. A value declared only in the leaf is too late to rebuild its parent model. This is especially important for imported BOM and plugin versions, which Maven validates while it builds the parent.

mvn -f pom-v5.xml clean verify \
  -Dframework.version=5.0.6 \
  -Dframework.plugin.version=5.0.6

Do not promote every property ending in .version. Maven user properties are global and may override an unrelated property with the same name inside an imported BOM. Promote only the model-critical values used by the source-parent chain, and only when the selected line differs from the parent's default.

One library, different JDK implementations and adapters

A library can share most of its sources while selecting a small JDK-specific implementation per line. A module-local parent owns the source-selection plugin, and each leaf chooses its compiler release and source root. A separate adapter parent can use the same pattern to select the matching framework BOM and plugin generation.

repository parent (local)
|
+-- core parent (local)
|   shared API, tests and source-selection plugin
|   |
|   +-- Java 21 leaf  -- src/main/java21 --> published core line
|   +-- Java 22+ leaf -- src/main/java22 --> published core line
|
+-- adapter parent (local)
    shared framework dependencies and plugins
    |
    +-- Java 21 / framework 4 --> published adapter line
    +-- Java 25 / framework 5 --> published adapter line

Consumers choose a coherent line; the repository keeps one shared build definition.

A multi-module compatibility matrix

Larger reactors can layer repository-wide platform configuration with module-specific parents for database, broker, HTTP, or test-container support. Release aggregators select the leaf POM for each line; every leaf carries that line's Java and BOM properties. The local parents keep shared backend dependencies, test plugins, and lifecycle setup out of the thin variants.

release aggregator A                         release aggregator B
selects */pom.xml                            selects */pom-v5.xml
for Java 21 / framework 4                   for Java 25 / framework 5
          |                                           |
          +---------------+   +-----------------------+
                          |   |
repository parent (local; imports ${framework.bom.version})
|
+-- core common parent --------> core leaf A       core leaf B
+-- database common parent ----> database leaf A   database leaf B
+-- broker common parent ------> broker leaf A     broker leaf B
+-- test common parent --------> test leaf A       test leaf B
                                  |                 |
                                  +-- flatten each -+

Published modules contain their selected platform and inherited module setup.
Build-only verification modules can still opt out of install or deploy normally.

This separates two Maven roles cleanly: aggregators choose which variants enter a reactor, while parents provide reusable configuration. flattenRelativePathParent removes the local inheritance scaffolding from the consumer-facing result without changing either role during the build.

How the local parent chain is collapsed

The following complete example shows the underlying mechanism with a repository-wide parent, a module-specific parent, and one published child.

The flattened POM keeps an empty relativePath on the retained external parent reference, so consumers resolve that parent from a repository.

Repository-wide parent

The repository-wide parent has fixed coordinates and inherits from a stable published parent. Its empty relativePath marks the end of the local chain. It also configures the flatten plugin once for every project using this layout.

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>published-parent</artifactId>
    <version>2.0.0</version>
    <relativePath/>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>repository-common-parent</artifactId>
  <version>LOCAL</version>
  <packaging>pom</packaging>

  <properties>
    <repository.property>repository-value</repository.property>
    <!-- Keeps this parent independently resolvable in Maven's reactor. -->
    <platform.version>4.1.0</platform.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.example</groupId>
        <artifactId>platform-bom</artifactId>
        <version>${platform.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <version>${project.version}</version>
        <configuration>
          <flattenMode>resolveCiFriendliesOnly</flattenMode>
          <flattenRelativePathParent>true</flattenRelativePathParent>
        </configuration>
        <executions>
          <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
              <goal>flatten</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Module-specific parent

A second repository-only parent can add configuration for one module or a group of modules. Its parent is resolved from another source-tree POM, so both local layers will be collapsed.

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>repository-common-parent</artifactId>
    <version>LOCAL</version>
    <relativePath>../common-pom.xml</relativePath>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>library-common-parent</artifactId>
  <version>LOCAL</version>
  <packaging>pom</packaging>

  <properties>
    <module.property>module-value</module.property>
  </properties>
</project>

Published child

The child points to its nearest repository-only parent explicitly. The coordinates in every local parent reference must match the POM found at its relativePath.

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>library-common-parent</artifactId>
    <version>LOCAL</version>
    <relativePath>common-pom.xml</relativePath>
  </parent>

  <artifactId>example-library</artifactId>
  <version>${revision}</version>
  <properties>
    <platform.version>4.2.0</platform.version>
  </properties>
</project>

The installed or deployed POM contains both inherited property sets and the published parent:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>published-parent</artifactId>
    <version>2.0.0</version>
    <relativePath></relativePath>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>example-library</artifactId>
  <version>1.0.0</version>
  <properties>
    <repository.property>repository-value</repository.property>
    <module.property>module-value</module.property>
    <platform.version>4.2.0</platform.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.example</groupId>
        <artifactId>platform-bom</artifactId>
        <version>${platform.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

The flattened child does not reference either repository-only parent. The flattenRelativePathParent control flag is removed when a local parent chain is collapsed. Otherwise, a published parent POM that retains its build configuration also retains the flag for downstream consumers. An empty relativePath, a missing local POM, or mismatched local coordinates ends the local chain and leaves that parent for repository resolution. Because each repository-only parent supplies valid defaults, the inherited execution can also run safely when those parents are included in the reactor.

Contributions from Maven 3 model processors are preserved in the published parent POMs. Maven 4 flattens from the source model instead, so such contributions are not preserved there.

Parent versions resolved by Maven

A core extension can resolve a parent version using properties that are absent from the source POM. For example, an extension may supply platform.parent.version=2.0.0 from a composite POM while the source parent version remains ${platform.parent.version}. With flattenRelativePathParent enabled, an expression left in the published parent version is replaced with Maven's resolved reactor parent version, so the published POM contains 2.0.0. The property need not be duplicated in the source POM or supplied with -D for this reconciliation.

This substitution requires the published parent's groupId and artifactId to match the parents in both Maven's original and effective project models. A different external parent promoted from a collapsed local chain is not replaced. Before writing the flattened POM, the plugin rejects any retained parent whose version is missing or still contains an expression, including promoted external parents. The reconciliation and validation are only enabled by flattenRelativePathParent; the requirement for independently resolvable local parents and their defaults still applies.