Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java google checkstyle Maven

I'm trying to configure my Maven project to use google java check style with the following configuration:

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

pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>2.17</version>
      <executions>
        <execution>
          <id>checkstyle</id>
          <phase>validate</phase>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <configLocation>google_checks.xml</configLocation>
        <encoding>UTF-8</encoding>
        <consoleOutput>true</consoleOutput>
        <failsOnError>true</failsOnError>
      </configuration>
    </plugin>
  </plugins>
</build>

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jxr-plugin</artifactId>
      <version>2.5</version>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>2.17</version>
      <configuration>
        <configLocation>google_checks.xml</configLocation>
        <failOnViolation>false</failOnViolation>
        <enableFilesSummary>false</enableFilesSummary>
      </configuration>
    </plugin>
  </plugins>
</reporting>

It seems to run mvn checkstyle:check fine at first. But after a few runs I start getting the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check 
(default-cli) on project PROJECT: Failed during checkstyle configuration: cannot initialize 
module TreeWalker - Token "METHOD_REF" was not found in Acceptable tokens list in check 
com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck -> [Help 1]

What does that mean? Why does it only happen some times and how do I get rid of it?

like image 672
Maglethong Spirr Avatar asked May 15 '17 04:05

Maglethong Spirr


2 Answers

I was using version 3.0.0 (which is the newest one right now) of the maven-checkstyle-plugin and I still got the error. I solved it by adding the following dependency to the plugin.

 <dependency>
     <groupId>com.puppycrawl.tools</groupId>
     <artifactId>checkstyle</artifactId>
     <version>8.11</version>
 </dependency>

like image 67
Dennis Böttcher Avatar answered Oct 23 '22 04:10

Dennis Böttcher


Token "METHOD_REF" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck

You are trying to use a newer configuration with an old version of Checkstyle.

The configuration at https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml is in master which is dependent on the snapshot version of checkstyle.

If you are using google configuration without any modifications, you need to use the one that comes embedded in checkstyle. See https://stackoverflow.com/a/35486365/1016482

Otherwise you can integrate a newer version of checkstyle to work with maven. See https://stackoverflow.com/a/27359107/1016482

like image 15
rveach Avatar answered Oct 23 '22 05:10

rveach