java - Gradle 的 PMD 插件 : what are acceptable arguments?

标签 java gradle pmd

  • Java 1.7.0_40
  • Gradle 1.10

我从未使用过 Gradle 的 PMD 插件,我在尝试将规则集添加到我的 build.gradle 时遇到了麻烦。 Pmd documentation不清楚 ruleSets 的有效值是什么。他们的示例是 ruleSets = ["basic", "braces"] 并且他们链接到 "official list" .不幸的是,没什么可继续的。

我猜部分标题以某种方式映射到有效字符串?喜欢,

但是像 "Empty Code (java)" 这样的东西呢? ?

这是一个有效的 build.gradle 示例:

apply plugin: 'java'
apply plugin: 'pmd'

pmd {
    ruleSets = [
            // The first two better work since it's right in the Javadoc...
            "basic",
            "braces",
            // This one does not work and other variations like 
            // "empty code", "emptycode", "empty-code", "empty_code" do not work.
            "emptyCode"
    ]
}
repositories {
    mavenCentral()
}

Gradle 吐出以下错误:

$ gradle check
:pmdMain FAILED                                                                  

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':pmdMain'.
> Can't find resource emptyCode.  Make sure the resource is a valid file or URL 
    or is on the CLASSPATH.  Here's the current classpath:
    /Users/kuporific/gradle/gradle-1.10/lib/gradle-launcher-1.10.jar

* Try:        
Run with --stacktrace option to get the stack trace. Run with --info or --debug 
    option to get more log output.

BUILD FAILED  

Total time: 9.907 secs

Running with --stacktrace or --debug as suggested doesn't seem to yield anything useful...

Note: create a dummy file like src/main/java/Dummy.java. Otherwise, the build will succeed.

How are ruleSets supposed to be declared?


Edit:

It ended up being easier declaring an xml rule set because it offers fine-grained control over the rules. It is included in build.gradle like so:

apply plugin: 'pmd'

pmd {
    ruleSetFiles = files('path/to/ruleSet.xml')
}

规则集文件看起来像这样:

注意:此示例是为 Gradle 1.10 编写的。较新版本的 Gradle(大约 2.0)使用较新版本的 PMD;因此,许多 rulesets 路径发生了变化。例如,rulesets/logging-java.xml 现在可以在 rulesets/java/logging-java.xml 中找到。

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    name="Android Application Rules"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 
                        http://pmd.sf.net/ruleset_xml_schema.xsd" >

    <rule ref="rulesets/logging-java.xml" />
    <rule ref="rulesets/basic.xml" />
    <rule ref="rulesets/braces.xml" />
    <rule ref="rulesets/codesize.xml" >
        <exclude name="TooManyMethods" />
    </rule>
    <rule ref="rulesets/controversial.xml">
        <exclude name="UseConcurrentHashMap" />
        <exclude name="AvoidLiteralsInIfCondition" />
        <exclude name="DataflowAnomalyAnalysis" />
        <exclude name="CallSuperInConstructor" />
        <exclude name="AtLeastOneConstructor" />
        <exclude name="NullAssignment" />
    </rule>
    <!-- etc... -->
</ruleset>

最佳答案

gradle支持最新版本的PMD(写这个答案时是5.1.3)。规则集需要以 java-

为前缀

我用 gradle-1.12 测试了这个

要将 PMD 5.1.3 与 gradle 一起使用,以下配置定义了我能找到的所有可能的规则集:

pmd {
    toolVersion = '5.1.3'
    ruleSets = [
            'java-android',
            'java-basic',
            'java-braces',
            'java-clone',
            'java-codesize',
            'java-comments',
            'java-controversial',
            'java-coupling',
            'java-design',
            'java-empty',
            'java-finalizers',
            'java-imports',
            'java-j2ee',
            'java-javabeans',
            'java-junit',
            'java-logging-jakarta-commons',
            'java-logging-java',
            'java-migrating',
            'java-naming',
            'java-optimizations',
            'java-strictexception',
            'java-strings',
            'java-sunsecure',
            'java-typeresolution',
            'java-unnecessary',
            'java-unusedcode'
            ]
}

引用: http://forums.gradle.org/gradle/topics/_pmdtest_fails_with_cant_find_resource_null_when_rulesets_include_braces_gradle_2_0_rc_1

关于java - Gradle 的 PMD 插件 : what are acceptable arguments?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710704/

相关文章:

gradlew 在获取 gradle 二进制文件时得到 403

gradle - 如何只用一个jar文件定义一个简单的Gradle项目?

java - Eclipse-PMD 全局配置规则集

gradle - 运行 Gradle 的 PMD 插件时在类路径中包含 jar

maven-2 - 如何使 maven-pmd-plugin 支持最新的 PMD 版本?

java - 是否存在竞争条件,或者对同一对象的同一方法进行多次调用时如何工作

java - 将 Parse (Android) 与 RxJava 结合使用

gradle - 如何使用单个 build.gradle 配置向 kotlin 多平台项目添加依赖项

java - 创建一个 ArrayList,每个值都有一个 hashmap

java - 如何使用集合添加列表(字符串数组)和arrayList?