Daniel
Daniel

Reputation: 41

Checkstyle not working in build or verify phase

I cannot get maven checkstyle to run on build or verify, despite copy & pasting the code directly from the checkstyle website.

I can see checkstyle working fine and finding problems when running mvn checkstyle:check, but not on any phase like clean compile verify. These phases just compile fine and find no problems, completely ignoring checkstyle. I set up a minimal example here. The pom.xml is as follows:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>checkstyle-test</groupId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>15</java.version>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>core</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>3.1.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <failOnViolation>true</failOnViolation>
                        <linkXRef>false</linkXRef>
                    </configuration>
                    <executions>
                        <execution>
                            <id>validate</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

Upvotes: 1

Views: 1273

Answers (2)

Daniel
Daniel

Reputation: 41

Figured it out: I had the block in <pluginManagement>. Moving it out of there it instantly worked. I wished, maven would have more complete example on their website.

Upvotes: 0

Nick Mancuso
Nick Mancuso

Reputation: 191

You need to bind Checkstyle execution to the verify phase, like this:

...
    <execution>
        <id>checkstyle-check</id>
        <phase>verify</phase>
        <configuration>
            <sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>
            <configLocation>config/checkstyle_checks.xml</configLocation>
            <encoding>UTF-8</encoding>
        </configuration>
        <goals>
            <goal>check</goal>
        </goals>
    </execution>
...

This snippet is taken from one of my projects, you can view the entire pom.xml here.

Upvotes: 1

Related Questions