如果 pom 验证有警告,如何让 mvn 安装失败?

how to get mvn install fail if there are warnings on pom validation?

我启动了一个有一些警告的 Maven 构建(来自本地环境或来自 jenkins),例如:

[WARNING] Some problems were encountered while building the effective model for com.example:application:jar:1.0-SNAPSHOT
[WARNING] some random warning @ com.example:application:${project.version}, /var/jenkins_home/workspace/com-example/example/pom.xml, line 14, column 11

如果有这种警告,有没有办法让 mvn 构建失败?

让我澄清一下:

我不需要特定参数的解决方案(project.version 变量)

当我 运行

我需要那个
mvn install

然后我收到“[警告] ...某事某事”

mvn 构建失败,我得到一个 1 作为进程的 return 代码:

mvn install
...
[WARNING]...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

解决方案实际上比预期的要简单。 只需要一个小脚本来 运行 构建。我省略了 soem 环境变量。

此外,我发现使用“管道”|运算符,我隐藏了第一个命令的结果,我发现可以使用 PIPESTATUS env var

获取特定管道阶段的结果
./mvnw $MVN_CLI_OPTS -f   | tee build.log
if [[ "${PIPESTATUS[0]}" == "1" ]]; then
    echo "mvn build failed"
    return 1
fi
#this will grep only in the first n rows of the log
head -n30 build.log | grep -in "\[WARNING\]"
if [[ ! -z "$FAIL_ON_WARNING" && $? == 0 ]]; then
    echo "--------------------------------------"
    echo "PLEASE FIX MAVEN BUILD WARNINGS AND RETRY"
    echo "BUILD STATUS: FAILED"
    echo "--------------------------------------"
    return 1
fi