【问题标题】:Custom PMD rule with Gradle使用 Gradle 自定义 PMD 规则
【发布时间】:2022-05-08 22:55:26
【问题描述】:

我想在使用 gradle 构建的企业项目中使用 gradle PMD 插件。

我有一个 pmd_rules.xml 文件已经可以使用,但是我不能添加自己的 java 规则(我得到一个类未找到异常)。我按照其网站上的教程进行操作。

我必须将自己的规则放在哪里才能让 gradle 和 PMD 识别它们?有人已经做过类似的事情了吗?

pmd.gradle:

apply from: rootProject.file("core/modules.gradle"), to : ext

if(project.name in (modules["modules"] +modules["modules"])){
    apply plugin: 'pmd'

    pmd {
        ignoreFailures = true
        ruleSetFiles = rootProject.files("../repo/pmd_rules.xml")
        sourceSets = [sourceSets.main,sourceSets.test]
        targetJdk = org.gradle.api.plugins.quality.TargetJdk.VERSION_1_7
        ruleSets = []
        toolVersion = "5.0.5"
    }
}

【问题讨论】:

    标签: gradle pmd


    【解决方案1】:
    tasks.withType(Pmd) {
        pmdClasspath += file("path/to/rules.jar")
    }
    

    【讨论】:

    【解决方案2】:

    对于最新的 Gradle pmd 插件,您需要执行以下操作:

    1. 将带有 pmd 规则的库添加到插件类路径 https://github.com/dgroup/arch4u-pmd#gradle-buildgradle
      dependencies {
         ...
         pmd "io.github.groupid:libid:versionid"  
         pmd "commons-io:commons-io:2.11.0" 
         ...
      }
      
    2. 从您的库中包含规则集 https://github.com/dgroup/arch4u-pmd#include-arch4u-pmd-rules-into-your-existing-custom-ruleset
      <?xml version="1.0"?>
      <ruleset name="pmd ruleset with your rules">
      
        ...
        <rule ref="io/github/path-to-ruleset-from-your-lib.xml"/>
        ...
      
      </ruleset>
      
      或在您的 https://github.com/dgroup/arch4u-pmd#reconfigure-a-rule
      <?xml version="1.0"?>
      <ruleset name="pmd ruleset with your rules">
      
        ...
        <!-- 2. Reconfigure rule with expected property -->
        <rule name="RuleFromYourLibrary" class="io.github.RuleFromYourLibrary">
          <priority>3</priority>
          <properties>
            ...
          </properties>
        </rule>
        ...
      
      </ruleset>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-21
      • 2013-04-08
      • 2017-03-10
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多