'How to add custom java pmd rules to gradle project using gradle-pmd-plugin?

During working on project arch4u-pmd we made several java-based pmd rules, configured them in XML-based ruleset our-rules.xml, and published it as a plain java lib/artifact (io.github.abc:my-pmd-rules:0.1.0) to our artifacts repository. The artifact structure looks like this:

> 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
     ...

How we can add them to the Gradle project using pmd plugin?

We have to process the following materials/questions/answers:

  1. https://stackoverflow.com/search?page=2&tab=Relevance&q=pmd%20classpath
  2. ClassNotFoundException: Using custom java rule for PMD ruleset
  3. Gradle's PMD plugin: what are acceptable arguments?
  4. Adding a ruleset to PMD?
  5. Adding custom rules in PMD - class not found issue
  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. How to configure PMD Auxiliary classpath in Sonar
  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. Custom PMD rule with Gradle also don't work
    tasks.withType(Pmd) {
       pmdClasspath += file("path/to/rules.jar")
    }
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source