Java google checkstyle Maven

Mag*_*irr 12 java checkstyle maven

我正在尝试使用以下配置将我的Maven项目配置为使用google java check样式:

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>
Run Code Online (Sandbox Code Playgroud)

它起初似乎运行mvn checkstyle:check良好.但经过几次运行后,我开始收到以下错误:

[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]
Run Code Online (Sandbox Code Playgroud)

那是什么意思?为什么它只发生一些时间,我该如何摆脱它?

rve*_*ach 14

在com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck中的Acceptable tokens列表中找不到令牌"METHOD_REF"

您正尝试使用旧版Checkstyle的较新配置.

在该配置https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml是在master其取决于CheckStyle的快照版本.

如果您使用谷歌配置而不做任何修改,则需要使用checkstyle中嵌入的那个.请参阅/sf/answers/2484045581/

否则,您可以集成更新版本的checkstyle以与maven一起使用.请参阅/sf/answers/1915137521/


小智 10

我使用的是maven-checkstyle-plugin版本3.0.0(现在是最新版本),我仍然遇到错误.我通过向插件添加以下依赖项来解决它.

 <dependency>
     <groupId>com.puppycrawl.tools</groupId>
     <artifactId>checkstyle</artifactId>
     <version>8.11</version>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

  • Maven-checkstyle-plugin默认使用Checkstyle 6.18,并且他们没有更改它,因为其项目需要Java 7+。 (3认同)