java Maven 检查样式作为构建的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12180765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:54:14  来源:igfitidea点击:

Maven check style as a part of the build

javamavencheckstyle

提问by Paulius Matulionis

Is there a possibility to somehow force maven to fail the build if there are some checkstyleerrors? Now I have to run sitegoal to generate javadocsand checkstylereports. I want to make it on installgoal and if checkstyle has some error I need build to fail. Is this possible to achieve?

如果出现一些checkstyle错误,是否有可能以某种方式强制 maven 使构建失败?现在我必须运行site目标来生成javadocscheckstyle报告。我想实现install目标,如果 checkstyle 有一些错误,我需要构建失败。这有可能实现吗?

Now I have my checkstylein reporting block of maven:

现在我有我checkstyle的 maven 报告块:

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <configLocation>src/test/resources/checkstyle.xml</configLocation>
            </configuration>
        </plugin>
    </plugins>
</reporting>

回答by hgrey

You need to bind checkstyle:checkto a Maven lifecycle phase (e.g. validate ) and set failOnViolationto true.

您需要绑定checkstyle:check到 Maven 生命周期阶段(例如 validate )并设置failOnViolation为 true。

Something like:

就像是:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.9.1</version>
    <executions>
        <execution>
        <id>checkstyle</id>
        <phase>validate</phase>
        <goals>
            <goal>check</goal>
        </goals>
        <configuration>
            <failOnViolation>true</failOnViolation>
        </configuration>
        </execution>
    </executions>
</plugin>

回答by JChrist

It might have been some time since the question was asked, but this wasn't working for me.

问这个问题可能已经有一段时间了,但这对我不起作用。

For anyone else that might be having the same issue as me, with the build succeeding despite a multitude of issues, I fixed it by lowering the violationSeverityproperty from its default errorto warningin the plugin's configurationblock.

对于可能与我有相同问题的其他任何人,尽管存在许多问题,但构建仍成功,我通过将violationSeverity属性从默认值降低errorwarning插件configuration块中来修复它。

回答by Garret Fick

Even though it has been a long time since this was asked, I did run into another problem:

尽管已经很长时间没有提出这个问题了,但我确实遇到了另一个问题:

JavadocMethod: Unable to get class information for @throws tag 'X'.

JavadocMethod: Unable to get class information for @throws tag 'X'.

I solved this by changing the phase from "validate" to "test" so that checkstyle runs after the compile phase.

我通过将阶段从“验证”更改为“测试”来解决这个问题,以便在编译阶段之后运行 checkstyle。