ChatGPT解决这个技术问题 Extra ChatGPT

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

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


K
Kariem

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


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?
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).
K
Kariem

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)


An alternative nice-to-have, would be profile-inheritance or profile-decorators, allowing base configurations to be re-used.
@crowne definitely, yes. Why not suggest that as a feature request?
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. I'm leaving this comment in case someone stumbles here and is using modules for a similar reason to me.
i
inanutshellus

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!


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.
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.
The OP litterally asked for a solution that did not involve settings.xml...
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.
O
Ondra Žižka

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


small correction it seems to be <file><exists>.</exists></file>
A
Assem

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.


That's not entirely true. Manually activating profiles with -P only deactivates <activeByDefault> profiles. Profiles activated by <activeProfiles> in settings.xml or by any other type of <activation> remain active unless explicitly deactivated.
D
Daniel Stolz

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.


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.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now