PMD在具有多个模块的项目中使用自定义规则集时出现问题。

huangapple go评论98阅读模式
英文:

PMD fails with custom rulesets for project with multiple modules

问题

如何在父项目中定义 custom-rulesets.xml,以便在子模块中重用它?

我有一个正常运行的 PMD 示例项目(你可以稍后查看)。

然而,这个 Maven 项目没有子模块。

项目结构

pmd-java-14-example _.
                     |_ core
                     |_ tasks

custom-rulesets.xml

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

pom.xml(具有子模块的父 POM)。

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

    <!-- 其他部分略去 -->
</project>

不同之处在于现在 POM 有子模块和 &lt;packaging&gt;pom&lt;/packaging&gt;

这部分导致 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。

我还可以将这个自定义规则集推送到 GitHub,并添加指向 GitHub 仓库和文件的链接来使用它。这是一个更好的替代方案。

当然,我不想在不同的子模块之间复制粘贴这个文件。

英文:

How to define custom-rulesets.xml in parent project, so it is re-used in children modules?

I have a PMD example project that works well (you can check later).

However, this maven project does not have children.

Project structure

pmd-java-14-example _.
                     |_ core
                     |_ tasks

custom-rulesets.xml

&lt;?xml version=&quot;1.0&quot;?&gt;

&lt;!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml --&gt;
&lt;!-- https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#shortvariable --&gt;
&lt;ruleset
        name=&quot;custom-ruleset&quot;
        xmlns=&quot;http://pmd.sourceforge.net/ruleset/2.0.0&quot;&gt;
    &lt;description&gt;
        The Basic ruleset contains a collection of good practices which should be followed.
    &lt;/description&gt;

    &lt;rule ref=&quot;category/java/design.xml/SimplifiedTernary&quot;/&gt;
    &lt;rule ref=&quot;category/java/codestyle.xml/MethodArgumentCouldBeFinal&quot;/&gt;
    &lt;rule ref=&quot;category/java/codestyle.xml/LocalVariableCouldBeFinal&quot;/&gt;
    &lt;rule ref=&quot;category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor&quot;/&gt;
    &lt;rule ref=&quot;category/java/bestpractices.xml/UnusedLocalVariable&quot;/&gt;
    &lt;rule ref=&quot;category/java/bestpractices.xml/UnusedPrivateField&quot;/&gt;
    &lt;rule ref=&quot;category/java/codestyle.xml/DuplicateImports&quot;/&gt;
    &lt;rule ref=&quot;category/java/codestyle.xml/ShortMethodName&quot;/&gt;
    &lt;rule ref=&quot;category/java/codestyle.xml/ShortVariable&quot;/&gt;
&lt;/ruleset&gt;

When I have a parent pom and a child module, pmd fails for the child module, because it cannot find custom-rulesets.xml.

pom.xml (parent pom that has a child module).

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.yk.utils&lt;/groupId&gt;
    &lt;artifactId&gt;pmd-java-14-example&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;


    &lt;properties&gt;
        &lt;java.version&gt;14&lt;/java.version&gt;
        &lt;maven.compiler.source&gt;${java.version}&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;${java.version}&lt;/maven.compiler.target&gt;

        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;

        &lt;pmd.plugin.version&gt;3.13.0&lt;/pmd.plugin.version&gt;
        &lt;pmd.core.version&gt;6.23.0&lt;/pmd.core.version&gt;
    &lt;/properties&gt;


	&lt;modules&gt;
        &lt;module&gt;core&lt;/module&gt;
        &lt;module&gt;tasks&lt;/module&gt;
    &lt;/modules&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
            &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
            &lt;version&gt;1.1.7&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;


    &lt;reporting&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jxr-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0&lt;/version&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/reporting&gt;

    &lt;build&gt;
        &lt;pluginManagement&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-pmd-plugin&lt;/artifactId&gt;
                    &lt;version&gt;${pmd.plugin.version}&lt;/version&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/pluginManagement&gt;

        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-pmd-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;failOnViolation&gt;true&lt;/failOnViolation&gt;
                    &lt;printFailingErrors&gt;true&lt;/printFailingErrors&gt;
                    &lt;targetJdk&gt;${java.version}&lt;/targetJdk&gt;


                    &lt;rulesets&gt;
                        &lt;!-- https://maven.apache.org/plugins/maven-pmd-plugin/examples/usingRuleSets.html --&gt;
                        &lt;ruleset&gt;/rulesets/java/maven-pmd-plugin-default.xml&lt;/ruleset&gt;

                        &lt;!-- https://github.com/pmd/pmd/blob/master/pmd-java/src/main/resources/rulesets/java/basic.xml --&gt;
                        &lt;!-- https://github.com/pmd/pmd/blob/master/pmd-core/src/main/resources/rulesets/internal/all-java.xml --&gt;
                        &lt;ruleset&gt;/category/java/bestpractices.xml&lt;/ruleset&gt;
                        &lt;ruleset&gt;
                            custom-ruleset.xml
                        &lt;/ruleset&gt;
                    &lt;/rulesets&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;check pmd and fail&lt;/id&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;check&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;

                &lt;dependencies&gt;
                    &lt;dependency&gt;
                        &lt;groupId&gt;net.sourceforge.pmd&lt;/groupId&gt;
                        &lt;artifactId&gt;pmd-java&lt;/artifactId&gt;
                        &lt;version&gt;${pmd.core.version}&lt;/version&gt;
                    &lt;/dependency&gt;

                    &lt;dependency&gt;
                        &lt;groupId&gt;net.sourceforge.pmd&lt;/groupId&gt;
                        &lt;artifactId&gt;pmd-core&lt;/artifactId&gt;
                        &lt;version&gt;${pmd.core.version}&lt;/version&gt;
                    &lt;/dependency&gt;
                &lt;/dependencies&gt;
            &lt;/plugin&gt;


            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.9.0&lt;/version&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-project-info-reports-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0&lt;/version&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

The difference is that now the pom has child modules and &lt;packaging&gt;pom&lt;/packaging&gt;.

This part fails mvn clean install

&lt;ruleset&gt;custom-ruleset.xml&lt;/ruleset&gt;

Error messages

[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 &#39;pmd-java-14-example\core\custom-ruleset.xml&#39;. -&gt; [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.

What I tried

As I understand, maven uses custom-rulesets.xml file for parent modules - it is successful, then it tries to use this file for child module, and of course, this file is not present in the child module.

I tried to set a property - absolute path to the location of custom-rulesets.xml. It works, however, I cannot push it into github.

I can also push this custom rule set to git hub and add a link to github repository and file and use it.
This is a better alternative.

And of course, I do not want to copy-paste this file across different children modules.

答案1

得分: 1

我建议您将规则集放在Maven项目之外,就像您为其他规则集所做的那样。

问题是您自定义的规则集是否只应针对该项目中的子模块进行检查。还是您希望检查所有Java模块?

如果您想检查所有的Java模块,只需将规则集以其绝对路径添加到现有的PMD插件配置中。PMD插件会忽略POM项目。

如果这是一个特定的规则集,仅适用于该项目和其子模块,我会在父项目(POM项目)中进行配置。

英文:

I would suggest that you place your rulesets outside the Maven projects, like you are doing for the other rulesets.

The question is whether your custom ruleset should only be checked for those child modules in that project. Or is it rather something you would like to check for all Java modules?

If you want to check all your Java modules, just add the ruleset with its absolute path to your existing PMD plugin configuration. The PMD plugin ignores POM projects.

If it's a specific ruleset for just that project and just those child modules, I would configure that in the parent project (the POM project).

答案2

得分: 0

请发布您的答案,因为这是我现在需要的一个快速解决方法!

&lt;ruleset&gt;../custom-ruleset.xml&lt;/ruleset&gt;

解释一下,我的项目结构是一个父项目,有两个子模块。由于这两个子模块在目录树中处于同一级别,所以 ../ 是有效的。

然而,如果父项目中有 src 文件夹,或者某个子模块也有子模块,那么这种方法就不起作用。

英文:

Please, post your answers because this is a quick work-around which I need now!

&lt;ruleset&gt;../custom-ruleset.xml&lt;/ruleset&gt;.

Explanation, my project structure is one parent that has two children modules. As the children are on the same level in the directory tree, ../ thus it works.

However, it will not work, if there is src folder in parent, or if a child module has children as well.

huangapple
  • 本文由 发表于 2020年5月3日 19:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定