Skip to content

Instantly share code, notes, and snippets.

@tdonohue
Last active October 6, 2017 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdonohue/b51b919b87b5f08930fac4fd9d52b9ae to your computer and use it in GitHub Desktop.
Save tdonohue/b51b919b87b5f08930fac4fd9d52b9ae to your computer and use it in GitHub Desktop.
CheckStyle for DSpace?

Code Changes

Update Parent POM to use maven-checkstyle-plugin

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>

Create checkstyle.xml (based on Fedora's)

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="(?&lt;!\*)\s+$|\*\s\s+$"/>
    <property name="message" value="Trailing whitespace"/>
    <property name="fileExtensions" value="java,css,js,xml"/>
  </module>
  <module name="SuppressWarningsFilter" />
</module>

Test it out

mvn -U checkstyle:check

Fixing the codebase

Cleanup via IDEA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment