【问题标题】:Maven checkstyle plugin with properties具有属性的 Maven checkstyle 插件
【发布时间】:2013-09-12 09:49:49
【问题描述】:

我正在运行 Maven 3.0.5 并尝试使用 maven checkstyle 插件,但我无法让它正确解析我的配置文件。在我的 POM 的报告部分,我有

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <propertyExpansion>suppressions.file=${basedir}/suppressions.xml</propertyExpansion>
        <configLocation>file:///${basedir}/../buildTools/checkstyle/checkstyle-5.0-checks.xml</configLocation>
    </configuration>
</plugin>

在我的 checkstyle.xml 文件中,我将抑制过滤器定义为

<module name="SuppressionFilter">
<!--     <property name="file" value="${checkstyle.suppressions.file}"/>
-->
   <property name="file" value="${suppressions.file}"/>
</module>

当我运行 checkstyle:checkstyle 目标时,我收到一个错误,即未定义 suppresss.file。我可以让它在 Eclipse 或 ant 下运行,但是当我尝试将它与 maven 一起使用时,我无法设置要设置的属性。我还尝试使用 checkstyle.suppressions.file 值并使用 propertyExpansion 标记或 suppresssFile 标记进行设置。

有没有办法在 Maven 之外做到这一点。我正在寻找的最终结果是能够在 Eclipse、Ant 和 Maven 中使用相同的规则文件。这将是我要指出的一个通用文件。同时,我希望能够在每个单独的项目中放置一个 suppresss.xml 文件,以允许项目控制抑制过滤器。

【问题讨论】:

    标签: maven checkstyle


    【解决方案1】:

    首先我建议不要像configLocation 那样使用绝对文件名。更好的解决方案是使用一个包含适当配置文件的工件,比如有一个单独的项目让我们称之为:可以版本化的 checkstyle 等。

    checkstyle
      |-- src
      |   `-- main
      |       `-- resources
      |               |-- checkstyle.xml
      |               `-- LICENSE.TXT
      `-- pom.xml
    
    
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example.checkstyle</groupId>
      <artifactId>checkstyle</artifactId>
      <version>1.0</version>
      <name>checkstyle configuration</name>
    </project>
    

    创建mvn install 后,您可以使用如下配置:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>whatever-project</artifactId>
      <version>1.0</version>
      <packaging>pom</packaging>
      <name>My Project </name>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.10</version>
            <dependencies>
              <dependency>
                <groupId>com.example.checkstyle</groupId>
                <artifactId>checkstyle</artifactId>
                <version>1.0</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
      <reporting>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.10</version>
            <configuration>
              <configLocation>checkstyle.xml</configLocation>
              <headerLocation>LICENSE.txt</headerLocation>
            </configuration>
          </plugin>
        </plugins>
      </reporting>
    

    【讨论】:

    • 这并没有回答如何获取抑制文件的问题。我正在考虑稍后将主配置放入 jar 中,但需要弄清楚如何在 cs-eclipse 中使用它。我不能为此使用 maven eclipse 插件,因为它可能处于 ant 控制之下。我仍然希望能够使用基于项目的抑制文件
    【解决方案2】:

    好的,我是从某个网站开始的,该网站声明如果您在报告部分添加配置,它将被构建插件部分拾取。这不起作用。通过为检查添加 jar 文件的依赖项,我还有更多工作要做。我发现我必须指定抑制文件,即使我在检查文件中定义了它。我还必须使用指向内部抑制文件的 propertyExpansion 传递变量(我想要这个,不必再次指定抑制文件)。我现在有我的构建插件节作为

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.10</version>
        <configuration>
          <propertyExpansion>suppressions.file=${basedir}/suppressions.xml</propertyExpansion>
          <configLocation>file:///${basedir}/../buildTools/checkstyle/checkstyle-5.0-checks.xml</configLocation>
          <suppressionsFile>${basedir}/suppressions.xml</suppressionsFile>
        </configuration>
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2016-02-13
      • 2011-09-18
      • 2021-01-05
      相关资源
      最近更新 更多