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.

6 Responses to Solved: Activating Maven Profiles by Environment Variable

  1. Bryce Griner says:

    Thank you so much for this blog. I was using ${env.TOMCAT_HOME} in a property file and it seemed to work just fine on my Windows machine, but not on my Mac. When I added the activiation property as you defined above, then maven properly filtered and replaced the value with the environment variable.

  2. Thank you, it works for me!!!

  3. Stephane says:

    I think your prefixing the environment variable with env. is related to your OS and not something coming from Maven in fact. On my Linux box it was in the way. I had to not prefix it.

  4. Mingjiang Shi says:

    Thanks for your post. It saved my day.

  5. Chris K says:

    Thanks!

  6. Sergey says:

    Thank you very much for the env. thing, I was hitting my head against the wall for several hours trying to make my docker profile activate when DOCKER_HOST environment property is available

Leave a Reply

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