简体   繁体   English

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

[英]how to externalise the checkstyle config for maven-checkstyle-plugin

I'm trying to make the maven-checkstyle-plugin use the same config file for all our projects.我试图让 maven-checkstyle-plugin 对我们所有的项目使用相同的配置文件。

I've tried a couple of ways, but non of them was effective.我尝试了几种方法,但没有一种是有效的。

The only thing that seems to work is when i place the config file at the root of my maven-project and then use the name as configLocation configuration parameter in the pom.xml唯一似乎有效的是当我将配置文件放在我的 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>

I've tried specifying an absolute disk-path, but that doesn't seem to work.我试过指定绝对磁盘路径,但这似乎不起作用。 (Considering the endgoal is to have jenkins do the checkstyle this seemed a valid option if the file would be on the jenkins server at the specified location) (考虑到最终目标是让 jenkins 执行检查样式,如果文件位于指定位置的 jenkins 服务器上,这似乎是一个有效的选项)

I've also tried making a seperate jar-file only containing the xml-file and then using this as a dependency.我也试过制作一个单独的 jar 文件,只包含 xml 文件,然后将其用作依赖项。 (This would also centralise the config in 1 location and prevent project specific deviations.) Unfortunately this also doesn't work. (这也会将配置集中在 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. [错误] 无法在项目 jenkins-sandbox-project 上执行目标 org.apache.maven.plugins:maven-checkstyle-plugin:2.10:checkstyle (default-cli):Checkstyle 报告生成中发生错误。 Failed during checkstyle execution: Unable to find configuration file at location my-checkstyle-checker.xml: Could not find resource 'my-checkstyle-checker.xml'. checkstyle 执行期间失败:无法在 my-checkstyle-checker.xml 位置找到配置文件:找不到资源“my-checkstyle-checker.xml”。 -> [Help 1] -> [帮助 1]

Is there anyone that can tell me what i'm doing wrong here?有没有人可以告诉我我在这里做错了什么?

It seems it only knows about the files in the same location as where the maven command was started.它似乎只知道与启动 maven 命令的位置相同的文件。

  • maven-checkstyle-plugin version: 2.10 maven-checkstyle-插件版本:2.10
  • maven command: mvn checkstyle:checkstyle maven 命令:mvn checkstyle:checkstyle

Create a separate Maven project, that contains just the Checkstyle configuration.创建一个单独的 Maven 项目,其中仅包含 Checkstyle 配置。 In my case I called this project checkstyle-config and it contains the following:在我的例子中,我称这个项目为checkstyle-config ,它包含以下内容:

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

The POM file for this project is trivial:这个项目的 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>

Build it, so that it gets installed.构建它,以便它被安装。 Then use it as a dependency for your Checkstyle execution, eg:然后将其用作 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>

I had a similar problem.我有一个类似的问题。 I solved it with the following configuration.我用以下配置解决了它。

${project.basedir}/src/main/resources/checkstyle.xml ${project.basedir}/src/main/resources/checkstyle.xml

Note: my checkstyle file is located in "src/main/resources"注意:我的 checkstyle 文件位于“src/main/resources”

I also had some issues defining the location in my plugin configuration, but was able to get this working by overriding a Maven property that the plugin uses, checkstyle.config.location.我在定义插件配置中的位置时也遇到了一些问题,但能够通过覆盖插件使用的 Maven 属性 checkstyle.config.location 来使其工作。 See example below which works with a multi-module maven project and requires very little overhead.请参见下面的示例,该示例适用于多模块 Maven 项目并且需要很少的开销。

<checkstyle.config.location>${project.parent.basedir}/my_checks.xml</checkstyle.config.location>

On my case the order of dependencies is the key, this is my pom就我而言,依赖顺序是关键,这是我的 pom

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-wkt</artifactId>
    <version>${geotools.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-geometry</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-swing</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-shapefile</artifactId>
    <version>${geotools.version}</version>
</dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设计用于 YAML 文件验证的 maven-checkstyle-plugin? - How to design maven-checkstyle-plugin for YAML file validation? Eclipse Checkstyle插件显示与maven-checkstyle-plugin不同的结果 - Eclipse Checkstyle Plugin showin different results that maven-checkstyle-plugin 如何配置Eclipse Checkstyle插件以使用与maven-checkstyle-plugin相同的规则 - How to configure the Eclipse Checkstyle plugin to use the same rules used by the maven-checkstyle-plugin 将Maven-checkstyle-plugin集成到构建中时遇到麻烦 - trouble integrating maven-checkstyle-plugin in to build maven-checkstyle-plugin 不适用于 macOS 上的 google_checks.xml - maven-checkstyle-plugin not working with google_checks.xml on macOS 使用maven-checkstyle-plugin时如何抑制maven输出中的警告? - How to suppress warnings in maven output when using maven-checkstyle-plugin? NetBeans(Maven)项目:checkstyle-beans与maven-checkstyle-plugin - NetBeans (maven) project: checkstyle-beans vs maven-checkstyle-plugin 我可以使用checkstyle,maven-checkstyle-plugin和google_checks.xml启用抑制吗? - Can I enable suppressions with checkstyle, the maven-checkstyle-plugin and google_checks.xml? 使用带有maven-checkstyle-plugin的checkstyle / google_checks.xml时出错 - Error using checkstyle/google_checks.xml with maven-checkstyle-plugin maven-checkstyle-plugin:3.0.0:check: checkstyle 配置期间失败:无法初始化模块 StrictDuplicateCode - maven-checkstyle-plugin:3.0.0:check: Failed during checkstyle configuration: cannot initialize module StrictDuplicateCode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM