PMD/CPD can't detect duplicate code

1.7k views Asked by At

I am new to PMD/CPD. I have configured PMD in my maven project as below:

<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>

My maven project is compiling normally and generating all reports by mvn jxr:jxr site. But I am unable to find any result which shows duplicate code. To test this I have introduced duplicate code intentionally in my code, as:

@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;
}

But always CPD shows no problem detected in the source code. However I can find PMD reports normally. Am I missing some configuration or ruleset?

Kindly help!

1

There are 1 answers

0
barfuin On BEST ANSWER

Make sure that you set the minimum token count low enough. Your short piece of duplicate code has fewer tokens than the default 100.

According to the docs, the property is called minimumTokens. Older versions of the Maven PMD plugin had a property maven.pmd.cpd.minimumtokencount. Set it to 5 for testing. In real life, the default of 100 is a good value.