143

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)?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Peter
  • 1,471
  • 3
  • 11
  • 4
  • See also [Does using activeByDefault go against maven best practices?](http://stackoverflow.com/questions/16167206/does-using-activebydefault-go-against-maven-best-practices) – Vadzim Jan 23 '17 at 14:27

6 Answers6

178

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

Kariem
  • 4,398
  • 3
  • 44
  • 73
seanf
  • 6,504
  • 3
  • 42
  • 52
  • Under what circumstances is this helpful or better than explicitly mentioning the profile itself? i.e instead of passing in -DmyFlag I can do -PmyDefaultProfile right?Is there any benefit to controlling it with a flag that I'm missing? – Chetya Jun 27 '17 at 15:06
  • 4
    Because the profile is active automatically when the flag is *not* there. The profile `firstProfile` is disabled only if you specify `-DskipFirstProfile` (eg `mvn verify -DskipFirstProfile`). – seanf Jun 28 '17 at 04:08
30

I wish there was such a possibility, I have often missed it. The only relevant JIRA issue I could find is this one:

MNG-4917: Profile not active even though it has activeByDefault set to true

And it's been resolved as Not A Problem.

I've stopped using activeByDefault, because this "all or nothing" approach made it worthless for me.


The only way to change this behavior is to write your own replacement for DefaultProfileSelector, register it as a plexus component with @Component( role = ProfileSelector.class ) and put it in ${MAVEN_HOME}/lib/ext (that way it will be picked as default profile selector). (If you are using Maven 3.0.2 or older you will also have to edit ${MAVEN_HOME}/bin/m2.conf to load lib/ext before it loads lib)

Kariem
  • 4,398
  • 3
  • 44
  • 73
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • An alternative nice-to-have, would be profile-inheritance or profile-decorators, allowing base configurations to be re-used. – crowne Mar 15 '11 at 10:35
  • @crowne definitely, yes. Why not suggest that as a feature request? – Sean Patrick Floyd Mar 15 '11 at 10:40
  • This is sort of related... One thing I like to do it add all modules to an active by default profile because I thought there was no way to remove a module from execution. In 3.2.1 they added this as shown [here](http://stackoverflow.com/a/22783633/1858327). I'm leaving this comment in case someone stumbles here and is using modules for a similar reason to me. – Captain Man Aug 30 '16 at 15:49
12

This question is ancient, but it appears the problem is solvable by using activeProfile rather than activeByDefault. I'm on Maven 3.3.9, but the solution may work on earlier versions.

Simply list out your activeProfiles in your settings.xml, like so:

<settings>
  <profiles>
    [...]
  </profiles>
  <activeProfiles>
    <activeProfile>my-awesome-profile</activeProfile>
  </activeProfiles>
</settings>

In my-awesome-profile I have settings like database URLs and so on, so they always apply. Here, I activate a second profile, resolve-from-central:

$ mvn help:all-profiles -P resolve-from-central 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:all-profiles (default-cli) @ standalone-pom ---
[INFO] Listing Profiles for Project: org.apache.maven:standalone-pom:pom:1
  Profile Id: resolve-from-central (Active: true , Source: settings.xml)
  Profile Id: my-awesome-profile (Active: true , Source: settings.xml)
  Profile Id: resolve-from-internal (Active: false , Source: settings.xml)

Notice how my-awesome-profile is still active. Yay!

inanutshellus
  • 9,683
  • 9
  • 53
  • 71
  • 2
    This is not a good solution in a work environment where other developers will now get different build behaviour because their settings.xml might not contain this setting. – Geert Schuring Dec 14 '20 at 16:55
  • I believe you're confused, @GeertSchuring. This *is* a good solution in a work environment, as you're changing your personal `settings.xml` file not a shared `pom.xml`. – inanutshellus Jan 14 '21 at 15:45
  • 4
    The OP litterally asked for a solution that did not involve settings.xml... – Geert Schuring Jan 18 '21 at 14:35
  • If you can't change your pom.xml with the other solutions and can't change your personal settings.xml file then I suspect you're stuck in a "golden hammer" situation and need to reevaluate what you're trying to do. – inanutshellus Jan 19 '21 at 17:15
8

Profiles are a good way to bring some order into POM. Especially if you use multiple executions of the same plugin for different purposes.

Using files:

<profile>
    <id>alwaysActive</id>
    <activation>
         <file><exists>.</exists></file>
    </activation>
    ...
</profile>

This will always be true (unless someone deletes the directory during Maven boot :). Tested with Maven 3.6.0.

It might also be a good way to differentiate between types of projects. For instance, my project has always module.json present.

Using a profile activating extension

There are a few Maven extensions for profile activation. One of them in a fork here:
https://github.com/OndraZizka/el-profile-activator-extension

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
4

You can simply list all the profiles you want activated on the command line as such:

-P profile-1,profile-2

maven was designed to allow multiple profile activation automatically, if you however override that with the -P then only the profiles listed in the parameter are activated.

Assem
  • 59
  • 1
  • 2
  • 2
  • 7
    That's not entirely true. Manually activating profiles with `-P` only deactivates `` profiles. Profiles activated by `` in `settings.xml` or by any other type of `` remain active unless explicitly deactivated. – Sam Hanes Oct 07 '13 at 22:11
0

You can't keep the default profile active, but you can take the contents of that profile (the ... in your example) and just move it to the main section of the pom.

Just because you are using profiles, it does not mean everything you are doing needs to be within a profile.

Daniel Stolz
  • 308
  • 2
  • 4
  • 1
    This does not answer the question - OP explicitly states that the profile should be "always active unless it is explicitely deactivated". Presumably there is a need to disable it explicitly sometimes, which this answer does not allow. – sleske Aug 11 '20 at 07:31