Remove a single rule check from PMD in maven plugin

≯℡__Kan透↙ 提交于 2019-12-10 01:14:13

问题


I want to exclude a single PMD rule in POM, but it is not working. I have tried creating a pmd-exclude.xml (in the same dir as the pom.xml):

<?xml version="1.0"?>
<ruleset name="remove_rules">
    <description>Remove rules</description>
    <rule ref="rulesets/unnecessary.xml">
        <exclude name="UselessParentheses"/>
    </rule>
</ruleset>

From http://www.ing.iac.es/~docs/external/java/pmd/howtomakearuleset.html and referenced it in pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.0.1</version>
    <configuration>
        <sourceEncoding>utf-8</sourceEncoding>
            <rulesets>
                    <ruleset>${pom.basedir}/pmd-exclude.xml</ruleset>
            </rulesets>
    </configuration>
</plugin>

But it keeps reporting these rules.

Also: I do not want to specify which rules it must check, as newer versions can (and will) include new ones and I do not want to check which new rules will run in every new version.


回答1:


I think you will need to add the rulesets you want to check and discard just the specific rule you don't need.

The pmd-exclude.xml should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    name="Android Application Rules"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" >

    <description>Remove rules</description>

    <rule ref="rulesets/clone.xml" />
    <rule ref="rulesets/finalizers.xml" />
    <rule ref="rulesets/imports.xml" />
    <rule ref="rulesets/logging-java.xml" />
    <rule ref="rulesets/unnecessary.xml" >
        <exclude name="UselessParentheses" />
    </rule>

</ruleset>



回答2:


@marcelopazzo's solution worked for me. I just want to add that if you are working with a multi-module maven project then you will have to update your reference to pmd-exclude.xml, unless you plan to have a copy of this file in each module.

Example

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.6</version>
        <configuration>
            <rulesets>
                <ruleset>${project.parent.basedir}/pmd-exclude.xml</ruleset>
            </rulesets>
        </configuration>
    </plugin>


来源:https://stackoverflow.com/questions/16021453/remove-a-single-rule-check-from-pmd-in-maven-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!