Mar 12, 2012

Maven Profile -conditionally exclude junits


Our dataaccess layer is dependent on special software components that are not available in every developer’s sandbox. By default we exclude these tests, but we still need our continuous integration environment (Bamboo) to run all the tests.  
We were able to leverage Maven’s Profile to conditionally exclude tests.

We created a maven property in our pom: exclude.tim.tests

<exclude.tim.tests>%regex[com.somecompany.dataaccess.usermanager.dao.*Test.*]</exclude.tim.tests>



We used this property in exclude configuration.
<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.12</version>
       <configuration>
              <excludes>
                     <exclude>${exclude.tim.tests}</exclude>
              </excludes>
       </configuration>
</plugin>

Running, mvn clean install excludes these tests.

To include the tests, we created a profile IncludeSpecialTests and configured the test exclusion property with a dummy value (NothingToExclude)
<profiles>
  <profile>
    <id>IncludeSpecialTests </id>
    <properties>
      <exclude.tim.tests>NothingToExclude</exclude.tim.tests>
    </properties>
  </profile>
</profiles>

Running mvn install  runs all the tests.
mvn clean install –PIncludeSpecialTests