如何使用渐变pmd插件将自定义的java pmd规则添加到渐变项目中



在处理项目arch4u pmd期间,我们制定了几个基于java的pmd规则,在基于XML的规则集our-rules.xml中进行了配置,并将其作为普通的java lib/工件(io.github.abc:my-pmd-rules:0.1.0(发布到我们的工件存储库中。工件结构如下:

> unzip -l my-pmd-rules-0.1.0.jar   
Archive:  my-pmd-rules-0.1.0.jar   
Length      Date    Time    Name
---------  ---------- -----   ----
0  02-15-2022 00:24   META-INF/
139  02-15-2022 00:24   META-INF/MANIFEST.MF
0  02-15-2022 00:24   io/
0  02-15-2022 00:24   io/github/
0  02-15-2022 00:24   io/github/rules/
...
4781  02-15-2022 00:24   io/github/rules/MissingMandatoryAnnotation.class
...
1138  02-15-2022 00:24   io/github/rules/our-rules.xml
...

我们如何使用pmd插件将它们添加到Gradle项目中?

我们必须处理以下材料/问题/答案:

  1. https://stackoverflow.com/search?page=2&tab=相关性&q=pmd%20classpath
  2. ClassNotFoundException:对PMD规则集使用自定义java规则
  3. Gradle';s PMD插件:什么是可接受的参数
  4. 向PMD添加规则集
  5. 在PMD中添加自定义规则-找不到类问题
  6. https://discuss.gradle.org/t/pmd-ruleset-not-available-in-classpath/7201
  7. https://discuss.gradle.org/t/custom-rules-with-pmd-plugin/5859/4
  8. 如何在Sonar中配置PMD辅助类路径
  9. https://docs.gradle.org/current/userguide/pmd_plugin.html
  10. https://github.com/gradle/gradle/blob/master/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/PmdPlugin.java
  11. Gradle的自定义PMD规则也不起作用
    tasks.withType(Pmd) {
    pmdClasspath += file("path/to/rules.jar")
    }
    

在Gradle pmd插件的官方文档中,有一个依赖部分在高层解释了这一方面,但没有一个真正的例子:

  1. pmd-要使用的PMD库
  2. pmdAux-分析期间可用于类型解析的附加库。如果PMD抱怨缺少类,这可能会很有用

为了将自定义java规则添加到您的项目

  1. 使用Gradle插件

    apply plugin: 'pmd'
    repositories {
    mavenCentral() // if your rules in Maven Central
    mavenLocal()   // if your rules is in .m2 folder 
    maven {
    // if your rules are in some custom/self-hosted artifacts repository like Nexus
    }
    }
    dependencies {
    ...
    pmd "io.github.abc:my-pmd-rules:0.1.0" 
    pmd "commons-io:commons-io:2.11.0"     // required dependency by pmd engine
    ...
    }
    pmd {
    consoleOutput = true
    ruleSetFiles = files("io/github/rules/our-rules.xml")    // exactly path as in your lib classpath
    ruleSets = []                                            // Keep it as is, workaround for pmd
    }
    
  2. 使用Maven插件

    ...
    <build>
    <plugins>
    ...
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.15.0</version>
    <executions>
    <execution>
    <phase>test</phase>
    <goals>
    <goal>check</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <printFailingErrors>true</printFailingErrors>
    <rulesets>
    ...
    <ruleset>io/github/rules/our-rules.xml</ruleset> <!-- exactly path as in your lib classpath -->
    ...
    </rulesets>
    <excludeRoots>
    <excludeRoot>target/generated-sources/</excludeRoot>
    </excludeRoots>
    </configuration>
    <dependencies>
    <!-- your custom rules -->
    <dependency>
    <groupId>io.github.abc</groupId>
    <artifactId>my-pmd-rules</artifactId>
    <version>0.1.0</version>
    </dependency>
    </dependencies>
    </plugin>
    ...
    </plugins>
    </build>
    ...
    
  3. 如果您已经有了一个规则集,并且只想包含自定义Java规则(来自库(,您可以直接在规则集xml中定义它,如下所示:

    <!-- Define a rule -->
    <rule name="TheRuleName"
    language="java"
    externalInfoUrl="https://link.to.your.rule.official.docs.com"
    message="The violation message regarding your rule"
    class="io.github.rules.MissingMandatoryAnnotation">
    <priority>3</priority>
    <properties>
    ...
    </properties>
    </rule>
    

最新更新