即使另一个配置文件被激活,如何保持 activeByDefault 的 Maven 配置文件处于活动状态?

发布于 2024-10-22 03:55:33 字数 834 浏览 1 评论 0原文

我的 pom.xml 中有一个配置文件,除非明确停用(-P !firstProfile),否则它应该始终处于活动状态。 我通过使用 activeByDefault 标志解决了这个问题:

<profiles>
  <profile>
    <id>firstProfile</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

现在在同一个 pom.xml 中,我定义了第二个配置文件,只有当配置文件真正激活时(-P secondaryProfile),它才应该处于活动状态。 因此默认行为是:firstProfile 处于活动状态,secondProfile 处于非活动状态。 在其他时候,除了第一个配置文件之外,我还想激活第二个配置文件。 现在的问题是,如果我使用“-P secondaryProfile”执行此操作,则 firstProfile 不幸会被停用。 Maven 文档是这样描述的:

... 该配置文件将自动 对所有构建都有效,除非另一个 激活同一个 POM 中的配置文件 使用前面描述的一种 方法。所有处于活动状态的配置文件 默认情况下会自动 当 POM 中的配置文件被停用时 在命令行上激活或 通过其激活配置。 ...

是否有可能以某种方式保持firstProfile始终处于活动状态(无需在settings.xml中声明它)?

I have a profile in my pom.xml which should be always active unless it is explicitely deactivated (-P !firstProfile).
I solved this by using the activeByDefault flag:

<profiles>
  <profile>
    <id>firstProfile</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

Now in the same pom.xml I have a second profile defined this should only be active if the profile is really activated (-P secondProfile).
So the default behaviour is: firstProfile active, secondProfile inactive.
At some other point I would like to activated the second profile in addition to the first profile.
Now the problem is that if I do that with "-P secondProfile" the firstProfile unfortunately gets deactivated.
The Maven documentation states this:

...
This profile will automatically be
active for all builds unless another
profile in the same POM is activated
using one of the previously described
methods. All profiles that are active
by default are automatically
deactivated when a profile in the POM
is activated on the command line or
through its activation config.
...

Is there somehow a possibility how to keep the firstProfile always active (without having to declare it in the settings.xml)?

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

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

发布评论

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

