Maven配置文件中的依赖项

10

我有一个maven模块,拥有两个配置文件:profile-a和profile-b。

可以单独使用profile-a,但是要运行profile-b需要同时运行profile-a。

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

有没有办法确保用户无法仅使用 profile-b 安装模块?
或者说,如果只使用 profile-b,如何自动激活 profile-a?

3个回答

9

有没有办法确保用户只能使用profile-a和profile-b一起安装,或者当用户仅使用profile-b时自动激活profile-a?

不行,无法从另一个配置文件中触发配置文件(不支持,请参见相关问题的Brett's answer),也无法严格禁止使用特定配置文件。

您可以做的最好的事情是使用属性激活以及一个公共属性来激活两个配置文件:

<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>

在调用mvn时传递属性将触发它们两个:

$ 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] 
项目“com.stackoverflow:Q4099626:jar:1.0-SNAPSHOT”的活动配置文件:
以下配置文件是活动的:
- profile-a(来源:pom) - profile-b(来源:pom)

虽然不是最理想的,但目前这是您能得到的最好结果。

相关问题


2
把这段代码放到profile-b里面。这样你就可以避免出现糟糕的构建并且可以提醒用户。我没有测试过,但应该可以正常工作。如果有拼写错误,请更正。
<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>

0
尝试在profile-a中使用activation元素,通过检查属性是否设置来实现。然后在profile-b中设置该属性,使得profile-a变为活动状态。

2
很遗憾,这不是一个支持的功能。 - Pascal Thivent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接