【问题标题】:Maven Checkstyle configLocation ignored?Maven Checkstyle configLocation 被忽略了吗?
【发布时间】:2012-02-17 00:09:57
【问题描述】:

尝试根据guidelines on the apache site 在 maven pom 中设置自定义 checkstyle 配置在非常简单的情况下不起作用。

我创建了一个项目MyProject,使用Maven推荐的目录布局(即src/main/java/、src/main/resources),单个文件MyClass.java:

package com.myproject;

public class MyClass {

   public static void main(String[] args) {
      System.out.println("This line is longer than 80 characters which returns an error in sun_checks.xml, however my_checks.xml allows for much longer lines and will not return a long line error.");
   }
}

一个空的 checkstyle 文件,my_checks.xml:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
    <property name="severity" value="warning"/>
    <module name="TreeWalker">
    </module>
</module>

以及根据指南中的规范的 pom 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myproject</groupId>
    <artifactId>A_Project</artifactId>
    <name>A Project</name>
    <version>1.0.0</version>

    <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.8</version>
        <configuration>
          <configLocation>my_checks.xml</configLocation>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
</project>

运行 'mvn -X checkstyle:checkstyle' 正在使用 sun_checks.xml(默认)而不是使用 my_checks.xml 文件中的配置,由此产生的 checkstyle 错误和调试输出(例如。 '[DEBUG] request.getConfigLocation() config/sun_checks.xml').

我知道 my_checks.xml 是有效的,因为 checkstyle.config.location 可以使用 Carboni 在 previous stack overflow post 中概述的策略在属性中进行更改,但这会在迁移到多模块项目时导致问题,并且不同于'官方' apache maven checkstyle 说明。

【问题讨论】:

  • 您的配置 xml 到底在哪里?

标签: maven checkstyle


【解决方案1】:

是的,我发现 apache 网站上的指南已过时。通过在互联网上搜索和拼凑,我设法解决了这个问题。

移动到&lt;build&gt; 标记工作了一段时间,但是当更新到最新的检查样式时,它又被忽略了。我发现我必须设置一个属性:

<project ...>

 ....

  <properties>
    <checkstyle.config.location>properties/checkstyle-configuration.xml</checkstyle.config.location>
  </properties>

  <build>
    ...

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.9.1</version>
      </plugin>
    </plugins>
  </build>

  ...

</project>

这是我如何创建自己的 checkstyle 自定义检查的示例的一部分,可在此处找到:
http://blog.blundellapps.com/create-your-own-checkstyle-check/

并取自此处的源代码:
https://github.com/blundell/CreateYourOwnCheckStyleCheck

【讨论】:

  • 谢谢!这也适用于其他配置属性:&lt;checkstyle.suppressions.location&gt; 注意:我必须将 file: 添加到位置
【解决方案2】:

这对我有用:

  1. 我将my_checks.xml 放在根级别(与pom.xml 平行),并且
  2. 将插件包装在 &lt;build&gt; 元素中(而不是 &lt;reporting&gt;

通过这些更改,我看到以下内容:

[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-checkstyle-plugin:2.8:checkstyle' with basic configurator -->
[DEBUG]   (f) cacheFile = /home/dave/tech/lang/java/web/struts/playground/so/s231_01/target/checkstyle-cachefile
[DEBUG]   (f) configLocation = my_checks.xml
... etc ...
[DEBUG] request.getConfigLocation() my_checks.xml
[DEBUG] The resource 'my_checks.xml' was not found with resourceLoader
... etc ...
[DEBUG] The resource 'my_checks.xml' was not found with resourceLoader org.codehaus.plexus.resource.loader.URLResourceLoader.
[DEBUG] The resource 'my_checks.xml' was not found with resourceLoader org.codehaus.plexus.resource.loader.JarResourceLoader.
[DEBUG] The resource 'my_checks.xml' was found as /home/dave/tech/lang/java/web/struts/playground/so/s231_01/my_checks.xml.

请参考usage documentation,具体:

要专门配置 Checkstyle 插件,您需要将其添加到 pom.xml 的 &lt;build&gt; 部分,如下面的示例所示。

&lt;reporting&gt; 用于mvn site

【讨论】:

  • 感谢戴夫牛顿!使用 而不是 是问题所在。如果 Maven Checkstyle 指南提供了 checkstyle 示例而不是站点示例,那将会很有帮助。
  • @HeavyE 没问题 :) 从技术上讲确实如此,goals docs 确实区分了插件和报告目标,但如果您对术语不太满意,它有点不透明(因为是很多 Maven 文档 ;)
  • 如果您使用的是 Maven 3,请参阅 cwiki.apache.org/MAVEN/… 部分中的插件配置不再影响 POM 的 部分中的插件。”对于 Maven 2,您可能需要在报告和构建之间复制位。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2020-07-07
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
相关资源
最近更新 更多