Maven check style as a part of the build

Paulius Matulionis picture Paulius Matulionis · Aug 29, 2012 · Viewed 19.8k times · Source

Is there a possibility to somehow force maven to fail the build if there are some checkstyle errors? Now I have to run site goal to generate javadocs and checkstyle reports. I want to make it on install goal and if checkstyle has some error I need build to fail. Is this possible to achieve?

Now I have my checkstyle in reporting block of 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>

Answer

hgrey picture hgrey · Aug 29, 2012

You need to bind checkstyle:check to a Maven lifecycle phase (e.g. validate ) and set failOnViolation to 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>