Maven Checkstyle configLocation被忽略了吗?

Hea*_*vyE 16 checkstyle maven

尝试根据apache站点上的指导原则在 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.");
   }
}
Run Code Online (Sandbox Code Playgroud)

一个空的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>
Run Code Online (Sandbox Code Playgroud)

和根据指南中的规格的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>
Run Code Online (Sandbox Code Playgroud)

运行'mvn -X checkstyle:checkstyle'是使用sun_checks.xml(默认)而不是使用my_checks.xml文件中的配置,这可以通过生成的checkstyle错误和调试输出看到(例如'[DEBUG] ] request.getConfigLocation()config/sun_checks.xml').

我知道my_checks.xml是有效的,因为可以使用Carboni在之前的堆栈溢出帖子中概述的策略在属性中更改checkstyle.config.location ,但这会导致移动到多模块项目时出现问题并且与'官方的'apache maven checkstyle说明.

Blu*_*ell 24

是的,我发现apache网站上的指南已经过时了.通过拖网互联网和一起废弃我已经设法解决了这个问题.

移动到<build>标签工作了一段时间,但当更新到最新的checkstyle时,它再次被忽略.我发现我必须设置一个属性:

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

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

并从这里的源代码中获取:https:
//github.com/blundell/CreateYourOwnCheckStyleCheck


Dav*_*ton 12

这适用于我:

  1. 我放在my_checks.xml根级(平行pom.xml),和
  2. 将插件包装在<build>元素中(而不是<reporting>)

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

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

请参考使用文档,具体如下:

要专门配置Checkstyle插件,您需要将其添加到<build>pom.xml 的部分中,如下面的示例所示.

<reporting>用途是mvn site.

  • 谢谢Dave Newton!使用<reporting>而不是<build>是问题所在.如果Maven Checkstyle指南给出了checkstyle示例而不是站点示例,那将会很有帮助. (2认同)