Solved: Activating Maven Profiles by Environment Variable

The maven profiles manual is confusing when it discusses activating maven profiles via environment variables. The examples do not work on Ubuntu, so it’s a tough starting point.

Given that you have set an environment variable in bash or system wide using /etc/environment.
e.g.


set RUN_MY_SPECIAL_TESTS=true

Wrong way:


<profiles>
  <profile>
    <activation>
      <property>
        <name>RUN_MY_SPECIAL_TESTS</name>
        <value>true</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

Right way:


<profiles>
  <profile>
    <activation>
      <property>
        <name>env.RUN_MY_SPECIAL_TESTS</name>
        <value>true</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

Right way, if you only care that an environment variable exists. Not what it’s value is:

<profiles>
  <profile>
    <activation>
      <property>
        <name>env.RUN_MY_SPECIAL_TESTS</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

These are your friends when configuring your profiles and env properties:
mvn help:active-profiles
mvn help:effective-pom

Note, if you activate 2 or more profiles that all try to set the same property, the last profile in the list will be the winner.

Hope this helps.

This entry was posted in Tips and tagged . Bookmark the permalink.

7 Responses to Solved: Activating Maven Profiles by Environment Variable

Leave a Reply

Your email address will not be published. Required fields are marked *