Maven Checkstyle configLocation被忽略应该怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (112)

试图根据apache网站上的指南在 maven pom中设置一个自定义checkstyle配置,但不简单。

我使用Maven推荐的目录布局(即src / main / java /,src / main / resources)创建了一个项目MyProject,它是一个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')。

提问于
用户回答回答于

这适用于我时:

  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.

请参考使用文档,具体为:

要专门配置Checkstyle插件,需要将其添加到<build>pom.xml 的部分

用户回答回答于

必须设置一个属性:

<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>

扫码关注云+社区

领取腾讯云代金券