防止PMD二次打印违规

Prevent PMD from printing violations twice

我有一个 Java 7 项目正在由 Maven 3.2.1 构建。我正在使用 PMDmaven-pmd-plugin:3.2 在构建时分析我的源代码,它在后台使用 net.sourceforge.pmd:pmd:5.1.2)。

给定一个简单的 class 并有一次违规,我的 Maven 输出将那个确切的违规打印了两次,但我不希望出现重复的违规,因为它具有误导性且更难阅读。

我的源代码

public class Main {
    public static void main(final String[] args) {
        System.out.println("hello");
    }
}

我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.Whosebug</groupId>
    <artifactId>pmd-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <linkXref>false</linkXref>
                    <sourceEncoding>utf-8</sourceEncoding>
                    <minimumTokens>100</minimumTokens>
                    <targetJdk>1.7</targetJdk>
                    <minimumPriority>2</minimumPriority>
                    <rulesets>
                        <ruleset>/rulesets/java/logging-java.xml</ruleset>
                    </rulesets>
                    <failurePriority>2</failurePriority>
                    <printFailingErrors>true</printFailingErrors>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <phase>prepare-package</phase>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

Maven 输出

mvn prepare-package

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building pmd-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pmd-test ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pmd-test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Projects\pmd-test\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pmd-test ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pmd-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ pmd-test ---
[INFO]
[INFO] >>> maven-pmd-plugin:3.2:check (default) @ pmd-test >>>
[INFO]
[INFO] --- maven-pmd-plugin:3.2:pmd (pmd) @ pmd-test ---
[WARNING] Unable to locate Source XRef to link to - DISABLED
[INFO]
[INFO] <<< maven-pmd-plugin:3.2:check (default) @ pmd-test <<<
[INFO]
[INFO] --- maven-pmd-plugin:3.2:check (default) @ pmd-test ---
[INFO] PMD Failure: Main:4 Rule:SystemPrintln Priority:2 System.out.println is used.
[INFO] PMD Failure: Main:4 Rule:SystemPrintln Priority:2 System.out.println is used.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.161 s
[INFO] Finished at: 2016-07-06T20:21:41-08:00
[INFO] Final Memory: 21M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.2:check (default) on project pmd-test: You have 1 PMD violation. For more details see:C:\Projects\pmd-test\target\pmd.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.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

具体来说,它打印

[INFO] --- maven-pmd-plugin:3.2:check (default) @ pmd-test ---
[INFO] PMD Failure: Main:4 Rule:SystemPrintln Priority:2 System.out.println is used.
[INFO] PMD Failure: Main:4 Rule:SystemPrintln Priority:2 System.out.println is used.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

但我想让它打印出来

[INFO] --- maven-pmd-plugin:3.2:check (default) @ pmd-test ---
[INFO] PMD Failure: Main:4 Rule:SystemPrintln Priority:2 System.out.println is used.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

我试过的

这个问题是由于我对 Maven PMD 插件的错误配置造成的。

要解决此问题以便每次违规仅打印一次,请更改 maven-pmd-plugin 的配置以禁用 printFailingErrorsverbose。它们都默认为 false,因此删除其中一个元素即可解决问题。

pom.xml

中的固定插件配置示例
<plugin>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <linkXref>false</linkXref>
        <sourceEncoding>utf-8</sourceEncoding>
        <minimumTokens>100</minimumTokens>
        <targetJdk>1.7</targetJdk>
        <minimumPriority>2</minimumPriority>
        <rulesets>
            <ruleset>/rulesets/java/logging-java.xml</ruleset>
        </rulesets>
        <failurePriority>2</failurePriority>
        <verbose>true</verbose>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>check</goal>
            </goals>
            <phase>prepare-package</phase>
        </execution>
    </executions>
</plugin>

我认为详细模式应该只打印本来不会打印的错误或警告,以使其与曾经制作的所有其他日志系统保持一致,但我们做到了。