[JAVA] CheckStyle support

Java development environment memo I am building a development environment with CheckStyle, but since a lot of alerts are issued with CheckStyle, I will investigate and describe how to deal with it.

1.CheckStyle

1-1. Pom setting

pom.xml


			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-checkstyle-plugin</artifactId>
				<version>3.1.1</version>
				<dependencies>
					<dependency>
						<groupId>com.puppycrawl.tools</groupId>
						<artifactId>checkstyle</artifactId>
						<version>8.37</version>
					</dependency>
				</dependencies>
				<configuration>
					<configLocation>google_checks.xml</configLocation>
					<failsOnError>true</failsOnError>
					<failOnViolation>true</failOnViolation>
					<violationSeverity>error</violationSeverity>
					<consoleOutput>true</consoleOutput>
				</configuration>
				<executions>
					<execution>
						<id>checkstyle</id>
						<phase>verify</phase>
						<goals>
							<goal>check</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

1-2. Downloading the configuration file

Download from below?

https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

The pom.xml file has the following sites set for selection. https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/custom-checker-config.html

What if I get an error?

https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

There may be a difference between the rule file and library versions.

2-1. The import statement is not in lexicographic order.

2-1-1. Cause

If the package of your own class is at the beginning of the alphabet than org (jp etc.), you will be warned.

1-2. Solution by Eclipse

  1. [Window]-[Settings]
  2. [Java]-[Code Style]-[Organize Import]
  3. Create your own package (jp) with the "New" button and move it to org.

2.Package name'must match pattern' ^ [a-z] + (. [A-z] [a-z0-9] *) * $'.

Cause The package name contains symbols such as "_".

2-1. CheckStyle solution

Before correction


    <module name="PackageName">
      <property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
      <message key="name.invalidPattern"
             value="Package name ''{0}'' must match pattern ''{1}''."/>
    </module>

Change the above rule to INFO instead of warning. You can specify either ignore, info, warning, or error.

Revised


    <module name="PackageName">
      <property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
      <message key="name.invalidPattern"
             value="Package name ''{0}'' must match pattern ''{1}''."/>
             <property name="severity" value="info" />
    </module>

Reference URL

Share rules such as CheckStyle with multiple modules https://ssogabe.hatenadiary.org/entry/20091219/1261196056

Cause

Eclipse solution

CheckStyle solution

Recommended Posts

CheckStyle support
CheckStyle parameters
SonarQube Java 11 support
Java support period
Java8, 9, 10 support period
Support for CheckStyle "Do not use inline conditions"