maven profile dependency
Asked Answered
H

3

10

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?

Himes answered 4/11, 2010 at 17:52 Comment(0)
P
9

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

Preponderant answered 4/11, 2010 at 20:16 Comment(0)
V
2

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>
Voluptuary answered 29/8, 2013 at 11:28 Comment(0)
G
0

Try using the activation element in profile-a by checking if a property is set. Then in profile-b set the property so profile-a will become active.

Gibson answered 4/11, 2010 at 18:49 Comment(1)
Unfortunately, this is not a supported feature.Preponderant

© 2022 - 2024 — McMap. All rights reserved.