maven - > profile - >激活 - 所有条件都需要或只需一个?

时间:2021-11-27 23:08:07

Configuration:
- Maven: 3.0.5
- Java: 1.6.0_45

配置: - Maven:3.0.5 - Java:1.6.0_45

Description:

Let's say we have profile configuration like below:

假设我们有如下配置文件配置:

<profiles>
    <profile>
        <id>profile-1</id>
        <activation>
            <jdk>1.6</jdk>
            <property>
                <name>name</name>
                <value>Hubert</value>
            </property>
        </activation>
    </profile>
    <profile>
        <id>profile-2</id>
        <activation>
            <jdk>1.6</jdk>
            <property>
                <name>name</name>
                <value>Wiktoria</value>
            </property>
        </activation>
    </profile>
</profiles>

We have two profiles: profile-1 and profile-2.

我们有两个配置文件:profile-1和profile-2。

Profile profile-1 should be active when two requirements are met:
- jdk is version 1.6
- property name has value Hubert

当满足两个要求时,配置文件profile-1应该是活动的: - jdk是版本1.6 - 属性名称具有值Hubert

Question:

Let's check this configuration:

我们来看看这个配置:

mvn -Dname=Hubert help:active-profiles

mvn -Dname = Hubert help:active-profiles

As a result I get that there are two active profiles: profile-1 and profile-2.
Hmm...
Profile profile-2 should not be active since property name has value different from expected Wiktoria.

结果我得到了两个活动的配置文件:profile-1和profile-2。嗯...配置文件profile-2不应该处于活动状态,因为属性名称的值与预期的Wiktoria不同。

Could someone explain me why this work like this? Is it a normal behavior?
Thanks.

有人可以解释一下为什么这样的工作?这是正常的行为吗?谢谢。

2 个解决方案

#1


6  

The problem here is that the activation list with your trigger conditions is connected with OR. They do have a ticket to provide multiple activation triggers, but it's still open. That means, that it matches your sdk rule which is true and therefore active.

这里的问题是带有触发条件的激活列表与OR连接。他们确实有一张提供多个激活触发器的票证,但它仍然是开放的。这意味着,它匹配您的sdk规则,该规则是真实的,因此是活动的。

<profile>
    <id>profile-1</id>
    <activation> <!-- true || true = true -->
        <jdk>1.6</jdk> <!-- true -->
        <property> <!-- true -->
            <name>name</name>
            <value>Hubert</value>
        </property>
    </activation>
</profile>
<profile>
    <id>profile-2</id>
    <activation> <!-- true || false = true -->
        <jdk>1.6</jdk> <!-- true -->
        <property> <!-- false -->
            <name>name</name>
            <value>Wiktoria</value>
        </property>
    </activation>
</profile>

#2


2  

NOTE: This is only a suplement to Chasmo's correct answer.

注意:这只是Chasmo正确答案的补充。

There is a book from sonatype describing Maven. In section Sonatype book (section 5.3.1) we can find:

有一本描述Maven的sonatype的书。在Sonatype一书(第5.3.1节)中,我们可以找到:

A profile is activated when all activation criteria has been satisfied.

当满足所有激活标准时,激活配置文件。

This is not true. Truth is that one condition is enought to activate profile, which is of course equal to OR logical condition. This behavior is described in Maven docs:

这不是真的。事实是,一个条件是激活配置文件,这当然等于OR逻辑条件。 Maven文档中描述了此行为:

Activation occurs when one or more of the specified criteria have been met. When the first positive result is encountered, processing stops and the profile is marked as active.

当满足一个或多个指定标准时发生激活。遇到第一个肯定结果时,处理停止并且配置文件被标记为活动。

This is for me neither intiutive nor very useful. But thats how maven works at the time of writing that.

这对我来说既不是激励性的,也不是非常有用的。但那就是maven在写这篇文章时的作用。

There is a ticket MNG-4565 for AND conjunction. This is marked as Bug, but acording to Maven doc it is not, so this ticket has been opened for almost 4 years. The most useful part is last comment to this ticket written by Ronny Pscheidl. His comment points to this source: and-activation-profile-selector. This changes default maven OR condition to AND condition. Tested. Works. But of course if you decide to use this, you have one more thing to remember of.

还有一张MNG-4565门票,用于AND连接。这被标记为Bug,但根据Maven doc,它不是,所以这张票已经开了差不多4年了。最有用的部分是对Ronny Pscheidl写的这张票的最后评论。他的评论指向了这个来源:and-activation-profile-selector。这会将默认的maven OR条件更改为AND条件。测试。作品。但是当然如果你决定使用它,你还有一件事需要记住。

#1


6  

The problem here is that the activation list with your trigger conditions is connected with OR. They do have a ticket to provide multiple activation triggers, but it's still open. That means, that it matches your sdk rule which is true and therefore active.

这里的问题是带有触发条件的激活列表与OR连接。他们确实有一张提供多个激活触发器的票证,但它仍然是开放的。这意味着,它匹配您的sdk规则,该规则是真实的,因此是活动的。

<profile>
    <id>profile-1</id>
    <activation> <!-- true || true = true -->
        <jdk>1.6</jdk> <!-- true -->
        <property> <!-- true -->
            <name>name</name>
            <value>Hubert</value>
        </property>
    </activation>
</profile>
<profile>
    <id>profile-2</id>
    <activation> <!-- true || false = true -->
        <jdk>1.6</jdk> <!-- true -->
        <property> <!-- false -->
            <name>name</name>
            <value>Wiktoria</value>
        </property>
    </activation>
</profile>

#2


2  

NOTE: This is only a suplement to Chasmo's correct answer.

注意:这只是Chasmo正确答案的补充。

There is a book from sonatype describing Maven. In section Sonatype book (section 5.3.1) we can find:

有一本描述Maven的sonatype的书。在Sonatype一书(第5.3.1节)中,我们可以找到:

A profile is activated when all activation criteria has been satisfied.

当满足所有激活标准时,激活配置文件。

This is not true. Truth is that one condition is enought to activate profile, which is of course equal to OR logical condition. This behavior is described in Maven docs:

这不是真的。事实是,一个条件是激活配置文件,这当然等于OR逻辑条件。 Maven文档中描述了此行为:

Activation occurs when one or more of the specified criteria have been met. When the first positive result is encountered, processing stops and the profile is marked as active.

当满足一个或多个指定标准时发生激活。遇到第一个肯定结果时,处理停止并且配置文件被标记为活动。

This is for me neither intiutive nor very useful. But thats how maven works at the time of writing that.

这对我来说既不是激励性的,也不是非常有用的。但那就是maven在写这篇文章时的作用。

There is a ticket MNG-4565 for AND conjunction. This is marked as Bug, but acording to Maven doc it is not, so this ticket has been opened for almost 4 years. The most useful part is last comment to this ticket written by Ronny Pscheidl. His comment points to this source: and-activation-profile-selector. This changes default maven OR condition to AND condition. Tested. Works. But of course if you decide to use this, you have one more thing to remember of.

还有一张MNG-4565门票,用于AND连接。这被标记为Bug,但根据Maven doc,它不是,所以这张票已经开了差不多4年了。最有用的部分是对Ronny Pscheidl写的这张票的最后评论。他的评论指向了这个来源:and-activation-profile-selector。这会将默认的maven OR条件更改为AND条件。测试。作品。但是当然如果你决定使用它,你还有一件事需要记住。