【问题标题】:Java google checkstyle MavenJava 谷歌检查样式 Maven
【发布时间】:2017-05-15 04:11:37
【问题描述】:

我正在尝试将我的 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>

一开始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]

这是什么意思?为什么它只发生几次,我该如何摆脱它?

【问题讨论】:

标签: java maven checkstyle


【解决方案1】:

在检查的可接受令牌列表中未找到令牌“METHOD_REF” com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck

您正在尝试使用较新的配置和旧版本的 Checkstyle。

https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml 的配置在 master 中,这取决于 checkstyle 的快照版本。

如果您使用的是未经任何修改的 google 配置,则需要使用嵌入在 checkstyle 中的配置。见https://stackoverflow.com/a/35486365/1016482

否则,您可以集成更新版本的 checkstyle 以与 maven 一起使用。见https://stackoverflow.com/a/27359107/1016482

【讨论】:

  • 如果我不使用maven,我怎么能得到它?
  • @moondaisy Checkstyle 嵌入了 google 配置。这可以通过命令行、maven 等进行访问。如果需要,您还可以从 JAR 中提取它并将其作为文件放在 HD 上。这完全取决于您如何调用 Checkstyle。如果您需要设置的详细信息,您可能需要提出一个新问题。
  • 我在 jar 中查找它似乎不存在,我正在使用 NetBeans 的 Check Style 插件,所以我需要单独的文件。
  • @moondaisy 最好打开自己的问题并标记 Checkstyle,因为它特定于 NetBeans 插件。
【解决方案2】:

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

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

【讨论】:

  • Maven-checkstyle-plugin 默认使用 Checkstyle 6.18,他们没有更改它,因为他们的项目需要 Java 7+。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
相关资源
最近更新 更多