开发者

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?


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

  • Why can't I activate a Maven2 profile from another profile?
  • Can I make one maven profile activate another?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