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)?
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
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
)
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!
settings.xml
file not a shared pom.xml
.
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
<file><exists>.</exists></file>
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.
-P
only deactivates <activeByDefault>
profiles. Profiles activated by <activeProfiles>
in settings.xml
or by any other type of <activation>
remain active unless explicitly deactivated.
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.
Success story sharing
firstProfile
is disabled only if you specify-DskipFirstProfile
(egmvn verify -DskipFirstProfile
).