Resources: https://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html
Add maven-checkstyle-plugin
to the <pluginManagement>
section:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>verify-style</id>
<phase>process-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>${root.basedir}/checkstyle.xml</configLocation>
<encoding>${project.build.sourceEncoding}</encoding>
<logViolationsToConsole>true</logViolationsToConsole>
<linkXRef>false</linkXRef>
</configuration>
<dependencies>
<!-- Use latest version of checkstyle -->
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.2</version>
</dependency>
</dependencies>
</plugin>
Fedora's for reference: https://github.com/fcrepo4/fcrepo-build-tools/blob/master/src/main/resources/fcrepo-checkstyle/checkstyle.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<!-- Suppress -->
<!--<module name="SuppressionFilter">
<property name="file" value="${checkstyle.suppressions.file}"/>
</module>-->
<!-- Checks that there are no tab characters ('\t') in the source code. -->
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
<property name="fileExtensions" value="java,css,js,xml"/>
</module>
<!-- License Header module disabled in favor of license-maven-plugin -->
<module name="TreeWalker">
<!-- Checks for redundant import statements -->
<module name="RedundantImport"/>
<!-- Checks for unused import statements -->
<module name="UnusedImports"/>
<!-- Check that finds import statements that use the * notation. -->
<module name="AvoidStarImport"/>
<!-- Checks for long lines. -->
<!--<module name="LineLength">
<property name="max" value="120"/>
</module>-->
<!-- Write Javadocs for public methods and classes. Keep it short and to the point
/**
* @author Joe Developer
* @date MMM DD, YYYY
*/
public class MyClass
-->
<!--
<module name="JavadocType">
<property name="scope" value="public"/>
<property name="excludeScope" value="anoninner"/> -->
<!-- Contrary to expectation only checks for author tags without an author -->
<!--<property name="authorFormat" value="\S"/>
<property name="allowUnknownTags" value="true"/>
<property name="allowMissingParamTags" value="false"/>
</module>-->
<!-- Ensure files have the Javadoc @author tag -->
<!--<module name="Regexp">
<property name="format" value=" \* \@author "/>
<property name="message" value="Javadoc @author tag"/>
</module>
<module name="JavadocMethod">
<property name="scope" value="public"/>
<property name="allowUndeclaredRTE" value="true"/>
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingThrowsTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
</module>-->
<!-- K&R style braces -->
<module name="NeedBraces"/>
<module name="LeftCurly"/>
<module name="RightCurly"/>
<module name="WhitespaceAround">
<property name="tokens"
value="ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT,
MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN, TYPE_EXTENSION_AND"
/>
</module>
<module name="Indentation">
<property name="throwsIndent" value="8"/>
</module>
<module name="SuppressWarningsHolder"/>
<!--<module name="FinalLocalVariable"/>
<module name="FinalParameters"/>-->
<module name="GenericWhitespace"/>
<module name="HideUtilityClassConstructor"/>
<module name="MultipleVariableDeclarations"/>
</module>
<!-- No Trailing Whitespace, except on lines that only have an asterisk (e.g. Javadoc comments) -->
<module name="RegexpSingleline">
<property name="format" value="(?<!\*)\s+$|\*\s\s+$"/>
<property name="message" value="Trailing whitespace"/>
<property name="fileExtensions" value="java,css,js,xml"/>
</module>
<module name="SuppressWarningsFilter" />
</module>
mvn -U checkstyle:check
- Install checkstyle plugin: https://plugins.jetbrains.com/plugin/1065-checkstyle-idea
- File -> Settings -> Plugins
- Restart IDE
- Configure it to use our custom
checkstyle.xml
(see above)- File -> Settings -> Checkstyle
- Add a "Configuration File" (press the
+
in the table). Select ourcheckstyle.xml
- Make it "Active"
- Update IDEA's Code Style to obey the Checkstyle Rules
- Now open up any Java source file. You'll see Checkstyle warnings appear (if any) in RED
- Select "Code -> Reformat Code" (
Ctrl + Alt + L
) to reformat the open source file based on Checkstyle rules.- If any
import
statements use an asterisk, you will also need to do "Code -> Optimize Imports" (Ctrl + Alt + O
) to correct those.
- If any
- NOTE: You can also reformat code in bulk in IDEA: https://www.jetbrains.com/help/idea/reformatting-source-code.html#refromat