Search code examples
maven-2mavenprofiles

maven profile dependency


I have a maven module with 2 profile profile-a and profile-b

profile-a can be used independent but profile-b should be run with profile-a

mvn install -P profile-a                   // valid
mvn install -P profile-a,profile-b         // valid
mvn install -P profile-b                   // INVALID

is there anyway to make sure that user cannot install the module with only profile-b? or active the profile-a automatically if profile-b used alone?


Solution

  • is there anyway to make sure that user cannot install the module with only profile-b? or active the profile-a automatically if profile-b used alone?

    No, there is no way to trigger a profile from another one (not supported, see Brett's answer to a related question) nor to strictly forbid the use of a given profile.

    The best thing you can do is to use property activation and a common property to activate both profiles:

    <project>
      ...
      </dependencies>
      <profiles>
        <profile>
          <id>profile-a</id>
          <activation>
            <property>
              <name>propertyX</name>
            </property>
          </activation>
        </profile>
        <profile>
          <id>profile-b</id>
          <activation>
            <property>
              <name>propertyX</name>
            </property>
          </activation>
        </profile>
      </profiles>
    </project>
    

    And passing the property when invoking mvn would trigger both of them:

    $ mvn help:active-profiles -DpropertyX
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Q4099626 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-help-plugin:2.1.1:active-profiles (default-cli) @ Q4099626 ---
    [INFO] 
    Active Profiles for Project 'com.stackoverflow:Q4099626:jar:1.0-SNAPSHOT': 
    
    The following profiles are active:
    
     - profile-a (source: pom)
     - profile-b (source: pom)
    

    That's not ideal, but currently, that's the best you can get.

    Related questions