简体   繁体   English

不同的Maven版本提供不同的Pmd结果

[英]Different Maven versions give different pmd results

One of my projects used the maven-pmd-plugin and I have found that I receive different results when using Maven 3.0.5 and Maven 3.2.1. 我的一个项目使用了maven-pmd-plugin,发现使用Maven 3.0.5和Maven 3.2.1时会收到不同的结果。 All other variables (Java version, OS version, project configuration). 所有其他变量(Java版本,OS版本,项目配置)。 I've also tried Maven 3.1.1 我也尝试过Maven 3.1.1

I'm running 我在跑

mvn clean pmd:pmd

on the command line 在命令行上

In Maven 3.0.5, I get one warning: 在Maven 3.0.5中,我得到一个警告:

<violation beginline="44" endline="307" begincolumn="8" endcolumn="1" rule="MoreThanOneLogger" ruleset="Java Logging" package="com.adobe.acs.commons.logging.impl" externalInfoUrl="${pmd.website.baseurl}/rules/java/logging-java.html#MoreThanOneLogger" priority="2">
Class contains more than one logger.
</violation>

In Maven 3.2.1 (and 3.1.1), I get zero warnings. 在Maven 3.2.1(和3.1.1)中,我得到零警告。

I also tried removing my custom rulset and now have the maven-pmd-plugin configured like this: 我还尝试删除了我的自定义rulset,现在将maven-pmd-plugin配置如下:

<plugin>
     <artifactId>maven-pmd-plugin</artifactId>
     <version>3.1</version>
     <configuration>
         <linkXRef>false</linkXRef>
         <rulesets>
             <ruleset>/rulesets/java/logging-java.xml</ruleset>
         </rulesets>
         <targetJdk>1.6</targetJdk>
     </configuration>
 </plugin>

With the same result. 结果相同。

Knowing the wider context of your issue helped me to arrive at the solution without too much discussion. 了解问题的更广泛背景有助于我无需过多讨论即可得出解决方案。

There is a difference in the graph of implicit dependencies between the various maven versions, as in the ones that come along with maven as baked in defaults. 各种maven版本之间的隐式依赖关系图有所不同,例如默认情况下随maven一起提供的版本。 I am able to reproduce this issue locally, and when I add the -X switch for debug mode, and run a diff for mvn clean pmd:pmd pmd:check -X on both maven versions, I see the following import in the 3.2.1 output, which I thought was a possible candidate for follow-up, since it has to do not only with the kind of logging that the rule checks for, but also because I happen to know you are using slf4j: 我能够在本地重现此问题,并且当我在调试模式下添加-X开关并在两个maven版本上为mvn clean pmd:pmd pmd:check -X运行diff时,我在3.2中看到以下导入。 1个输出,我认为它可能是后续的选择,因为它不仅与规则检查的日志记录有关,而且还因为我碰巧知道您正在使用slf4j:

[DEBUG] Imported: org.slf4j.* < plexus.core [DEBUG]导入:org.slf4j。* <plexus.core

It appears that SLF4j was added to plexus.core, which may mean that the slf4j api is available to all plugin classpaths now, whereas before it would require that the plugin imported the library in its own dependencies. 看来SLF4j已添加到plexus.core,这可能意味着slf4j api现在可用于所有插件类路径,而在这之前,要求插件以其自己的依赖项导入库。

I went to see what the PMD MoreThanOneLoggerRule is looking up, and it turns out it only looks for Log4j loggers and JCL loggers by resolved type, and anything named "Logger" if it can't resolve the type: 我去看了一下PMD MoreThanOneLoggerRule在查找什么,结果发现它仅按解析类型查找Log4j记录器和JCL记录器,如果无法解析该类型,则名称为“ Logger”的任何内容:

http://pmd.sourceforge.net/pmd-5.1.1/xref/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.html http://pmd.sourceforge.net/pmd-5.1.1/xref/net/sourceforge/pmd/lang/java/rule/logging/MoreThanOneLoggerRule.html

The solution appears to be to add the slf4j API right in the pmd plugin dependencies, as in: 解决方案似乎是在pmd插件依赖项中添加slf4j API,如下所示:

<plugin>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <linkXRef>false</linkXRef>
        <rulesets>
            <ruleset>/rulesets/java/logging-java.xml</ruleset>
        </rulesets>
        <targetJdk>1.6</targetJdk>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
    </dependencies>
</plugin>

This puts org.slf4j.Logger on the PMD classpath, so that it is able to resolve the type during analysis. 这会将org.slf4j.Logger放在PMD类路径上,以便它能够在分析期间解析类型。 Since it is neither a Log4j Logger or JCL Logger, it no longer cares about having more than one of them. 由于它既不是Log4j记录器也不是JCL记录器,所以它不再关心拥有多个记录器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM