how to stop maven build using checkstyle

2019-02-17 07:53发布

I am new to maven and checkstyle so please be polite:).

I've managed to use maven with checkstyle plugin and I can create reports on my code. But what I want really to have is that I can stop the build process of maven if there are any errors on style check.

So far my pom.xml looks like below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.8</version>
      </plugin>
    </plugins>
  </reporting>
</project>

How can I reach my goal here? Our team wants to have strict coding style standards so I have to use it.

3条回答
▲ chillily
2楼-- · 2019-02-17 08:08

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> 
查看更多
疯言疯语
3楼-- · 2019-02-17 08:10

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>
查看更多
老娘就宠你
4楼-- · 2019-02-17 08:18

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.

查看更多
登录 后发表回答