Search code examples
gradlepmd

PMD exclude-pattern with gradle


I'm attempting to create some exclude patterns for a PMD task in Gradle.

My task is generated in the next way:

/* Allows generation of pmd config  */
allprojects {
    apply plugin: 'pmd'
}
gradle.projectsEvaluated {

    subprojects.each() { project ->


        if (project.hasProperty('android')) {

            project.task("runPmd", type: Pmd) {

                description "Run pmd"
                group 'verification'

                source = fileTree("${project.projectDir}/src/main/java")
                ruleSetFiles = files("${project.rootDir}/build-tools/pmd.xml")
                ignoreFailures = true

                reports {
                    xml.enabled = true
                    html.enabled = true
                }
            }

        }
    }
}

And the ruleSet is:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MyCompany ruleset"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

    <description>
        MyCompany ruleset for Android PMD
    </description>

  <exclude-pattern>.*/org/jivesoftware/.*</exclude-pattern>
  <exclude-pattern>.*/net/java/.*</exclude-pattern>

...rules...
</ruleset>

But in my reports, I'm getting: Report

Am I doing something wrong? Checking this answer seems that I'm defining the exclude-pattern right, but pmd is analyzing those files.


Solution

  • I was running into the same issue and adding an empty ruleSets [] property seemed to fix it for me.
    Make sure to define the rules that you actually want to apply in your ruleset file - i.e. Move them from the ruleSet property block to the file (if you had any there).

    This is what my task generation looks like:

    // PMD
    afterEvaluate {
        def variants = plugins.hasPlugin('com.android.application') ?
                android.applicationVariants : android.libraryVariants
    
        variants.each { variant ->
            def task = tasks.create("pmd${variant.name.capitalize()}", Pmd)
    
            task.group = 'verification'
            task.description = "Run PMD for the ${variant.description}."
    
            task.ruleSetFiles = files("pmd-ruleset.xml")
            task.ruleSets = []
    
            task.reports {
                xml.enabled = false
                html.enabled = true
            }
    
            def variantCompile = variant.javaCompile
    
            task.source = variantCompile.source
    
            task.dependsOn(variantCompile)
            tasks.getByName('check').dependsOn(task)
        }
    }
    

    I got the hint from this thread: http://sourceforge.net/p/pmd/discussion/188193/thread/6e9c6017/