maven - 如何外部化 maven-checkstyle-plugin 的 checkstyle 配置

标签 maven plugins configuration checkstyle

我试图让 maven-checkstyle-plugin 对我们所有的项目使用相同的配置文件。

我尝试了几种方法,但没有一种是有效的。

唯一似乎有效的是当我将配置文件放在我的 maven 项目的根目录下,然后在 pom.xml 中使用该名称作为 configLocation 配置参数时

                <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.10</version>

                <configuration>
                    <configLocation>my-checkstyle-checker.xml</configLocation>
                </configuration>
            </plugin>

我试过指定绝对磁盘路径,但这似乎不起作用。
(考虑到最终目标是让 jenkins 执行 checkstyle,如果文件位于指定位置的 jenkins 服务器上,这似乎是一个有效的选项)

我还尝试制作一个仅包含 xml 文件的单独 jar 文件,然后将其用作依赖项。 (这也会将配置集中在 1 个位置并防止项目特定偏差。)不幸的是,这也不起作用。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.10:checkstyle (default-cli) on project jenkins-sandbox-project: An error has occurred in Checkstyle report generation. Failed during checkstyle execution: Unable to find configuration file at location my-checkstyle-checker.xml: Could not find resource 'my-checkstyle-checker.xml'. -> [Help 1]



有没有人可以告诉我我在这里做错了什么?

它似乎只知道与 maven 命令启动位置相同的文件。
  • maven-checkstyle-plugin 版本:2.10
  • maven 命令:mvn checkstyle:checkstyle
  • 最佳答案

    创建一个单独的 Maven 项目,其中仅包含 Checkstyle 配置。就我而言,我称这个项目为 checkstyle-config它包含以下内容:

    checkstyle-config/src/main/resources/checkstyle.config.xml
    checkstyle-config/src/main/resources/checkstyle.suppressions.xml
    checkstyle-config/pom.xml
    

    这个项目的 POM 文件很简单:
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.totaalsoftware.incidentmanager</groupId>
      <artifactId>checkstyle-config</artifactId>
      <version>2.0.0-SNAPSHOT</version>
    </project>
    

    构建它,以便它被安装。然后将其用作 Checkstyle 执行的依赖项,例如:
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.totaalsoftware.incidentmanager</groupId>
            <artifactId>checkstyle-config</artifactId>
            <version>2.0.0-SNAPSHOT</version>
          </dependency>
        </dependencies>
        <configuration>
          <configLocation>checkstyle.config.xml</configLocation>
          <suppressionsLocation>checkstyle.suppressions.xml</suppressionsLocation>
    
          ... other configuration ...
    
        </configuration>
      </plugin>
    

    关于maven - 如何外部化 maven-checkstyle-plugin 的 checkstyle 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19682455/

    相关文章:

    maven - Jenkins:如何从Nexus check out Artifact 并在Tomcat上部署-

    java - 独立 JAR 的行为与从 IDE 中启动时的行为不同

    java.lang.NoClassDefFoundError : scala/Predef$any2stringadd$ 错误

    jQuery.live() 无法在插件内部工作

    javascript - Windows 7 中的 NPAPI 插件问题

    configuration - 读取自定义配置部分时出错 : No parameterless constructor defined for this object

    java - NoClassDefFoundError : Mockito Bytebuddy

    java - 内联网应用程序的签名小程序

    在 IIS 6.0 中使用自签名 SSL 的 WCF,在客户端出现奇怪的错误

    asp.net-core - 在 .NetCore 3.0 中配置 ApiVersionDescriptions 而不在启动时使用构建服务提供程序