Maven pmd插件 - 排除/禁用规则

时间:2014-03-27 14:20:32

标签: maven maven-3 pmd

我正在尝试使用maven-pmd-plugin(在多模块maven项目中)从pmd中排除某个规则。

方法

使用excludeFromFailureFile http://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html

理想情况下,我想要为整个产品(基于父包)排除此规则,但是,我正在测试某个特定类 - 即使这不起作用。

环境

Java 7,Maven 3.0.3

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-pmd-plugin</artifactId>
       <version>3.1</version>
       <executions>
         <execution>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-pmd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
          <execution>
             <goals>
                 <goal>cpd-check</goal>
             </goals>
          <!-- Added explicit execution Id to avoid the below problem -->
      <!-- 'build.pluginManagement.plugins.plugin[org.apache.maven.plugins:maven-pmd-plugin].executions.execution.id' must be unique but found duplicate execution with id default @ line 1423, column 36 -->
            <id>cpd-check</id>
          </execution>
   </executions>

exclude-pmd.properties的内容

mycompany.project.classA=UselessParentheses

1 个答案:

答案 0 :(得分:4)

排除规则的最简单方法是提供自己的规则集文件。您可以在this问题的答案中查看在何处查找默认规则集文件。如果您使用的是Sonar,则可以使用固定链接检索文件。

将文件复制到父模块,然后您可以自定义它,删除要排除的规则,并使用以下配置:

父级 pom.xml中:

...
<properties>
    <main.basedir>${project.basedir}</main.basedir> 
    <!-- Some child module could set this to true to skip PMD check -->
    <skip.pmd.check>false</skip.pmd.check>
</properties>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <!-- This is the version I'm using -->
    <version>2.7.1</version>
    <configuration>
        <rulesets>
            <ruleset>${main.basedir}/path/to/pmd-rules.xml</ruleset>
        </rulesets>
        <skip>${skip.pmd.check}</skip>
    </configuration>
    <executions>
        <execution>
            <id>pmd-config</id>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

在子模块 pom.xml中:

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
</properties>

在某些模块中要跳过(即包含生成的代码):

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
    <skip.pmd.check>true</skip.pmd.check>
</properties>

希望这有帮助!