自定义Java PMD规则:找不到类CustomRule [英] Custom Java PMD rule: Can't find the class CustomRule
问题描述
我正在尝试用Java编写自定义PMD规则。
我创建了一个自定义规则集,如下所示:
<?xml version = 1.0?> ;
< ruleset name =自定义规则集
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_0.xsd\"> ;
<说明>
我的自定义规则
< / description>
<规则名称= CustomRule
message =自定义消息
class = mwe.CustomRule>
< description>
自定义说明
< / description>
< priority> 3< / priority>
< / rule>
< / ruleset>
我使用此Java调用 pmd.bat
类:
package mwe;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
公共类PmdStarter {
public static void main(String [] args){
callPmd();
}
public static void callPmd(){
String pmdPath = pmd-src-5.0.5 / bin / pmd.bat;
字符串checkThisCode = checkThisCode /;
字符串customRuleSet = pmd-src-5.0.5 / src / main / resources / rulesets / java / customRules.xml;
String []命令= {pmdPath, -dir,checkThisCode, -rulesets,
customRuleSet};
ProcessBuilder pb =新的ProcessBuilder(命令);
试试{
InputStream是= pb.start()。getInputStream();
字符串输出= convertStreamToString(is);
is.close();
System.out.println(output);
} catch(IOException e){
e.printStackTrace();
}
}
静态字符串convertStreamToString(InputStream is){
Scanner s = new Scanner(is);
s.useDelimiter( \\A);
字符串streamContent = s.hasNext()吗? s.next():;
s.close();
return streamContent;
}
}
不幸的是,我用Java编写的自定义规则不能发现;这是来自PmdStarter的消息:
找不到类mwe.CustomRule
这是我的(最小)自定义规则:
package mwe;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
公共类CustomRule扩展AbstractJavaRule {
公共对象访问(ASTWhileStatement节点,对象数据){
System.out.println( Hello PMD);
个返回数据;
}
}
这是我在Eclipse中的项目结构:
我已阅读此处这种错误似乎是类路径错误。
阅读此之后,我将 CustomRule.class
在项目中的两个目录中,希望徒劳的是PMD会找到它。
我的问题是:如何使PMD执行我的 CustomRule
?
我已经找到了一种方法:如果将
放在一个jar中并将该jar放在 pmd-src-5.0.5\lib
中,PMD会找到您的规则。 / p>
这是我用来使我的规则可用于PMD的shell命令(我的规则在软件包 mwe
中)。请注意,这是从我的项目 bin
文件夹发出的:
C: \用户\me\dev\CodeCheckerMWE\bin> jar -cf ..\pmd-src-5.0.5\lib\myrules.jar mwe
我已经尝试了PMD的classpath和 -auxclasspath
选项,以使其接受不同的位置
如果有人对此有不同的解决方案(最好是更优雅),请告诉我/我们。
I'm trying to write custom PMD rules in Java. 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:
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; this is the message from PmdStarter:
Couldn't find the class 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:
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.
My question is: How can I make PMD execute my 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
.
This is the shell command I use to make my rule accessible to PMD (my rules are in package mwe
). Note that this is issued from my projects bin
folder:
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.
If anyone has a different (preferably more elegant) solution to this, please let me/us know.
这篇关于自定义Java PMD规则:找不到类CustomRule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!