评论(7

神爱温柔 2024-10-29 03:55:33

一个技巧是避免 activeByDefault,而是通过缺少属性来激活配置文件,例如:

<profiles>
  <profile>
    <id>firstProfile</id>
    <activation>
      <property>
        <name>!skipFirstProfile</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

然后您应该能够使用 -DskipFirstProfile 停用配置文件
或使用 -P !firstProfile,但否则配置文件将处于活动状态。

请参阅: Maven:完整参考,配置文件激活- 缺少属性时激活

One trick is to avoid activeByDefault, and instead activate the profile by the absence of a property, eg:

<profiles>
  <profile>
    <id>firstProfile</id>
    <activation>
      <property>
        <name>!skipFirstProfile</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

You should then be able to deactivate the profile with -DskipFirstProfile
or with -P !firstProfile, but otherwise the profile will be active.

See: Maven: The Complete Reference, Profile Activation - Activation by the Absence of a Property

雪花飘飘的天空 2024-10-29 03:55:33

我希望有这样的可能性,但我经常错过它。我能找到的唯一相关的 JIRA 问题是这个:

MNG-4917:配置文件未激活,即使其 activeByDefault 设置为 true

也 未处于活动状态已解决为不是问题

我已经停止使用 activeByDefault,因为这种“全有或全无”的方法使其对我来说毫无价值。


更改此行为的唯一方法是编写自己的替换 DefaultProfileSelector,使用 @Component( role = ProfileSelector.class ) 将其注册为 plexus 组件,并将其放入 ${MAVEN_HOME}/lib/ext (这样它将被选为默认配置文件选择器)。 (如果您使用的是 Maven 3.0.2 或更早版本,您还必须在加载 ${MAVEN_HOME}/bin/m2.conf 之前编辑 ${MAVEN_HOME}/bin/m2.conf 来加载 lib/ext代码>lib)

I wish there was such a possibility, I have often missed it. The only relevant JIRA issue I could find is this one:

MNG-4917: Profile not active even though it has activeByDefault set to true

And it's been resolved as Not A Problem.

I've stopped using activeByDefault, because this "all or nothing" approach made it worthless for me.


The only way to change this behavior is to write your own replacement for DefaultProfileSelector, register it as a plexus component with @Component( role = ProfileSelector.class ) and put it in ${MAVEN_HOME}/lib/ext (that way it will be picked as default profile selector). (If you are using Maven 3.0.2 or older you will also have to edit ${MAVEN_HOME}/bin/m2.conf to load lib/ext before it loads lib)

忆离笙 2024-10-29 03:55:33

这个问题很古老,但似乎可以通过使用 activeProfile 而不是 activeByDefault 来解决该问题。我使用的是 Maven 3.3.9,但该解决方案可能适用于早期版本。

只需在 settings.xml 中列出您的 activeProfiles,如下所示:

<settings>
  <profiles>
    [...]
  </profiles>
  <activeProfiles>
    <activeProfile>my-awesome-profile</activeProfile>
  </activeProfiles>
</settings>

my-awesome-profile 中,我有数据库 URL 等设置,所以它们总是适用。在这里,我激活了第二个配置文件 resolve-from-central

$ mvn help:all-profiles -P resolve-from-central 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:all-profiles (default-cli) @ standalone-pom ---
[INFO] Listing Profiles for Project: org.apache.maven:standalone-pom:pom:1
  Profile Id: resolve-from-central (Active: true , Source: settings.xml)
  Profile Id: my-awesome-profile (Active: true , Source: settings.xml)
  Profile Id: resolve-from-internal (Active: false , Source: settings.xml)

请注意 my-awesome-profile 仍然处于活动状态。耶!

This question is ancient, but it appears the problem is solvable by using activeProfile rather than activeByDefault. I'm on Maven 3.3.9, but the solution may work on earlier versions.

Simply list out your activeProfiles in your settings.xml, like so:

<settings>
  <profiles>
    [...]
  </profiles>
  <activeProfiles>
    <activeProfile>my-awesome-profile</activeProfile>
  </activeProfiles>
</settings>

In my-awesome-profile I have settings like database URLs and so on, so they always apply. Here, I activate a second profile, resolve-from-central:

$ mvn help:all-profiles -P resolve-from-central 
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:all-profiles (default-cli) @ standalone-pom ---
[INFO] Listing Profiles for Project: org.apache.maven:standalone-pom:pom:1
  Profile Id: resolve-from-central (Active: true , Source: settings.xml)
  Profile Id: my-awesome-profile (Active: true , Source: settings.xml)
  Profile Id: resolve-from-internal (Active: false , Source: settings.xml)

Notice how my-awesome-profile is still active. Yay!

九命猫 2024-10-29 03:55:33

配置文件是将一些秩序带入 POM 的好方法。
特别是当您出于不同目的多次执行同一插件时。

使用文件:

<profile>
    <id>alwaysActive</id>
    <activation>
         <file><exists>.</exists></file>
    </activation>
    ...
</profile>

这始终是正确的(除非有人在 Maven 启动期间删除了该目录:)。使用 Maven 3.6.0 进行测试。

这也可能是区分项目类型的好方法。例如,我的项目始终存在 module.json

使用配置文件激活扩展

有一些用于配置文件激活的 Maven 扩展。其中一个在叉子里:
https://github.com/OndraZizka/el-profile-activator-extension

Profiles are a good way to bring some order into POM.
Especially if you use multiple executions of the same plugin for different purposes.

Using files:

<profile>
    <id>alwaysActive</id>
    <activation>
         <file><exists>.</exists></file>
    </activation>
    ...
</profile>

This will always be true (unless someone deletes the directory during Maven boot :). Tested with Maven 3.6.0.

It might also be a good way to differentiate between types of projects. For instance, my project has always module.json present.

Using a profile activating extension

There are a few Maven extensions for profile activation. One of them in a fork here:
https://github.com/OndraZizka/el-profile-activator-extension

熟人话多 2024-10-29 03:55:33

您可以简单地在命令行上列出您想要激活的所有配置文件,如下所示:

-P profile-1,profile-2

maven 被设计为允许自动激活多个配置文件,但是如果您使用 -P 覆盖它,那么只有列出的配置文件参数中的已激活。

You can simply list all the profiles you want activated on the command line as such:

-P profile-1,profile-2

maven was designed to allow multiple profile activation automatically, if you however override that with the -P then only the profiles listed in the parameter are activated.

你爱我像她 2024-10-29 03:55:33

您无法保持默认配置文件处于活动状态,但您可以获取该配置文件的内容(示例中的 ...)并将其移动到 pom.xml 的主要部分。

仅仅因为您正在使用配置文件,并不意味着您所做的一切都需要在配置文件中。

You can't keep the default profile active, but you can take the contents of that profile (the ... in your example) and just move it to the main section of the pom.

Just because you are using profiles, it does not mean everything you are doing needs to be within a profile.

情深如许 2024-10-29 03:55:33

这是我针对 activeByDefault 的解决方法,它在设计上被破坏了,请参阅 MNG-4917。感谢肖恩·帕特里克·弗洛伊德 (Sean Patrick Floyd) 在他的回答中提供了该问题的链接。

<activation>
  <jdk>[1.)</jdk>
</activation>

This is my workaround for activeByDefault, which is broken by design, see MNG-4917. Thanks to Sean Patrick Floyd for the link to that issue in his answer.

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