如何使得在进行 POM 验证时出现警告时,mvn install 失败?

huangapple go评论114阅读模式
英文:

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

问题

我启动一个Maven构建(从本地环境或Jenkins启动),其中包含一些警告,例如:

[WARNING] 在构建com.example:application:jar:1.0-SNAPSHOT的有效模型时遇到了一些问题
[WARNING] com.example:application:${project.version}处的一些随机警告,/var/jenkins_home/workspace/com-example/example/pom.xml,第14行,第11列

是否有办法让mvn构建在出现这种警告时失败?

让我澄清一下:

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

我需要当我运行

mvn install

并且我收到一个"[WARNING] ...某些内容"时,mvn构建失败,并且我得到一个返回代码为1的进程:

mvn install
...
[WARNING]...
[INFO] ------------------------------------------------------------------------
[INFO] 构建失败
英文:

I launch a maven build (from local env or from jenkins) that has some warning, for example:

[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

is there a way to let the mvn build fail if there are this kind of warning?

Let me clarify:

I don't need a solution to the specific argument (the project.version variable)

I need that when i run

mvn install

and I get a "[WARNING] ...somethingsomething"

the mvn build fails and i got a 1 as a return code of the process:

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

答案1

得分: 0

以下是翻译好的部分:

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

另外,我发现使用“pipe” | 操作符时,我隐藏了第一个命令的结果,并且我发现可以使用PIPESTATUS环境变量获取特定管道阶段的结果。

./mvnw $MVN_CLI_OPTS -f $1 $2 | tee build.log
if [[ "${PIPESTATUS[0]}" == "1" ]]; then
    echo "mvn构建失败"
    return 1
fi
# 这将在日志的前n行中搜索
head -n30 build.log | grep -in "\[WARNING\]"
if [[ ! -z "$FAIL_ON_WARNING" && $? == 0 ]]; then
    echo "--------------------------------------"
    echo "请修复Maven构建警告并重试"
    echo "构建状态:失败"
    echo "--------------------------------------"
    return 1
fi
英文:

the solution was actually more easy than expected.
Just needed a little script to run the build. I'm omitting soem env vars.

Also, I found out that using the "pipe" | operator, I was hiding the result of the first command, and I found out that it's possible to get the result of a specific pipe stage using PIPESTATUS env var

./mvnw $MVN_CLI_OPTS -f $1 $2 | 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

huangapple
  • 本文由 发表于 2020年9月4日 16:20:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63737425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定