Combine Maven profile properties

I have fought with this problem a couple of times, but now I found a way out.

Let’s say you have a Maven POM and, depending on the active profiles, you want to set a property, like:

<profiles>
  <profile>
    <id>ci</id>
    <properties>
      <spring.profiles.active>something</spring.profiles.active>
    </properties>
  </profile>
  <profile>
    <id>dev</id>
    <properties>
      <spring.profiles.active>another</spring.profiles.active>
    </properties>
   </profile>
</profiles>

You should know by now that using mvn package -Pdev,ci is not going to produce the outcome you would expect: the spring.profiles.active Maven property will have a value of something, the last one of the activated profiles.

You actually have no straightforward way to concatenate properties in Maven, but the following will do:

<plugin>
  <groupId>org.codehaus.gmavenplus</groupId>
  <artifactId>gmavenplus-plugin</artifactId>
  <version>1.6</version>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.9</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <id>excludedGroups</id>
      <phase>initialize</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <scripts>
          <script><![CDATA[             def value = ""             (project.activeProfiles).each{ profile -> value += profile.properties.springProfiles + "," }
            project.properties.springProfiles = value.substring(0, value.length() - 1)
          ]]></script>
        </scripts>
      </configuration>
    </execution>
  </executions>
</plugin>

The plugin, activated at the very beginning of POM processing, will create a property springProfiles with a value resulting by concatenating the properties defined in the active profiles.

The same configuration, with a slight variation, is also usable to concatenate JUnit categories:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <excludedGroups>${excludedGroups}</excludedGroups>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.gmavenplus</groupId>
      <artifactId>gmavenplus-plugin</artifactId>
      <version>1.6</version>
      <dependencies>
        <dependency>
          <groupId>org.codehaus.groovy</groupId>
          <artifactId>groovy-all</artifactId>
          <version>2.4.9</version>
          <scope>runtime</scope>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <id>excludedGroups</id>
          <phase>initialize</phase>
          <goals>
            <goal>execute</goal>
          </goals>
          <configuration>
            <scripts>
              <script><![CDATA[                 def value = ""                 (project.activeProfiles).each{ profile -> value += profile.properties.excludedGroups + "," }
                project.properties.excludedGroups = value.substring(0, value.length() - 1)
              ]]></script>
            </scripts>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

<profiles>
  <!-- Exclude any tests in `SlowTest` category when profile 'skipSlow' is specified. -->
  <profile>
    <id>skipSlow</id>
    <properties>
      <excludedGroups>SlowTest.class</activeByDefault>
    </properties>
  </profile>

  <!-- Skip any tests in 'WindowsTest' category when not running on Windows. -->
  <profile>
    <id>skipWindowsTests</id>
    <activation>
      <os><family>!windows</family></os>
    </activation>
    <properties>
      <excludedGroups>WindowsTest.class</excludedGroups>
    </properties>
  </profile>
</profiles>

Launch the above as mvn test -PskipSlow on a non-Windows machine to have both SlowTest.class and WindowsTest.class JUnit categories excluded.

Happy concatenation!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s