我试图要求在构建期间使用 maven 强制插件和RequireProperty
规则定义特定属性。
我的相关部分pom.xml
是这个。
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<foo>bar</foo>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<id>require-properties</id>
<goals><goal>enforce</goal></goals>
<configuration>
<rules>
<requireProperty>
<property>${foo}</property>
<message>foo must be defined -${foo}-</message>
</requireProperty>
</rules>
<!-- Disabling, since it seems to not notice properties that are defined -->
<!-- <skip>true</skip> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
当我运行它时,执行器规则失败。
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M1:enforce (require-properties) @ test ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireProperty failed with message:
foo must be defined -bar-
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.398 s
[INFO] Finished at: 2019-03-15T11:45:58-04:00
[INFO] ------------------------------------------------------------------------
如果我在命令行上定义它也会失败。
$ mvn install -Dfoo=quz
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M1:enforce (require-properties) @ test ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireProperty failed with message:
foo must be defined -quz-
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
这条规则不是为了<properties>
价值观吗?从消息中可以清楚地看出该值是实际设置的。
我也尝试添加<regex>.*</regex>
没有效果。
如何检查是否设置了 Maven 属性?
真实案例详情
在示例中,属性是在 pom 中定义的,因此包含检查有点多余。在实际情况下,插件在父 pom 中定义,属性在叶 pom 中定义。实际情况下的行为是相同的。
mvn-版本
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00) Maven 主页:/usr/local/Cellar/maven/3.6.0/libexec Java 版本:10.0.1,供应商:Oracle Corporation ,运行时:/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home 默认语言环境:en_US,平台编码:UTF-8 操作系统名称:“mac os x”,版本:“10.14.3”,arch :“x86_64”,家庭:“mac”