18

Is there a way to tell PMD to ignore checking parts of code for duplication?

For example, can I do something like this:

// CPD-Ignore-On
...
// CPD-Ignore-Off

Currently I have PMD set up like this using Maven, but don't see any arguments that would like me do what I want unless I am missing something.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <minimumTokens>40</minimumTokens>
                <targetJdk>1.5</targetJdk>
                <ignoreIdentifiers>true</ignoreIdentifiers>
                <ignoreLiterals>true</ignoreLiterals>
            </configuration>
        </plugin>
digiarnie
  • 20,378
  • 29
  • 75
  • 124

3 Answers3

23

After digging around enough, I finally came across it.

By adding the annotations @SuppressWarnings("CPD-START") and @SuppressWarnings("CPD-END") all code within will be ignored by CPD - thus you can avoid false positives.

Source - http://pmd.sourceforge.net/pmd-5.0.5/cpd-usage.html.

Avik
  • 1,051
  • 9
  • 25
  • 5
    The problem with this approach is that you can't exclude a whole class from CPD. The end annotation needs something to target. – Snicolas Jun 18 '14 at 12:34
  • 5
    @Snicolas I bound `@SuppressWarnings("CPD-START")` to the class, omitted the end annotation, and it looks like the entire class got ignored. Not ideal, but it seems to work. – Gili May 06 '16 at 17:12
  • Comment based suppressions are supported since PMD 5.7.0 – Johnco Apr 17 '19 at 03:31
3

I know this is a 8 years old question, but for completeness, CPD does support this since PMD 5.6.0 (April 2017).

The complete (current) docs for comment-based suppression are available at https://pmd.github.io/pmd-6.13.0/pmd_userdocs_cpd.html#suppression

It's worth noting, if a file has a // CPD-OFF comment, but no matching // CPD-ON, everything will be ignored until the end of file.

Johnco
  • 3,703
  • 4
  • 27
  • 40
2

I found it possible only to disable a whole class check in maven-pmd-plugin configuration in project pom. It is performed by adding <excludes> tag. If you would like to do it, your conf should be like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <minimumTokens>40</minimumTokens>
        <targetJdk>1.5</targetJdk>
        <ignoreIdentifiers>true</ignoreIdentifiers>
        <ignoreLiterals>true</ignoreLiterals>
        <excludes>
            <exclude>**/YourClassName.java</exclude>
            ........
            <exclude>....</exclude>
        </excludes>
    </configuration>
</plugin>
kamuflage661
  • 519
  • 4
  • 15