忽略gradle pmd插件中的优先级3,4,5违规

时间:2014-07-30 07:44:03

标签: gradle pmd

我希望我的gradle pmd插件只有在存在优先级为1和2的违规时才会失败。如果只有优先级为3,4和5的违规行为,我不想让构建失败。是否存在一种实现相同的方法?我浏览了gradle pmd插件文档,但在那里找不到任何内容。

目前,我的build.gradle有以下代码:

pmd {
    ruleSetFiles = files('src/test/resources/ruleset.xml')
}

1 个答案:

答案 0 :(得分:0)

您可以define a config file包含您希望运行的规则。您可以将此文件提供给pmd gradle插件。

这里有一个示例文件(MyRules.xml):

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ruleset name="PMD.rul" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

  <description>This ruleset was created from PMD.rul</description>

  <!-- We'll use the entire 'strings' ruleset -->
  <rule ref="rulesets/java/strings.xml"/>

  <!-- Here's some rules we'll specify one at a time -->
  <rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable"/>
  <rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField"/>
  <rule ref="rulesets/java/imports.xml/DuplicateImports"/>
  <rule ref="rulesets/java/basic.xml/UnnecessaryConversionTemporary"/>

  <!-- We want to customize this rule a bit, change the message and raise the priority  -->
  <rule
   ref="rulesets/java/basic.xml/EmptyCatchBlock"
   message="Must handle exceptions">
    <priority>2</priority>
  </rule>

</ruleset>

命令行参数如下所示:

pmd {
    ruleSetFiles = files('path/to/MyRules.xml')
}

通过这种方式,您可以定义构建失败的规则违规行为。