Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I run a specific profile in Maven?

What you should do is check the profile behavior by doing the following : set activeByDefault to true in the profile configuration, run mvn help:active-profiles (to make sure it is effectively activated even without -Pdev1 ), run mvn install .


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!


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


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.