gpt4 book ai didi

maven-enforcer-plugin 和子 pom

转载 作者:行者123 更新时间:2023-12-04 10:36:17 27 4
gpt4 key购买 nike

我正在尝试使用 maven-enforcer-plugin 来确保激活一个配置文件(requireActiveProfile 规则)。请参阅此页面上的第二个示例:Require Active Profile

它适用于单个 pom 文件。当我尝试将它与 2 个 pom 文件(一个父文件和一个子文件)一起使用时,当我尝试构建子模块时,它不再起作用。

我知道为什么它不起作用?

完整示例:

EXAMPLE
| pom.xml <1> (parent pom)
|
\---child
pom.xml <2> (child pom)

父 pom 文件 <1> :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app.parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<profiles>
<profile>
<id>first</id>
<properties>
<my-name>Alice</my-name>
</properties>
</profile>
<profile>
<id>second</id>
<properties>
<my-name>Bob</my-name>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-first-or-second-profile-is-active</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireActiveProfile>
<profiles>first,second</profiles>
<all>false</all>
</requireActiveProfile>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Hello ${my-name}!</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

子 pom 文件 <2> :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app.parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>my-app</artifactId>
<packaging>pom</packaging>

</project>

现在我运行:
EXAMPLE>cd child
EXAMPLE\child>mvn compile -Psecond

输出如下所示:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-first-or-second-profile-is-active) @ my-app ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireActiveProfile failed with message:
Profile "first" is not activated.
Profile "second" is not activated.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.178 s
[INFO] Finished at: 2015-10-30T18:03:50+01:00
[INFO] Final Memory: 24M/989M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (enforce-first-or-second-profile-is-active) on project my-app: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

当我在父 pom 上运行 maven 时,它可以工作。我错过了什么?

最佳答案

Maven Profiles 有点令人困惑,但我会根据我的经验冒险解释一下。

配置文件定义不会被继承,但它们的效果是。

如果您跳转到 child目录并运行mvn -Pfirst help:active-profiles您会看到 parent 的个人资料的效果确实存在:

部分输出:

...
<properties>
<my-name>Alice</my-name>
</properties>
...

但您不会在其中看到配置文件定义。

坏消息是您的 enforcer规则在这种情况下不起作用,除非每个 child 也定义了所需的配置文件。

好消息是,有了这些新知识,您可以使用另一个 enforcer规则( requireProperty )来实现你想要的:
   <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>my-name</property>
<message>You must set a my-name property! Did you activate the right profile?</message>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>

关于maven-enforcer-plugin 和子 pom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442256/

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