简体   繁体   English

自定义Java PMD规则:找不到类CustomRule

[英]Custom Java PMD rule: Can't find the class CustomRule

I'm trying to write custom PMD rules in Java. 我正在尝试用Java编写自定义PMD规则。 I have created a custom ruleset that looks like this: 我创建了一个自定义规则集,如下所示:

<?xml version="1.0"?>
<ruleset name="Custom 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>
        My custom rules
    </description>
    <rule name="CustomRule"
        message="Custom message"
        class="mwe.CustomRule">
        <description>
            Custom description
        </description>
        <priority>3</priority>
    </rule>

</ruleset>

I call pmd.bat using this Java class: 我使用以下Java类调用pmd.bat

package mwe;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class PmdStarter {

    public static void main(String[] args) {
        callPmd();
    }

    public static void callPmd() {
        String pmdPath = "pmd-src-5.0.5/bin/pmd.bat";
        String checkThisCode = "checkThisCode/";
        String customRuleSet = "pmd-src-5.0.5/src/main/resources/rulesets/java/customRules.xml";
        String[] command = { pmdPath, "-dir", checkThisCode, "-rulesets",
                customRuleSet };

        ProcessBuilder pb = new ProcessBuilder(command);

        try {
            InputStream is = pb.start().getInputStream();
            String output = convertStreamToString(is);
            is.close();
            System.out.println(output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static String convertStreamToString(InputStream is) {
        Scanner s = new Scanner(is);
        s.useDelimiter("\\A");
        String streamContent = s.hasNext() ? s.next() : "";
        s.close();
        return streamContent;
    }
}

Unfortunately my custom rule written in Java can't be found; 不幸的是,找不到我用Java编写的自定义规则。 this is the message from PmdStarter: 这是来自PmdStarter的消息:

Couldn't find the class mwe.CustomRule 找不到类mwe.CustomRule

This is my (minimal) custom rule: 这是我的(最小)自定义规则:

package mwe;

import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;

public class CustomRule extends AbstractJavaRule {

    public Object visit(ASTWhileStatement node, Object data) {
        System.out.println("Hello PMD");
        return data;
    }
}

This is my project structure in Eclipse: 这是我在Eclipse中的项目结构:

Eclipse中的PMD测试项目

I have read here that this sort of error seems to be a classpath error. 我在这里已经读到这种错误似乎是类路径错误。 After reading this I have placed CustomRule.class in a couple of directories within the project, hoping in vain that PMD would find it. 阅读此内容后,我将CustomRule.class放置在项目中的几个目录中,希望白白地让PMD找到它。

My question is: How can I make PMD execute my CustomRule ? 我的问题是:如何使PMD执行我的CustomRule

I've found a way: PMD will find your rule if you put it in a jar and place that jar in pmd-src-5.0.5\\lib . 我找到了一种方法:如果将规则放在jar中并将该jar放在pmd-src-5.0.5\\lib PMD会找到您的规则。

This is the shell command I use to make my rule accessible to PMD (my rules are in package mwe ). 这是我用来使PMD可以访问我的规则的shell命令(我的规则在package mwe )。 Note that this is issued from my projects bin folder: 请注意,这是从我的项目bin文件夹发出的:

C:\Users\me\dev\CodeCheckerMWE\bin>jar -cf ..\pmd-src-5.0.5\lib\myrules.jar mwe

I have experimented with the classpath and the -auxclasspath option of PMD to make it accept different locations but with no success. 我已经尝试使用PMD的classpath和-auxclasspath选项使它接受不同的位置,但是没有成功。

If anyone has a different (preferably more elegant) solution to this, please let me/us know. 如果有人对此有不同的解决方案(最好是更优雅),请让我/我们知道。

By the way, for anyone else having problems, it looks like the instructions under http://pmd.sourceforge.net/snapshot/customizing/howtowritearule.html are outdated. 顺便说一句,对于其他任何有问题的人,看起来http://pmd.sourceforge.net/snapshot/customizing/howtowritearule.html下的说明已经过时。

  • You need to add a description to the first child of the ruleset tag 您需要向规则集标记的第一个子项添加描述

    <description> Avoid using 'while' statements without using curly braces </description>

  • The syntax for actually running pmd seems to have changed, eg 实际运行pmd的语法似乎已更改,例如

./run.sh pmd -d /home/markus/project/src/de/ -f xml -R /home/markus/project/myrules.xml

  • You need to put the jar containing the java rule class under the lib folder of extracted binary zip of PMD. 您需要将包含Java规则类的jar放在提取的PMD二进制zip的lib文件夹下。 (See answer from Matthias Braun) (请参阅Matthias Braun的回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM