Search code examples
javamavencheckstylepmdcpd

PMD Failure: ILogin:73 Rule:ConstantsInInterface Priority:3 Avoid constants in interfaces


Can someone tell me how to exclude some interfaces from PMD analysis using maven. I am getting the below exception while making the maven build.

PMD Failure: ILogin$RetrieveLoginInfo_:4 Rule:ConstantsInInterface Priority:3 Avoid constants in interfaces. Interfaces define types, constants are implementation details better placed in classes or enums. See Effective Java, item 19.. [

I have added exclude-pmd.properties in pom's properties. This is my entry in pom.xml properties.

<pmd.excludeFromFailureFile>${project.basedir}/src/etc/exclude-pmd.properties</pmd.excludeFromFailureFile>

exclude-pmd.properties entry:

com.login.ILogin=ConstantsInInterface Priority:3 Avoid constants in interfaces.

Interface:

public interface ILogin {
    interface RetrieveLoginInfo_ {
    int STATUS=0
    }
    }

But maven is not exluding ILogin interface from PMD analysis.


Solution

  • Firstly, the structure of your exclude-pmd.properties is incorrect. As per https://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html

    You are expected to use the rule name alone as the value (comma-separated), so in this case it should read:

    com.login.ILogin=ConstantsInInterface
    

    That being said, I think you are expecting a different behavior from this property.

    This will simply avoid the pmd:check task from failing if the only violation reported is that one on that file. It will not ignore the file from analysis. It's meant mostly as a way to incrementally enable PMD on a legacy project.

    You should probably use a different approach for the use case you describe.

    1. You can simply suppress this warning in the source code, following https://pmd.github.io/pmd-6.15.0/pmd_userdocs_suppressing_warnings.html
    2. You can actually ignore the file from analysis (for all rules!) through a custom ruleset, following https://pmd.github.io/pmd-6.15.0/pmd_userdocs_making_rulesets.html#filtering-the-processed-files