[英]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:我们必须处理以下材料/问题/答案:
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插件的官方文档中,有一个依赖部分在没有实际示例的情况下从高层次解释了这方面:
pmd
- The PMD libraries to use pmd
- 要使用的 PMD 库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 规则添加到您的项目中
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 }
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>...
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.