首页 > 解决方案 > 运行 Maven Checkstyle 并因错误而失败

问题描述

除此之外mvn test,我想运行 Maven 的构建过程,它应该会因违规而失败。我在 pom.xml 中尝试了不同的选项,但没有任何效果。failOnViolationfailsOnError没有帮助。

我可以运行mvn checkstyle:check并将错误输出到控制台(起诉<consoleOutput>true</consoleOutput>)。但是我怎样才能运行 checkstyle 并让它在违规时失败?

这是我到目前为止所尝试的:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>true</failsOnError>
                    <failOnViolation>true</failOnViolation>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

标签: mavencheckstyle

解决方案


正确的方法是修改violationSeverity值。

<configuration>
    <violationSeverity>warning</violationSeverity>
    ...
</configuration>

这设置了被视为违规的最低严重性级别。有效值为“错误”、“警告”和“信息”。更多信息:https ://maven.apache.org/plugins/maven-checkstyle-plugin/check-mojo.html


推荐阅读