Gradle的PMD插件:什么是可接受的参数?

时间:2013-12-20 19:31:09

标签: 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

根据建议使用--stacktrace--debug投放似乎没有产生任何有用的内容......

注意:创建一个像src/main/java/Dummy.java这样的虚拟文件。否则,构建成功。

ruleSets 如何宣布


编辑:

最终更容易声明xml规则集,因为它提供了对规则的细粒度控制。它包含在build.gradle中,如此:

apply plugin: 'pmd'

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

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

注意:这个exaple是为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>

4 个答案:

答案 0 :(得分:18)

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

答案 1 :(得分:6)

我意识到这是一个巨大的编辑,但它本质上是一个不同的答案。所以在与你交谈并玩弄它之后,我已经确定Gradle插件使用的pmd库的版本比发布时稍早(即版本4.3);但是,插件中缺少一些规则集,因为最新的pmd版本是5.0.5,它与使用Gradle和Java的NullPointerException打破。现在,在编写了我生命中最具语法正确和最艰苦的Hello World Java程序来测试所有这些之后,我编写了每一个与Gradle插件一起使用的Java规则集:

这是Main.java:

package william;
import java.util.logging.Logger;

public final class Main{
    private Main(){}
    public static void main(final String [ ] args){
        final Logger log = Logger.getLogger(Main.class.getName());
        log.fine("Hello World");
    }
}

这是build.gradle:

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

pmd {
    ruleSets = [
            "basic",
            "braces",
            "naming",
            "android",
            "clone",
            "codesize",
            "controversial",
            "design", 
            "finalizers",
            "imports",
            "j2ee",
            "javabeans",
            "junit",
            "logging-jakarta-commons",
            "logging-java",
            "migrating",
            "optimizations",
            "strictexception",
            "strings",
            "sunsecure",
            "typeresolution",
            "unusedcode"
            ]
}
repositories {
    mavenCentral()
}

现在你可能想知道,哪些规则集还不支持?答案是:

  1. "comments"
  2. "empty"
  3. "unnecessary"
  4. 当我说,相信我,其余规则完美无瑕。在写一个Hello World时,他们让我分开了。所以,我希望这有帮助,定义了所有Java rulesets.xml文件的目录位于:link to pmd's github java ruleset directory我忽略了迁移的目录,因为它们不起作用。我认为他们是针对具体的事情。

    祝你好运,我会在gradle论坛上提出缺少规则集的问题,请求添加或更新版本。或者您可以自定义编译插件并将其链接到较新的pmd版本,如果您真的非常渴望缺少规则集。

答案 2 :(得分:6)

在这里添加其他优秀答案。将pmd应用于您的gradle构建并通过gradle pmdMain调用它后,pmd jar将被下载到您的gradle缓存中。从那里你可以运行:

find ~/.gradle -name "*pmd*.jar" -exec jar -tvf {} \;|grep rulesets

你会得到输出:

0 Thu Nov 10 20:48:06 EST 2011 rulesets/
     0 Thu Nov 10 20:48:06 EST 2011 rulesets/internal/
     0 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/
 18068 Thu Nov 10 20:48:06 EST 2011 rulesets/naming.xml
    65 Thu Nov 10 20:48:06 EST 2011 rulesets/jsprulesets.properties
   710 Thu Nov 10 20:48:06 EST 2011 rulesets/migrating_to_15.xml
   483 Thu Nov 10 20:48:06 EST 2011 rulesets/migrating_to_14.xml
  1048 Thu Nov 10 20:48:06 EST 2011 rulesets/rulesets.properties
  3017 Thu Nov 10 20:48:06 EST 2011 rulesets/javabeans.xml
  2089 Thu Nov 10 20:48:06 EST 2011 rulesets/sunsecure.xml
   777 Thu Nov 10 20:48:06 EST 2011 rulesets/migrating_to_junit4.xml
  3198 Thu Nov 10 20:48:06 EST 2011 rulesets/scratchpad.xml
 13190 Thu Nov 10 20:48:06 EST 2011 rulesets/strings.xml
  1379 Thu Nov 10 20:48:06 EST 2011 rulesets/internal/all-java.xml
  2639 Thu Nov 10 20:48:06 EST 2011 rulesets/internal/dogfood.xml
  6036 Thu Nov 10 20:48:06 EST 2011 rulesets/finalizers.xml
  5347 Thu Nov 10 20:48:06 EST 2011 rulesets/logging-jakarta-commons.xml
 13629 Thu Nov 10 20:48:06 EST 2011 rulesets/migrating.xml
   610 Thu Nov 10 20:48:06 EST 2011 rulesets/migrating_to_13.xml
  3593 Thu Nov 10 20:48:06 EST 2011 rulesets/braces.xml
  4163 Thu Nov 10 20:48:06 EST 2011 rulesets/clone.xml
   702 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/33.xml
  1332 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/41.xml
  1009 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/35.xml
   395 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/43.xml
  1340 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/40rc1.xml
  1110 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/34.xml
   537 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/38.xml
   346 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/37-jsp.xml
   393 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/37.xml
   744 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/39.xml
  1066 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/36.xml
  1256 Thu Nov 10 20:48:06 EST 2011 rulesets/releases/42.xml
  6379 Thu Nov 10 20:48:06 EST 2011 rulesets/android.xml
  4967 Thu Nov 10 20:48:06 EST 2011 rulesets/logging-java.xml
 11557 Thu Nov 10 20:48:06 EST 2011 rulesets/j2ee.xml
 52926 Thu Nov 10 20:48:06 EST 2011 rulesets/design.xml
  9216 Thu Nov 10 20:48:06 EST 2011 rulesets/basic-jsp.xml
 37773 Thu Nov 10 20:48:06 EST 2011 rulesets/basic.xml
  3981 Thu Nov 10 20:48:06 EST 2011 rulesets/imports.xml
  3836 Thu Nov 10 20:48:06 EST 2011 rulesets/typeresolution.xml
  2755 Thu Nov 10 20:48:06 EST 2011 rulesets/unusedcode.xml
 25043 Thu Nov 10 20:48:06 EST 2011 rulesets/controversial.xml
  3045 Thu Nov 10 20:48:06 EST 2011 rulesets/coupling.xml
 13379 Thu Nov 10 20:48:06 EST 2011 rulesets/strictexception.xml
 12787 Thu Nov 10 20:48:06 EST 2011 rulesets/codesize.xml
 12484 Thu Nov 10 20:48:06 EST 2011 rulesets/junit.xml
 10784 Thu Nov 10 20:48:06 EST 2011 rulesets/optimizations.xml
  1412 Thu Nov 10 20:48:06 EST 2011 rulesets/basic-jsf.xml
  1396 Thu Nov 10 20:48:06 EST 2011 rulesets/favorites.xml

这些可能并非全部实施,但它是一个很好的起点。有关每个文件的说明,您可以查看文档here

答案 3 :(得分:1)

使用kuporific描述的自己的rulset.xml文件时,如:

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

gradle 1.10使用某种默认规则。是的,如果文件路径错误,它会抱怨,是的,如果内容无效,它会抱怨。但在检查期间,将应用一些默认规则集。所以我很惊讶这对你有用。

另见:http://forums.gradle.org/gradle/topics/with_gradle_1_5_pmd_applying_basic_rules_even_when_they_are_not_included_in_our_rule_set_files

解决方法不是使用ruleSetfiles,而是逐个引用它们:

ruleSets = [ "$projectRoot/buildtools/pmd-rules/strings.xml"]