How can I configure checkstyle in maven?

自古美人都是妖i 提交于 2020-01-23 07:52:51

问题


I am using Maven
I have a parent module and some other modules. They look like:

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

I tried to replace the rules with my own rules. But it ignores my rules. I added the plug-in to parent 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>

Where is the problem?

EDIT:


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

回答1:


For it to pick up your custom configuration, you need to declare your file as a property:

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

Taken from my tutorial on how to create a custom CheckStyle check here:
http://blog.blundellapps.com/create-your-own-checkstyle-check/

Source code here:
https://github.com/blundell/CreateYourOwnCheckStyleCheck




回答2:


Why you don't put your checkstyle config in a directory in parent project like src/main/resources and parametrized the plugin in parent pom.xml like this

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

Child projects should have access do this configuration



来源:https://stackoverflow.com/questions/12513277/how-can-i-configure-checkstyle-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!