Maven project uses Checkstyle to check code

Catalog

Maven project uses Checkstyle to check code

Checkstyle can do custom style code checking, and here are some examples to use for reference.

Configure checkstyle-IDEA plug-in in idea

With the checkstyle-IDEA plug-in, you can directly rely on idea to check your code. The advantage is the graphical interface, intuitive operation, shared by all projects after installation, but the disadvantage is that you need to perform the check manually.
Press the shortcut key Ctrl+Alt+S, select Plugins, and enter checkStyle-IDEA in the marketplace to search for installations.

If you can't find it, you can also open the plug-in Official Web https://plugins.jetbrains.com/Search for checkStyle-IDEA and click Install to automatically download the installation when you return to idea.


After installation, press the shortcut key Ctrl+Alt+S and select the Checkstyle item to configure, as shown in the figure (check the rule file to refer to what I customized at the end of the article)

In the lower left corner, there is a Checkstyle tab, select Rule File to run.

Configure using Checkstyle in Maven projects

Since the project is a Maven project, you want the checks to be performed automatically at compile time without additional manual execution, so you can choose toPom.xmlConfigure the maven-checkstyle-plugin plug-in to bind to Maven's life cycle so that execution checks are triggered automatically when commands such as mvn compile are executed.

Single module maven project

A single-module maven project only needs to configure plugins.Pom.xmlThe configuration is as follows:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>3.1.1</version>
                    <dependencies>
                        <!--Specify Dependent checkstyle Edition-->
                        <dependency>
                            <groupId>com.puppycrawl.tools</groupId>
                            <artifactId>checkstyle</artifactId>
                            <version>8.33</version>
                        </dependency>
                    </dependencies>
                    <!--Specify Profile-->
                    <configuration>
                        <configLocation>checkstyle.xml</configLocation>
                        <encoding>UTF-8</encoding>
                        <consoleOutput>true</consoleOutput>
                        <failsOnError>true</failsOnError>
                        <linkXRef>false</linkXRef>
                    </configuration>
                    <executions>
                        <execution>
                            <id>validate</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.9.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Multi-module maven project

Multi-module maven project, reference Official Configuration (https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html), only in the parent module'sPom.xmlJust configure the plug-in inside,Pom.xmlConfiguration is as follows

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.1.1</version>
        <dependencies>
          <dependency>
            <groupId>com.example.whizbang</groupId>
            <artifactId>build-tools</artifactId>
            <version>1.0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

Configuration in Jenkins

Using Checkstyle in Jenkins, you configure maven's checkstyle (steps above), then edit Jenkins'build configuration.
1. Add C to Build's Goals and OptionsHeckstyle:checkstyle,
2. Check the [Deprecated] Publish Checkstyle analysis results option for the build configuration.
3. Save, rebuild the project, and restart Jenkins if there is no Checkstyle Trend on the right.

abnormal

com.puppycrawl.tools.checkstyle.api.CheckstyleException: 
cannot initialize module TreeWalker 
- TreeWalker is not allowed as a parent of LineLength Please review 
 'Parent Module' section for this Check in web documentation if Check is standard.

Looking at the official documentation, the property LineLength needs to be placed in Checker, but many examples on the web are placed in TreeWalker.

Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1:check
 (validate) on project junitdemo: Failed during checkstyle configuration:
  cannot initialize module TreeWalker - Token "INTERFACE_DEF" was not found 
  in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck

Google_in official warehouseChecks.xml, using INTERFACE_A new token was added to DEF, and by looking at GitHub's submission records, you can see that RightCurlyCheck did not have merge code to support INTERFACE_in version 8.33DEF, so just remove it.

Reference resources

Official warehouse: https://github.com/checkstyle/checkstyle
checks list: https://checkstyle.org/checks.html
Maven Plugin: https://maven.apache.org/plugins/maven-checkstyle-plugin/

Tags: Maven Apache jenkins xml

Posted on Mon, 22 Jun 2020 22:19:38 -0400 by Trevors