Skip to content

2. How to use Maven Checkstyle Plugin

ozlmulg edited this page Jul 23, 2021 · 3 revisions

Configuration

To use it, configure your maven-checkstyle-plugin like so:

If you want to check checkstyle on builds:

  <properties>
    <!--checkstyle-->
    <checkstyle.version>${YOUR_CHECKSTYLE_VERSION}</checkstyle.version>
    <startupheroes-checkstyle.version>${YOUR_STARTUPHEROES_CHECKSTYLE_VERSION}</startupheroes-checkstyle.version>
    <maven-checkstyle-plugin.version>${YOUR_MAVEN_CHECKSTYLE_VERSION}</maven-checkstyle-plugin.version>
    <checkstyle.configLocation>/es/startuphero/checkstyle/startupheroes_checks.xml</checkstyle.configLocation>
    <checkstyle.excludes>${YOUR_CHECKSTYLE_EXCLUDES}</checkstyle.excludes>
  </properties>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>${maven-checkstyle-plugin.version}</version>
        <dependencies>
          <dependency>
            <groupId>es.startuphero.checkstyle</groupId>
            <artifactId>startupheroes-checks</artifactId>
            <version>${startupheroes-checkstyle.version}</version>
          </dependency>
          <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>${checkstyle.version}</version>
          </dependency>
        </dependencies>
        <configuration>
          <configLocation>${checkstyle.configLocation}</configLocation>
          <violationSeverity>warning</violationSeverity>
          <includeTestSourceDirectory>true</includeTestSourceDirectory>
          <excludes>${checkstyle.excludes}</excludes>
        </configuration>
        <executions>
          <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

If you want to use as a profile:

<profile>
  <id>checkstyle</id>
  <properties>
    <checkstyle.configLocation>/es/startuphero/checkstyle/startupheroes_checks.xml</checkstyle.configLocation>
    <checkstyle.excludes>${YOUR_CHECKSTYLE_EXCLUDES}</checkstyle.excludes>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>${maven-checkstyle-plugin.version}</version>
        <dependencies>
          <dependency>
            <groupId>es.startuphero.checkstyle</groupId>
            <artifactId>startupheroes-checks</artifactId>
            <version>${startupheroes-checkstyle.version}</version>
          </dependency>
          <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>${checkstyle.version}</version>
          </dependency>
        </dependencies>
        <configuration>
          <configLocation>${checkstyle.configLocation}</configLocation>
          <!-- The things above this line are required, the rest is 'bonus' -->
          <consoleOutput>true</consoleOutput>
          <!-- Remove or switch to false to keep building even with checkstyle errors -->
          <failOnViolation>true</failOnViolation>
          <logViolationsToConsole>true</logViolationsToConsole>
          <!-- change to 'error' to be more strict about following checkstyle conventions -->
          <violationSeverity>warning</violationSeverity>
          <includeTestSourceDirectory>true</includeTestSourceDirectory>
          <excludes>${checkstyle.excludes}</excludes>
        </configuration>
        <executions>
          <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
              <goal>checkstyle</goal>
              <goal>checkstyle-aggregate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>

See the maven-checkstyle-plugin docs for more information about what the configuration settings mean.

Internally, we have the above configuration in the <pluginManagement/> section of a company-wide parent pom, meaning that projects only need to specify the below in their <build><plugins> section:

   <plugin>
      <artifactId>maven-checkstyle-plugin</artifactId>
   </plugin>

Suppressions

If you want to exclude specific packages or classes then add maven properties for each module like:

      <checkstyle.excludes>**/x/y/generated/GeneratedClass.java, **/generated/**</checkstyle.excludes>

Execution

To run checkstyle plugin:

   mvn checkstyle:checkstyle

To run checkstyle profile:

   mvn clean install -Pcheckstyle

Result

To see the checkstyle results after maven run, look checkstyle result file from target/site/checkstyle-aggregate.html: