java - 防止PMD二次打印违规

标签 java maven pmd

我有一个由 Maven 3.2.1 构建的 Java 7 项目。我在构建时使用 PMD 分析我的源代码(maven-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 日的最新版本,它没有改变输出。
  • 我从我的真实项目中提取了相关部分到上面的示例中,以确保一个最小的项目可以重现问题,它确实做到了。

最佳答案

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

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

关于java - 防止PMD二次打印违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237227/

相关文章:

java - OpenGL ES 2.0 中的立方体不绘制给定的纹理

java - 在 Java 8 中延迟返回第一个非空列表

java - 使 hibernate 反引号所有表/列名

java - Heroku - 找不到 Maven 子模块 pom

java - 将方法的复杂度降低 14

java - 避免 POJO 类中的重复代码

java - IConfigurationListener 参数 getInstance() 方法返回 null

Maven TestNG 项目,将命令行参数传递给 testng.xml 文件

java - 默认 jar 文件和 tests.jar 的不同 MANIFEST.MF

tfs - 如何强制开发人员在 checkin TFS 之前解决静态代码分析工具问题