Maven Tip: Disabling Profiles when a Property Does Not Exist

February 09, 2011

I wanted to share a method for enabling a Maven Build Profile in the absense of a system property.  I did not see this approach documented in the current version of the Maven Build Profile documentation, so I thought I would share it.

I find this approach particularly useful for turning off build features whenever you are disabling tests.  In Maven, there are two properties that will skip the execution of all tests, but I prefer the "skipTests" property.

As an example, let's take a Maven POM file that runs Liquibase to update the database.   If you want to run a Maven install without executing the tests, then you would run the following command (Note: I am not advocating that tests be skipped, but I think this is a useful example):

mvn clean install -DskipTests

If I wanted to also disable Liquibase whenever the tests were skipped, I would configure Liquibase is a profile.  This profile would be triggered in absence of the  the "skipTests" property.  This means that when the property is not declared, Liquibase will run.  Here is how you would declare this profile:

<profiles>
   <profile>
      <id>update-database</id>
      <activation>
         <property>
            <name>!skipTests</name>
         </property>
      </activation>
      ...
      <!-- Liquibase Plugin Configuration Here -->
      ...
   </profile>
</profiles>

An alternative to this approach is to turn off the profile explicitly:

mvn clean install -DskipTests -P !update-database

Using this approach does make it clear what you are trying to do, which is also an advantage.  There is an advantage in turning features on and off based on a related trigger, like a single system property.

In regards to the Liquibase Maven plugin, a feature was added to allow property based skipping.  This is only available as of Liquibase 2.0.

Share this:


comments powered by Disqus