【问题标题】:How can I configure Checkstyle in Maven?如何在 Maven 中配置 Checkstyle?
【发布时间】:2012-09-12 20:35:21
【问题描述】:

我正在使用 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:checktyle 还是你的 IDE?在所有模块上?
  • @yodamad 是的,在所有模块上..
  • @yodamad 我不知道checkstyle.xml的路径怎么写

标签: java maven configuration checkstyle


【解决方案1】:

为什么你不把你的 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 中
  • 好的,你可以试试这个变量${project.parent.basedir}而不是${basedir}
  • 如果您保留原来的配置,只需将${basedir}/../ModulB/folder/checkstyle.xml 替换为${project.parent.basedir}/../ModulB/folder/checkstyle.xml
  • 阿夫。您什么时候遇到问题:在源代码的 IDE 中或在生成 CS 报告时执行 mvn checkstyle:checkstyle ?
  • 我在父 pom 或模块 pom 上执行 mvn checkstyle:checkstyle
【解决方案2】:

要让它获取您的自定义配置,您需要将您的文件声明为属性:

<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

【讨论】:

猜你喜欢
  • 2013-11-10
  • 2015-07-01
  • 2021-02-14
  • 2012-01-11
  • 2019-03-25
  • 2017-10-31
  • 2012-01-03
  • 2015-12-02
相关资源
最近更新 更多