how to stop maven build using checkstyle

ε祈祈猫儿з 提交于 2019-11-30 13:27:41
Michael Schmeißer

To achieve what you want, you need to use the maven-checkstyle-plugin in the build lifecycle in addition to the reporting lifecycle:

<project>
...
<build>
...
 <plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.8</version>
    <executions>
      <execution>
        <phase>process-sources</phase>
        <goals>
          <goal>check</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <failsOnError>true</failsOnError>
    </configuration>
  </plugin>
 </plugins>
</build>
</project>

I realize that there has been some time since this question was asked, but none of the above answers resolved this for me.

In order for the build to fail on violations, I had to change the violationSeverity value from its default error to warning in the configuration block, similar to:

<plugin>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <dependencies>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>7.5.1</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>validate</id>
            <phase>validate</phase>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
                <encoding>UTF-8</encoding>
                <consoleOutput>true</consoleOutput>
                <failsOnError>false</failsOnError>
                <failOnViolation>true</failOnViolation>
                <violationSeverity>warning</violationSeverity>
                <linkXRef>false</linkXRef>
            </configuration>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Also, please note that we have established a slightly modified version of styles (defined in checkstyle.xml), mostly based on the latest google_checks.xml. However, in order for this to work, the com.puppycrawl.tools.checkstyle dependency had to be updated as well.

planetjones

You could try setting the failsOnError property e.g.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.8</version>
    <configuration>
    <failsOnError>true</failsOnError>
    </configuration>
  </plugin> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!