我正在尝试为Gradle中的PMD任务创建一些排除模式。

我的任务是通过以下方式生成的:

/* 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
                }
            }

        }
    }
}

规则集是:
<?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>

但是在我的报告中,我得到:
gradle - 带有PMdle的PMD排除模式-LMLPHP

难道我做错了什么?检查this答案似乎是我在正确定义exclude-pattern,但pmd正在分析这些文件。

最佳答案

我遇到了同样的问题,添加一个空的ruleSets []属性似乎可以解决该问题。
确保定义您实际上要在规则集文件中应用的规则-即,将它们从ruleSet属性块移动到文件中(如果有的话)。

这就是我的任务生成的样子:

// 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)
    }
}

我从这个线程得到了提示:
http://sourceforge.net/p/pmd/discussion/188193/thread/6e9c6017/

关于gradle - 带有PMdle的PMD排除模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32247190/

10-11 04:40