Maven – How to keep Maven profiles which are activeByDefault active even if another profile gets activated

mavenmaven-2maven-3

I have a profile in my pom.xml which should be always active unless it is explicitely deactivated (-P !firstProfile).
I solved this by using the activeByDefault flag:

<profiles>
  <profile>
    <id>firstProfile</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

Now in the same pom.xml I have a second profile defined this should only be active if the profile is really activated (-P secondProfile).
So the default behaviour is: firstProfile active, secondProfile inactive.
At some other point I would like to activated the second profile in addition to the first profile.
Now the problem is that if I do that with "-P secondProfile" the firstProfile unfortunately gets deactivated.
The Maven documentation states this:


This profile will automatically be
active for all builds unless another
profile in the same POM is activated
using one of the previously described
methods. All profiles that are active
by default are automatically
deactivated when a profile in the POM
is activated on the command line or
through its activation config.

Is there somehow a possibility how to keep the firstProfile always active (without having to declare it in the settings.xml)?

Best Answer

One trick is to avoid activeByDefault, and instead activate the profile by the absence of a property, eg:

<profiles>
  <profile>
    <id>firstProfile</id>
    <activation>
      <property>
        <name>!skipFirstProfile</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

You should then be able to deactivate the profile with -DskipFirstProfile or with -P !firstProfile, but otherwise the profile will be active.

See: Maven: The Complete Reference, Profile Activation - Activation by the Absence of a Property

Related Topic