How to keep Maven profiles that are active ByDefault active even if another profile is activated?

I have a profile in my pom.xml that should always be active if it is not explicitly deactivated (-P! FirstProfile). I solved this 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 that should be active if the profile is really activated (-P secondProfile). Thus, the default behavior is: firstProfile active, secondProfile is inactive. At some other point, I would like to activate a second profile in addition to the first profile. Now the problem is that if I do this with "-P secondProfile", the first file is unfortunately deactivated. The Maven documentation says this:

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

Is there any way to keep the first file always active (without declaring it in the settings.xml file)?

+96
maven maven-2 maven-3
Mar 15 2018-11-11T00:
source share
6 answers

One trick is to avoid activeByDefault and instead activate the profile due to the lack of a property, for example:

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

You can then disable the profile using -DskipFirstProfile or using -P !firstProfile , but otherwise the profile will be active.

See: Maven: full link, profile activation - activation by property absence

+136
Sep 20 2018-11-11T00:
source share

I would like this opportunity, I often missed it. The only current JIRA issue I could find was:

MNG-4917: Profile is inactive, although it has activeByDefault set to true

And it was resolved as Not A Problem .

I stopped using activeByDefault because this all-or-nothing approach made it useless to me.




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

+21
Mar 15 '11 at 9:11
source share

This question is ancient, but it seems the problem is solvable using activeProfile , not activeByDefault . I am on Maven 3.3.9, but the solution may work in earlier versions.

Just enter activeProfiles in your settings.xml , for example:

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

In my-awesome-profile I have settings, such as database URLs, etc., so they always apply. Here I activate the 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) 

Note that my-awesome-profile is still active. Hurrah!

+5
Sep 15 '16 at 14:02
source share

You can simply list all the profiles that you want to activate on the command line:

-P profile-1, profile-2

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

+3
Jun 09 2018-11-11T00:
source share

Profiles are a good way to clean up your POM. Especially if you use several versions of the same plugin for different purposes.

Use of files:

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

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

It can also be a good way to distinguish between types of projects. For example, module.json always present in my project.

Using an extension to activate a profile

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

0
Jul 22 '19 at 7:56
source share

You cannot keep the default profile active, but you can take the contents of this profile (... in your example) and simply transfer it to the main part of pom.

Just because you use profiles does not mean that everything you do should be within the profile.

-one
May 10 '16 at 12:43
source share



All Articles