Search code examples
javamavenpmdcpd

How to get PMD maven plugin to skip generated source code?


So I'm creating a maven plugin using the maven-plugin-plugin. The HelpMojo in maven-plugin-plugin generates a java source file.

Unfortunately, PMD is picking this up and complaining about it. Is there a way to have PMD ignore just a single source file? Thanks!

Maven PMD Configuration:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <executions>
                <execution>
                    <id>pmd-verify</id>
                    <goals>
                        <goal>check</goal>
                        <goal>cpd-check</goal>
                    </goals>
                    <configuration>
                        <printFailingErrors>true</printFailingErrors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Solution

  • Generated sources usually end up (with maven) in a subdirectory in target/generated-sources, for the maven-plugin-plugin it's target/generated-sources/plugin.

    You can exclude these complete directories with excludeRoots, e.g.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <executions>
                <execution>
                    <id>pmd-verify</id>
                    <goals>
                        <goal>check</goal>
                        <goal>cpd-check</goal>
                    </goals>
                    <configuration>
                        <printFailingErrors>true</printFailingErrors>
                        <excludeRoots>
                            <excludeRoot>target/generated-sources/plugin</excludeRoot>
                        </excludeRoots>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

    There is also a file based exclude option.