Conditional maven modules

This is a little something that’s now caught me out a few times. Quite often, I’ll have a project with a parent POM, and a module that I only care about in certain circumstances. So I naturally do this.

  <!-- foo/pom.xml -->
  <project>
    <modules>
      <module>foo-core</module>
      <module>foo-webapp</module>
    </modules>
    <profiles>
      <profile>
        <id>dist</id>
        <modules>
          <module>foo-dist</module>
        </modules>
      </profile>
    </profiles>
  </project>

Unfortunately, this can interact badly with the release plugin. Unless that profile is activated during release, the module won’t have it’s version updated. So the next time you use that profile, it’ll break.

The correct solution is to “push down” the profile into the foo-dist module. i.e.

  <!-- foo/pom.xml -->
  <project>
    <groupId>foo</groupId>
    <artifactId>foo-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
      <module>foo-core</module>
      <module>foo-webapp</module>
      <module>foo-dist</module>
    </modules>
  </project>
 
  <!-- foo/foo-dist/pom.xml -->
  <project>
    <artifactId>foo-dist</artifactId>
    <parent>
      <groupId>foo</groupId>
      <artifactId>foo-parent</artifactId>
      <version>1.0-SNAPSHOT</version>
    </parent>
    <profiles>
      <profile>
        <id>dist</id>
        <!-- add dependencies & plugins here -->
      </profile>
    </profiles>
  </project>

That way, foo-dist is always available, but is a noop unless the profile is activated. But it does ensure that the release plugin (and also the versions plugin) know that it’s there.

Comments are Closed