我正在尝试使用maven enforcer插件来确保激活一个配置文件(requireActivatProfile规则)。请参阅本页的第二个示例:需要活动配置文件
它可以很好地与单个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
EXAMPLEchild>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概要文件有点令人困惑,但我将根据我的经验大胆解释一下。
概要文件定义不是继承的,但它们的效果是继承的。
如果您跳转到child
目录并运行mvn -Pfirst help:active-profiles
,您将看到父配置文件的效果确实存在:
部分输出:
...
<properties>
<my-name>Alice</my-name>
</properties>
...
但你不会在那里看到概要文件定义。
坏消息是,除非每个孩子都定义了所需的配置文件,否则enforcer
规则在这种情况下不起作用。
好消息是,有了这些新知识,您可以使用另一个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>