Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD exclude-pattern with gradle

Tags:

gradle

pmd

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.

like image 815
Guillermo Merino Avatar asked Aug 27 '15 10:08

Guillermo Merino


People also ask

How do I exclude tasks in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

What is Gradle PMD?

The PMD plugin performs quality checks on your project's Java source files using PMD and generates reports from these checks.

What are PMD rules?

Conceptually, PMD rules work by matching a “pattern” against the AST of a file. Rules explore the AST and find nodes that satisfy some conditions that are characteristic of the specific thing the rule is trying to flag. Rules then report a violation on these nodes.


2 Answers

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/

like image 85
Kavi Avatar answered Sep 28 '22 06:09

Kavi


Add below snippet in your build.gradle, you can exclude Classes and packages also
pmd {
    sourceSets = [ project.sourceSets.main ]
    ruleSetFiles = rootProject.files("codequality/pmd-ruleset.xml")
    ruleSets = []

    pmdMain {
        excludes = [
                '**/Application.*',
                '**/jivesoftware/.*'
        ]
    }
}
like image 37
Abothula Ganesh Avatar answered Sep 28 '22 08:09

Abothula Ganesh