Maven配置文件依赖

发布于 2024-09-30 15:32:48 字数 362 浏览 3 评论 0原文

我有一个带有 2 个配置文件 profile-a 和 profile-b 的 Maven 模块

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 安装模块? 或者如果单独使用配置文件-b,则自动激活配置文件-a?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过潦 2024-10-07 15:32:48

是否有办法确保用户无法仅使用配置文件-b 安装模块?或者如果单独使用 profile-b,则自动激活 profile-a?

不,无法从另一个配置文件触发配置文件(不支持,请参阅 Brett 对相关问题的回答)也不是严格禁止使用给定的配置文件。

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

<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] 
Active Profiles for Project 'com.stackoverflow:Q4099626:jar:1.0-SNAPSHOT': 

The following profiles are active:

 - profile-a (source: pom)
 - profile-b (source: pom)

这并不理想,但目前这是最好的你可以得到。

相关问题

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

徒留西风 2024-10-07 15:32:48

将其放入配置文件-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>

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>
潦草背影 2024-10-07 15:32:48

通过检查是否设置了属性,尝试使用 profile-a 中的 activation 元素。然后在 profile-b 中设置属性,以便 profile-a 将变为活动状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文