java - 如何在 Maven 中配置 Checkstyle?

标签 java maven configuration checkstyle

我正在使用 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>
...

最佳答案

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

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

取 self 关于如何在此处创建自定义 CheckStyle 检查的教程:
http://blog.blundellapps.com/create-your-own-checkstyle-check/

源代码在这里:
https://github.com/blundell/CreateYourOwnCheckStyleCheck

关于java - 如何在 Maven 中配置 Checkstyle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12513277/

相关文章:

java - 如何为自定义日志记录解决方案正确创建性能测试?

java - 由于错误无法在eclipse中导入maven项目

flink 进程中 Java 反射的 java.lang.NoSuchMethodError 异常

c++ - 应用程序无法获取命令参数

java - 如何在 log4j2 中生成标题行

configuration - CC使变量可用

java - 类中的对象数组

java - Android:单击时更改按钮颜色

java - 迄今为止未指定格式的字符串

maven - Jenkins Maven 存储库中的备份文件夹