12

I have two Maven profiles profile-A and profile-B. "B" should only be activated if "A" is not activated. So if I would call

mvn install

profile-B is executed (but not profile-A). But if I would call

mvn install -Pprofile-A

then only profile-A is executed (but not profile-B).

Any hints how I need to write my pom.xml to achieve this?

I already tried this, but it doesn't work:

<profiles>
  <profile>
    <id>profile-A</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    ...
  </profile>

  <profile>
    <id>profile-B</id>
    <activation>
      <activeByDefault>true</activeByDefault>
      <property>
        <name>!profile-A</name>
      </property>       
      ...
    </activation>
    ...
  </profile>
</profiles>
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Peter Meier
  • 483
  • 3
  • 7
  • 17

1 Answers1

12

I think for your example command line to work as expected, all you need is the <activeByDefault>true</activeByDefault> for profile B.

http://maven.apache.org/guides/introduction/introduction-to-profiles.html states:

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.

ddso
  • 1,731
  • 12
  • 15
  • 1
    Just wanted to clarify a point that stumped me for about 15 minutes. From the latest version of the above link regarding `activeByDefault` (emphasis mine): "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." In my case, I had added profile B to the parent POM and profile A to a leaf POM in a multi-module build. In this configuration, profile B was not disabled when profile A was activated via the command line. Moving profiles A and B to the same POM produced the desired behavior. – Rusty Shackleford Dec 10 '15 at 19:06