如何在父项目中定义custom-rulesets.xml
,因此可以在子模块中重复使用它?
我有一个运行良好的PMD示例项目(you can check later)。
但是,这个maven项目没有孩子。
pmd-java-14-example -
|_ core
|_ tasks
<?xml version="1.0"?>
<!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml -->
<!-- https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#shortvariable -->
<ruleset
name="custom-ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0">
<description>
The Basic ruleset contains a collection of good practices which should be followed.
</description>
<rule ref="category/java/design.xml/SimplifiedTernary"/>
<rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal"/>
<rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal"/>
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/>
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
<rule ref="category/java/codestyle.xml/DuplicateImports"/>
<rule ref="category/java/codestyle.xml/ShortMethodName"/>
<rule ref="category/java/codestyle.xml/ShortVariable"/>
</ruleset>
当我有一个父pom和一个子模块时,pmd对于子模块失败,因为它找不到custom-rulesets.xml。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yk.utils</groupId>
<artifactId>pmd-java-14-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>14</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<pmd.plugin.version>3.13.0</pmd.plugin.version>
<pmd.core.version>6.23.0</pmd.core.version>
</properties>
<modules>
<module>core</module>
<module>tasks</module>
</modules>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</reporting>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<targetJdk>${java.version}</targetJdk>
<rulesets>
<!-- https://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html -->
<ruleset>/rulesets/java/maven-pmd-plugin-default.xml</ruleset>
<!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml -->
<!-- https://github.com/pmd/pmd/blob/master/pmd-core/src/main/resources/rulesets/internal/all-java.xml -->
<ruleset>/category/java/bestpractices.xml</ruleset>
<ruleset>
custom-ruleset.xml
</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<id>check pmd and fail</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmd.core.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmd.core.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>
不同之处在于,现在pom具有子模块和<packaging>pom</packaging>
。
此部分失败mvn clean install
<ruleset>custom-ruleset.xml</ruleset>
错误消息
[INFO]
[INFO] pmd-java-14-example ................................ SUCCESS [ 0.922 s]
[INFO] core ............................................... FAILURE [ 1.929 s]
[INFO] tasks .............................................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.980 s
[INFO] Finished at: 2020-05-03T13:15:45+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd (pmd) on project bst-core: Execution pmd of goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd failed: org.apache.maven.reporting.MavenReport
Exception: Could not find resource 'pmd-java-14-example\core\custom-ruleset.xml'. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
据我所知,maven将custom-rulesets.xml
文件用于父模块-成功,然后它尝试将此文件用于子模块,当然,该文件在子模块中不存在。
我试图设置一个属性-custom-rulesets.xml
位置的绝对路径。它有效,但是我不能将其推入github。
我还可以将此自定义规则集推送到git hub并添加指向github存储库和文件的链接并使用它。这是一个更好的选择。
当然,我不想在不同的子模块之间复制粘贴此文件。
[请发表您的答案,因为这是我现在需要的一种快速解决方法。
<ruleset>../custom-ruleset.xml</ruleset>
。
说明,我的项目结构是一个父级,有两个子级模块。由于子目录在目录树中处于同一级别,因此../
因此起作用。
但是,如果父目录中有src
文件夹,或者子模块中也有子目录,则将无法使用。>>