简体   繁体   English

如何使用 gradle-pmd-plugin 将自定义 java pmd 规则添加到 gradle 项目?

[英]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.在处理arch4u-pmd项目期间,我们制定了几个基于 java 的 pmd 规则,将它们配置在基于 XML 的规则our-rules.xml ,并将其发布为普通的 java lib/artifact ( io.github.abc:my-pmd-rules:0.1.0 ) 到我们的工件存储库。 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 ?我们如何使用pmd 插件将它们添加到 Gradle 项目中?

We have to process the following materials/questions/answers:我们必须处理以下材料/问题/答案:

  1. https://stackoverflow.com/search?page=2&tab=Relevance&q=pmd%20classpath https://stackoverflow.com/search?page=2&tab=Relevance&q=pmd%20classpath
  2. ClassNotFoundException: Using custom java rule for PMD ruleset ClassNotFoundException:对 PMD 规则集使用自定义 java 规则
  3. Gradle's PMD plugin: what are acceptable arguments? Gradle 的 PMD 插件:什么是可接受的 arguments?
  4. Adding a ruleset to PMD? 向 PMD 添加规则集?
  5. Adding custom rules in PMD - class not found issue 在 PMD 中添加自定义规则 - class 未找到问题
  6. https://discuss.gradle.org/t/pmd-ruleset-not-available-in-classpath/7201 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 https://discuss.gradle.org/t/custom-rules-with-pmd-plugin/5859/4
  8. How to configure PMD Auxiliary classpath in Sonar 如何在 Sonar 中配置 PMD 辅助类路径
  9. https://docs.gradle.org/current/userguide/pmd_plugin.html 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 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 使用 Gradle 的自定义 PMD 规则也不起作用
    tasks.withType(Pmd) { pmdClasspath += file("path/to/rules.jar") }

In official docs for Gradle pmd plugin there is a dependency section that explains this aspect on high-level without a real example:在 Gradle pmd插件的官方文档中,有一个依赖部分在没有实际示例的情况下从高层次解释了这方面:

  1. pmd - The PMD libraries to use pmd - 要使用的 PMD 库
  2. pmdAux - The additional libraries that are available for type resolution during analysis. pmdAux - 在分析期间可用于类型解析的附加库。 This might be useful if PMD complains about missing classes.如果 PMD 抱怨缺少类,这可能会有用。

In order to add the custom java rules to your project为了将自定义 java 规则添加到您的项目中

  1. Using Gradle plugin使用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. Using Maven plugin使用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. In case if you already have a ruleset and you just want to include the custom Java rule (that came from the library) you may define it directly in your ruleset xml as is:如果您已经有一个规则集并且您只想包含自定义 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM