Search code examples
mavenpom.xmlparent-pom

Can parent poms receive properties from children?


Can something in the parent pom take a value declared in the child pom?

Specifically I want to do something like this.

Parent:

<scm>
    <connection>scm:git:[email protected]:team/${git.repo}.git</connection>
    <developerConnection>scm:git:[email protected]:team/${git.repo}.git</developerConnection>
    <url>http://github.corp.com/team/${git.repo}</url>
</scm>

Child:

<properties>
    <git.repo>Foo</git.repo>
</properties>

Ideally the git.repo property is explicitly not set in the parent, such that the child must override either it, or override the scm section fully.


Solution

  • Can something in the parent pom take a value declared in the child pom?

    Yes. In order to have a valid parent POM, however, I suggest providing a default/dummy value for ${git.repo} in the parent.

    To deal with cases where a child POM does not override (i.e. redefine) ${git.repo} you could introduce a profile in the parent like so:

    <profiles>
      <profile>
        <id>property-not-redefined</id>
        <activation>
          <activeByDefault>false</activeByDefault>
          <property>
            <name>git.repo</name>
            <value>your-default-value</value>
          </property>
        </activation>
        <build>
          <plugins>
            <!-- do something here -->
          </plugins>
        </build>
      </profile>
    </profiles>
    

    This profile becomes active when the git.repo property still has the default value.