gpt4 book ai didi

java - 为什么 !0 匹配默认值 0?

转载 作者:行者123 更新时间:2023-12-02 11:45:32 25 4
gpt4 key购买 nike

我正在尝试设置一个 .pom 文件,如果 forkCount 为 0,则该文件将使用一个插件,否则使用不同的插件。此外,我希望 0 作为默认值。换句话说,我想要

mvn run_testsmvn -DforkCount=0 run_tests 都使用插件“A”,其中 mvn run_tests -DforkCount=5 将使用插件“B”。

我有一个包含以下片段的 .pom 文件:

<project ...>
...
<properties>
<forkCount>0</forkCount>
</properties>
...
<profiles>
<profile>
<!-- if forkCount==0, don't invoke any of the parallel execution configuration -->
<id>no-parallel-execution</id>
<activation>
<property>
<name>forkCount</name>
<value>0</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<! --- nothing in here references forkCount -->
</plugin>
</plugins>
</build>
</profile>
<profile>
<profile>
<!-- forkCount!=0, use the parallel execution configuration -->
<id>parallel-execution</id>
<activation>
<property>
<name>forkCount</name>
<value>!0</value>
</property>
</activation>
<build>
<plugins>
<plugin>
...
<configuration>
...
<forkCount>${forkCount}</forkCount>
...
</configuration>
</plugin>
</plugins>
</build>
...

上面包含了对 forkCount 的唯一引用。

如果我在命令行上传递 forkCount 的值,一切都会按预期工作(即,当 forkCount 为 0 时使用插件“A”;当 forkCount 为 0 时使用插件“B” “否则使用)。但是,如果我运行 mvn run_tests,那么插件“B”就会被激活,即使 ${forkCount} 的值为 0。这是怎么回事?

它的值(value):

>mvn -DforkCount=0 clean verify help:active-profiles
The following profiles are active:
- no-parallel-execution (source: ....


>mvn clean verify help:active-profiles
The following profiles are active:
- parallel-execution (source: ....

最佳答案

尝试

mvn -DforkCount=0 help:active-profiles

验证您真正想要激活的个人资料是否处于 Activity 状态(以及您真正不想激活的个人资料是否处于 Activity 状态)。

--更新答案以适应新信息--

感谢您对答案的更新,现在问题似乎很清楚了。

我认为问题在于“”不是“0”。有了这种理解,这意味着“!0”将在 '' 或缺少 forkCount 值时激活。

我的测试证实了这种解释。

也许您可以使用更多配置文件重做此操作。一种用于检测属性未设置的情况,另一种用于检测属性为零的情况。这两个配置文件都可能会留下 Artifact ,例如 $target 目录中的触摸文件。然后您可以使用此文件来知道您正在执行单线程调用,如果没有该文件,则为多线程调用。

用于确认这些想法的代码

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>edwinbuck.com</groupId>
<artifactId>example-properties</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<profiles>
<profile>
<id>unspecified-forkCount</id>
<activation>
<property>
<name>!forkCount</name>
</property>
</activation>
</profile>
<profile>
<id>zero-forkCount</id>
<activation>
<property>
<name>forkCount</name>
<value>0</value>
</property>
</activation>
</profile>
<profile>
<id>parallel-execution</id>
<activation>
<property>
<name>forkCount</name>
<value>!0</value>
</property>
</activation>
</profile>
</profiles>
</project>

用于确认这些想法的命令行调用

mvn help:active-profiles
mvn -DforkCount=0 help:active-profiles
mvn -DforkCount=3 help:active-profiles

结果

profiles: unspecified-forkCount parallel-execution
profiles: zero-forkCount
profiles: parallel-execution

关于java - 为什么 <value>!0</value> 匹配默认值 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48234412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com