如何在Maven中配置Checkstyle?

7

我正在使用Maven。

我有一个父模块和其他一些子模块。

它们看起来像这样:

PARENT
├── pom.xml
├── ModulA
|   └── pom.xml
└── ModulB
    ├── pom.xml
    └── folder
        └── checkstyle.xml

我试图使用我的规则替换原有规则,但是它忽略了我的规则。我已经将插件添加到父级pom.xml文件中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.9.1</version>
    <configuration>
        <includeTestSourceDirectory>true</includeTestSourceDirectory>
        <configLocation>
            ${basedir}/../ModulB/folder/checkstyle.xml                  
        </configLocation>
    </configuration>
</plugin>

问题出在哪里?

编辑:

mvn checkstyle:checkstyle -X 命令的结果如下:

...
    <configuration>
      <cacheFile default-value="${project.build.directory}/checkstyle-cachefile"/>
      <configLocation default-value="config/sun_checks.xml">${checkstyle.config.location}</configLocation>
      <consoleOutput default-value="false"/>
      <enableFilesSummary default-value="true">${checkstyle.enable.files.summary}</enableFilesSummary>
      <enableRSS default-value="true">${checkstyle.enable.rss}</enableRSS>
      <enableRulesSummary default-value="true">${checkstyle.enable.rules.summary}</enableRulesSummary>
      <enableSeveritySummary default-value="true">${checkstyle.enable.severity.summary}</enableSeveritySummary>
      <encoding default-value="${project.build.sourceEncoding}">${encoding}</encoding>
      <excludes>${checkstyle.excludes}</excludes>
      <failsOnError default-value="false"/>
      <format default-value="sun"/>
      <headerFile>${basedir}/LICENSE.txt</headerFile>
      <headerLocation default-value="LICENSE.txt">${checkstyle.header.file}</headerLocation>
      <includeTestSourceDirectory default-value="${false}"/>
      <includes default-value="**/*.java">${checkstyle.includes}</includes>
      <linkXRef default-value="true">${linkXRef}</linkXRef>
      <outputDirectory default-value="${project.reporting.outputDirectory}"/>
      <outputFile default-value="${project.build.directory}/checkstyle-result.xml">${checkstyle.output.file}</outputFile>
      <outputFileFormat default-value="xml">${checkstyle.output.format}</outputFileFormat>
      <project default-value="${project}"/>
      <propertiesLocation>${checkstyle.properties.location}</propertiesLocation>
      <skip default-value="false">${checkstyle.skip}</skip>
      <sourceDirectory default-value="${project.build.sourceDirectory}"/>
      <suppressionsFileExpression default-value="checkstyle.suppressions.file">${checkstyle.suppression.expression}</suppressionsFileExpression>
      <suppressionsLocation>${checkstyle.suppressions.location}</suppressionsLocation>
      <testSourceDirectory default-value="${project.build.testSourceDirectory}"/>
      <xrefLocation default-value="${project.reporting.outputDirectory}/xref"/>
    </configuration>
...

它什么时候会忽略你的规则:mvn checkstyle:checkstyle 还是你的 IDE?在所有模块上吗? - yodamad
@yodamad 是的,在所有模块上都是如此。 - Kayser
@yodamad 我不知道如何编写路径以检查 checkstyle.xml。 - Kayser
2个回答

15

为了使其采用您的自定义配置,您需要将文件声明为属性:

<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/xsd/maven-4.0.0.xsd">
  ...

  <properties>
    <checkstyle.config.location><!-- file location --></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


2
你是对的。官方文档 http://maven.apache.org/plugins/maven-checkstyle-plugin/ 和 http://maven.apache.org/plugins/maven-checkstyle-plugin/examples/custom-checker-config.html 表明,你需要使用 configLocation 参数来配置,但这对我不起作用,而设置 <checkstyle.config.location> 属性则非常好用。谢谢。 - Glenn Lawrence
当我终于意识到@GlennLawrence所说的是真的时,我简直要抓狂了! - gustavohenke

5
为什么不把您的checkstyle配置文件放在父项目的目录中,如src/main/resources并在父级pom.xml中参数化插件,类似于这样?
<!-- Checkstyle -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>RELEASE</version>
    <configuration>
        <configLocation>src/main/resources/checkstyle.xml</configLocation>                   
    </configuration>
</plugin>

子项目应该可以访问此配置。

这是项目结构。所有配置文件都在ModulB中。 - Kayser
1
好的,你可以尝试使用变量${project.parent.basedir}代替${basedir} - yodamad
如果您保留原始配置,只需将${basedir}/../ModulB/folder/checkstyle.xml替换为${project.parent.basedir}/../ModulB/folder/checkstyle.xml - yodamad
奇怪。你能发布完整的pom.xml吗?而且你的插件版本似乎很旧,尝试更新到最新版本(2.9.1)。然后运行“mvn checkstyle:checkstyle -X”以获取详细信息。 - yodamad
类似这样的信息:使用基本配置器配置mojo 'org.apache.maven.plugins:maven-checkstyle-plugin:2.9.1:checkstyle' --> (f) configLocation = config/sun_checks.xml 或者 request.getConfigLocation() config/sun_checks.xml - yodamad
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接