Paulius Matulionis
Paulius Matulionis

Reputation: 23415

Maven check style as a part of the build

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>

Upvotes: 23

Views: 22724

Answers (3)

Garret Fick
Garret Fick

Reputation: 83

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'.

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

Upvotes: 0

JChrist
JChrist

Reputation: 1752

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 violationSeverity property from its default error to warning in the plugin's configuration block.

Upvotes: 5

hgrey
hgrey

Reputation: 3093

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>

Upvotes: 31

Related Questions