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

前端 未结 6 1805
悲哀的现实
悲哀的现实 2020-11-30 21:15

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:

<
相关标签:
6条回答
  • 2020-11-30 21:23

    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

    0 讨论(0)
  • 2020-11-30 21:26

    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.

    0 讨论(0)
  • 2020-11-30 21:33

    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!

    0 讨论(0)
  • 2020-11-30 21:35

    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)

    0 讨论(0)
  • 2020-11-30 21:40

    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

    0 讨论(0)
  • 2020-11-30 21:42

    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.

    0 讨论(0)
提交回复
热议问题