PMD与Maven - 如何在规则集中禁用一个规则?

时间:2012-02-05 08:27:43

标签: maven pmd

我通过Maven运行PMD 2.现在我已经准备好所有规则集以查看生成的内容(请参阅下面的代码)。我正在修复那些对我来说有意义的事情。但是,在“优化”规则集中,我希望保留规则集,但仅禁用规则集中的一个规则。就我而言,我想禁用“AvoidInstantiatingObjectsInLoopss”规则。

这是我的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 个答案:

答案 0 :(得分:2)

规则及其激活的定义在规则集文件中。 由于您拥有“rulesets”文件夹中的文件副本,只需删除或注释规则集文件中包含对规则的引用的节点。 在这种情况下,“AvoidInstantiatingObjectsInLoops”(net.sourceforge.pmd.rules.optimization.AvoidInstantiatingObjectsInLoops)是一个优化规则,因此它可能位于“/rulesets/optimizations.xml”中。

只需编辑文件,查找包含规则,并注释或删除相应的规则节点。

我不知道您的文件内容,因此您可以将规则包含多次。