maven - PMD/CPD 无法检测重复代码

标签 maven pmd cpd

我是 PMD/CPD 的新手。我在我的 maven 项目中配置了 PMD,如下所示:

<groupId>org.parent</groupId>
<artifactId>CustRestExampleOsgi</artifactId>
<version>1.0</version>

<packaging>pom</packaging>
<name>CustRestExampleOsgii</name>

<modules>
    <module>CustImplProvider</module>
    <module>CustInterface</module>
    <module>RestCustConsumer</module>
</modules>

<properties>
<karaf.deploy.build.folder>
    G:\apache-karaf-3.0.0.RC1\deploy
</karaf.deploy.build.folder>
</properties>

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>3.0</version>
        </plugin>

        <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-javadoc-plugin</artifactId>
            <version>2.9.1</version>
        </plugin>
    </plugins>
</reporting>

我的 maven 项目正在正常编译并通过 mvn jxr:jxr site 生成所有报告。 但是我找不到任何显示重复代码的结果。为了测试这一点,我在我的代码中有意引入了重复代码,如:

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Address)) {
        return false;
    }
    Address other = (Address) object;
    if ((this.id == null && other.id != null)
      || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    if (!(object instanceof Address)) { //Duplicate is here
        return false;
    }
    return true;
}

但总是 CPD 显示在源代码中没有检测到问题。但是我可以正常找到 PMD 报告。我是否缺少某些配置或规则集?

请帮忙!

最佳答案

确保将最小 token 数设置得足够低。您的一小段重复代码的标记少于默认的 100 个。

根据 the docs ,该属性称为 minimumTokens。旧版本的 Maven PMD 插件有一个属性 maven.pmd.cpd.minimumtokencount。将其设置为 5 进行测试。在现实生活中,默认值 100 是一个不错的值。

关于maven - PMD/CPD 无法检测重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455837/

相关文章:

java - 为 Maven 项目获取 java.lang.ClassNotFoundException : com. google.gson.Gson

java - 基于 Maven 的代码生成器

java - 如何让 PMD maven 插件跳过生成的源代码?

java - Sonar CPD 检测 block 重复

c++: 未定义的错误引用

java - 如何使用properties-maven-plugin?

java - SimpleDateFormat 的 Checkstyle/PMD 规则不是线程安全警告

java - PMD 插件因 Java 14 : Unsupported targetJdk 而失败

c++ - 抑制来自 CPD 的 C/C++ 代码警告

java - Maven 站点 :deploy Compile Error : "cannot find symbol" - how can a specific dependency be excluded?