防止PMD两次打印违规

时间:2016-07-07 03:43:22

标签: java maven pmd

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

鉴于一个简单的类有一次违规,我的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.stackoverflow</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-plugin:3.6,这是2016年7月6日的最新版本,但没有更改输出。
  • 我将我真实项目中的相关部分提取到上面的示例中,以确保最小的项目可以重现问题。

1 个答案:

答案 0 :(得分:2)

此问题是由于我对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>

我认为详细模式应该只打印不会打印的错误或警告,以使其与所有其他日志系统保持一致,但我们就是这样。