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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20710704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:44:44  来源:igfitidea点击:

Gradle's PMD plugin: what are acceptable arguments?

javagradlepmd

提问by kuporific

  • Java 1.7.0_40
  • Gradle 1.10
  • Java 1.7.0_40
  • 摇篮 1.10

I've never used Gradle's PMD plugin and I'm running into trouble trying to add rule sets to my build.gradle. The Pmd documentationis not clear about what the valid values of ruleSetsare. Their example is ruleSets = ["basic", "braces"]and they link to the "official list". There's not much to go on, unfortunately.

我从未使用过 Gradle 的 PMD 插件,并且在尝试将规则集添加到我的build.gradle. 该PMD文件并未明确什么的有效值ruleSets是。他们的例子是ruleSets = ["basic", "braces"],他们链接到“官方列表”。不幸的是,没有太多事情要做。

I was guessing the section title maps to the valid string somehow? Like,

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

But what about things like "Empty Code (java)"?

但是诸如“空代码(java)”之类的东西呢?

Here's a working build.gradleexample:

这是一个工作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 spits out the following error:

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 --stacktraceor --debugas suggested doesn't seem to yield anything useful...

使用--stacktrace--debug按照建议运行似乎没有产生任何有用的东西......

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

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

How are ruleSetssupposedto be declared?

ruleSets应该怎么申报?



Edit:

编辑:

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

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

apply plugin: 'pmd'

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

And the rule set file looks something like this:

规则集文件如下所示:

Note: This exaple is written for Gradle 1.10. Newer versions of Gradle (circa 2.0) use a newer version of PMD; therefore, many of the rulesetspaths changed. So rulesets/logging-java.xmlis now found in rulesets/java/logging-java.xml, for example.

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

采纳答案by WillBD

I realize this is a huge edit, but it's essentially a different answer. So after speaking to you and playing around with it, I've determined that the Gradle plugin uses a slightly older version of the pmd library than is published (namely, version 4.3); however, there are a few rulesets missing from the plugin since the most recent pmd version is 5.0.5 which breaks with a NullPointerExceptionwith Gradle and Java. Now, after writing possibly the most syntactically correct and painstaking Hello World Java program of my life to test all of these, I've compiled every single Java rule-set that works with the Gradle plugin at the moment:

我意识到这是一个巨大的编辑,但它本质上是一个不同的答案。因此,在与您交谈并试用之后,我确定 Gradle 插件使用的 pmd 库版本比已发布的版本稍旧(即 4.3 版);然而,插件中缺少一些规则集,因为最新的 pmd 版本是 5.0.5,它NullPointerException与 Gradle 和 Java中断。现在,在编写了我一生中可能最语法正确和最辛苦的 Hello World Java 程序来测试所有这些之后,我已经编译了目前与 Gradle 插件一起使用的每个 Java 规则集:

here's the Main.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");
    }
}

here's the build.gradle:

这是 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()
}

and now you might be wondering, which rulesets AREN'T supported yet? well the answer is:

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

  1. "comments"
  2. "empty"
  3. "unnecessary"
  1. "comments"
  2. "empty"
  3. "unnecessary"

Trust me when I say, the rest of the rules work flawlessly. They tore me apart when writing a Hello World. So, I hope this helps, the directory that has all of the Java rulesets.xml files defined is at: link to pmd's github java ruleset directoryI ignore the migrate ones, because they didn't work. I think they're for something specific.

相信我,其余的规则都完美无缺。在编写 Hello World 时,他们把我撕裂了。所以,我希望这会有所帮助,定义了所有 Java rulesets.xml 文件的目录位于:链接到 pmd 的 github java 规则集目录我忽略迁移的那些,因为它们不起作用。我认为他们是为了特定的东西。

Good luck, and I would bring up the issue of the missing rulesets on the gradle forums to petition to get them added, or update the version. Or you could custom compile the plugin and link it to the newer pmd version if you are really desperate for the missing rulesets.

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

回答by icyerasor

When using your own rulset.xml file as described by kuporific, like:

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

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

gradle 1.10 uses some kind of default rules. Yes it does complain if the filepath is wrong, yes it complains if the contents are invalid. But during the checks some default ruleset will be applied. So I'm kind of suprised this worked for you.

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

Also see: 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

另请参阅: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

Workaround is not to use ruleSetfiles, but reference them one by one:

解决方法不是使用规则集文件,而是一一引用它们:

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

回答by dev

To add to the other excellent answers here. After applying pmd to your gradle build and invoking it via gradle pmdMain, the pmd jar will be downloaded to your gradle cache. From there you can run:

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

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

And you will get the output:

你会得到输出:

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

These may not all be implemented, but it is a good starting point. For a description of each file, you can check the documentation here.

这些可能无法全部实施,但这是一个很好的起点。有关每个文件的说明,您可以在此处查看文档。

回答by Tuxdude

The latest version of PMD(5.1.3when writing this answer) is supported by gradle. The rulesets need to be prefixed by a java-

gradle 支持PMD(5.1.3在编写此答案时)的最新版本。规则集需要以前缀为前缀java-

I tested this with gradle-1.12

我测试了这个 gradle-1.12

To use PMD 5.1.3with gradle, the following configuration defines all the possibles rulesets I could find:

为了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'
            ]
}

Reference:http://forums.gradle.org/gradle/topics/_pmdtest_fails_with_cant_find_resource_null_when_rulesets_include_braces_gradle_2_0_rc_1

参考:http : //forums.gradle.org/gradle/topics/_pmdtest_fails_with_cant_find_resource_null_when_rulesets_include_braces_gradle_2_0_rc_1