maven-enforcer-plugin 和子 pom
maven-enforcer-plugin and child pom
我正在尝试使用 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 来确保激活一个配置文件(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>