7
votes

I have PMD running via Maven 2. Right now I have all the rulesets in place to see what gets generated (see code below). I'm going through and fixing things that make sense to me to fix. However, there are cases such as in the "optimizations" ruleset where I want to keep the rule set, but disable just one of the rules within the rule set. In my case, I want to disable the "AvoidInstantiatingObjectsInLoopss" rule.

Here is the reporting section of my pom.xml

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
            <version>2.3</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <linkXref>true</linkXref>
                <sourceEncoding>utf-8</sourceEncoding>
                <minimumTokens>${pmd.minimumTokens}</minimumTokens>
                <failOnViolation>${pmd.failOnViolation}</failOnViolation>
                <targetJdk>${projectTargetJdk}</targetJdk>

                <rulesets>
                    <!-- See the FAQ here: http://maven.apache.org/maven-1.x/plugins/pmd/faq.html -->
                    <!-- See the rule sets here: http://pmd.sourceforge.net/ (menu on the left has a Rule Sets section -->
                    <!-- Unused rule sets -->
                    <!-- <ruleset>/rulesets/naming.xml</ruleset> -->

                    <!-- Unable to find rule sets -->
                    <!-- <ruleset>/rulesets/emptycode.xml</ruleset> -->
                    <!-- <ruleset>rulesets/comments.xml</ruleset> -->
                    <!-- <ruleset>/rulesets/unnecessary.xml</ruleset> -->
                    <!-- <ruleset>/rulesets/logging.xml</ruleset> -->

                    <!-- used rule sets -->
                    <!-- http://pmd.sourceforge.net/rules/java/basic.html -->
                    <ruleset>/rulesets/basic.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/braces.html -->
                    <ruleset>/rulesets/braces.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/clone.html -->
                    <ruleset>/rulesets/clone.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/codesize.html -->
                    <ruleset>/rulesets/codesize.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/controversial.html -->
                    <ruleset>/rulesets/controversial.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/coupling.html -->
                    <ruleset>/rulesets/coupling.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/design.html -->
                    <ruleset>/rulesets/design.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/finalizers.html -->
                    <ruleset>/rulesets/finalizers.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/imports.html -->
                    <ruleset>/rulesets/imports.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/j2ee.html -->
                    <ruleset>/rulesets/j2ee.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/junit.html -->
                    <ruleset>/rulesets/junit.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/javabeans.html -->
                    <ruleset>/rulesets/javabeans.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/migrating.html -->
                    <ruleset>/rulesets/migrating.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/optimizations.html -->
                    <ruleset>/rulesets/optimizations.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/strictexception.html -->
                    <ruleset>/rulesets/strictexception.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/strings.html -->
                    <ruleset>/rulesets/strings.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/sunsecure.html -->
                    <ruleset>/rulesets/sunsecure.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/typeresolution.html -->
                    <ruleset>/rulesets/typeresolution.xml</ruleset>
                    <!-- http://pmd.sourceforge.net/rules/java/unusedcode.html -->
                    <ruleset>/rulesets/unusedcode.xml</ruleset>
                </rulesets>
            </configuration>
        </plugin>
    </plugins>
</reporting>
1
I run PMD as part of Sonar. Sonar provides a GUI for managing which rules are enabled.Mark O'Connor
Sonar looks very interesting. I'll investigate more in that regard. For now, I'm still interested in understanding if there is a way to address my needs via maven configurations for example?Bellini

1 Answers

2
votes

The definition of the rules and its activation is on the ruleset files. As you have a copy of the files on the "rulesets" folder, just delete or comment the node on the ruleset file which contains the reference to the rule. In this case, "AvoidInstantiatingObjectsInLoops" (net.sourceforge.pmd.rules.optimization.AvoidInstantiatingObjectsInLoops) is an Optimization rule, so it's likely to be in "/rulesets/optimizations.xml".

Just edit the file, look for the inclusion of the rule, and comment or delete the corresponding rule node.

I don't know the content of your files, so you could have the rule included more than one time.