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?
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.
Put this in the profile-b. At least you prevent then bad builds and you inform the user. I did not test it, but it should work. If there is some typo, please correct it:
<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.gmaven</groupId>
      <artifactId>gmaven-plugin</artifactId>
      <executions>
        <execution>
          <id>check-profile-combinations</id>
          <phase>validate</phase>
          <goals>
            <goal>execute</goal>
          </goals>
          <configuration>
            <source>
              List profiles = project.getActiveProfiles()
              boolean profileAPresent=false
              profiles.each {
                if ( it.getId().equals("profile-a" ) {
                  profileAPresent=true
                }
              }
              if ( !profileAPresent ) {
                fail("profile-b can be used only together with profile-a")
              }
            </source>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With