gpt4 book ai didi

java - 我们如何在 Maven 中为主要和测试 java 代码集拥有不同的 PMD 规则集?

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:02 27 4
gpt4 key购买 nike

在 Gradle 中,我们可以为 pmdMain 和 pmdTest 源集指定不同的 PMD 配置(包括不同的规则集)。例如

pmdMain {
ruleSetFiles = files("$javaBuildSystemRoot/src-pmd-rulesets.xml")
}

pmdTest {
ruleSetFiles = files("$javaBuildSystemRoot/test-pmd-rulesets.xml")
}

我们希望测试代码不像主代码那么严格。

有一个单独的基于 Maven 的项目,目前我们无法使用 gradle。但目前,我们希望至少应用基于 main 和 test 的 2 个不同的规则集。它是一个单模块单项目,使用maven PMD插件。

我们如何在 Maven pom 文件中执行此操作?

最佳答案

pmd 测试源是“相当不传统的”,但这不是问题的一部分。 :)

使用executions标记和利用maven-pmd-plugin ,你可以使用maven来做到这一点。

<小时/>

EDIT: In short & applied to the given input (and maybe more than you wanted/need), it enables/forces you to make both checks in every build:

<project><build><plugins>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.11.0</version> <!-- latest up-to-date -->
<executions>
<execution>
<id>pmd-execution</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rulesets>
<ruleset>/path/to/.../src-pmd-rulesets.xml</ruleset>
</rulesets>
</configuration>
</execution>
<execution>
<id>pmd-test-execution</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<includeTests>true</includeTests> <!-- this defaults to false! -->
<rulesets>
<ruleset>/path/to/.../test-pmd-rulesets.xml</ruleset>
</rulesets>
</configuration>
</execution>
</executions>
</plugin>
...

另请参阅:Can I configure multiple plugin executions in pluginManagement, and choose from them in my child POM?

<小时/>

EDIT 2: If you indeed don't need "both executions" (in 1 build), but only "two configurations" for "different builds", then:Maven Profiles fits your needs (with profiles ... your "possibilities converge to infinity") !

您可以介绍如下个人资料:

 <project>
...
<profiles>
<profile>
<id>pmdMain</id>
<properties>
<myPmdRuleSetLocation>/path/to/.../src-pmd-rulesets.xml</myPmdRuleSetLocation>
<myPmdTestsInclude>false</myPmdTestsInclude>
</properties>
</profile>
<profile>
<id>pmdTest</id>
<properties>
<myPmdRuleSetLocation>/path/to/.../test-pmd-rulesets.xml</myPmdRuleSetLocation>
<myPmdTestsInclude>true</myPmdTestsInclude>
</properties>
</profile>
<profiles>
...
</project>

并在您的(单次执行)pmd-plugin 配置中使用它:

...
<ruleset>${myPmdRuleSetLocation}</ruleset>
<includeTests>${myPmdTestsInclude}</includeTests>
...

read further关于配置文件及其激活。

(另外 <profile/> 可以包含并覆盖 <build/> 标签!)

关于java - 我们如何在 Maven 中为主要和测试 java 代码集拥有不同的 PMD 规则集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54613445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com