【问题标题】:How to create custom rule for PMD with Maven?如何使用 Maven 为 PMD 创建自定义规则?
【发布时间】:2014-11-06 17:47:37
【问题描述】:

我正在尝试根据“Jenkins Continuous Integration Cookbook”一书中的示例实现一个新的 XPath PMD 规则。

我的 pom 文件的相关部分:

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
            <version>2.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <configuration>
                <targetJdk>1.6</targetJdk>
                <format>xml</format>
                <rulesets>
                    <ruleset>password_ruleset.xml</ruleset>
                </rulesets>
            </configuration>
        </plugin>
    </plugins>
</reporting>

我的 maven 项目的根目录中有文件“password_ruleset.xml”,它看起来如下:

<?xml version="1.0"?>
<ruleset name="STUPID PASSWORDS ruleset"
    xmlns="http://pmd.sf.net/ruleset/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
    xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  <description>
  Lets find stupid password examples
  </description>
<rule name="NO_PASSWORD"
  message="If we see a PASSWORD we should flag"
  class="net.sourceforge.pmd.rules.XPathRule">
  <description>
  If we see a PASSWORD we should flag
  </description>
  <properties>
    <property name="xpath">
    <value>
<![CDATA[
//VariableDeclaratorId[@Image='PASSWORD']
]]>

执行时出现以下错误:

执行 PMD 失败:找不到类 net.sourceforge.pmd.rules.XPathRule

检查哪些库包含此类,我意识到它本身就是“pmd”。我试图将依赖项添加到依赖项部分,但没有运气。

我应该在哪里改变什么来克服这个问题?

请在 github 中查看整个设置: https://github.com/dave00/pmdcustomrule

【问题讨论】:

  • 您使用的是哪个版本的 PMD 插件?
  • 我使用的是 maven-pmd-plugin 3.2。请参阅下面的解决方案。

标签: java maven pmd


【解决方案1】:

我的一个朋友通过 github 解决了我的问题: https://github.com/mnyeste/pmdcustomrule/commit/ad2f04e33d2a5a04ef95d059d64a258ebca5b7be

总结:

PMD API 更改 4.3 -> 5.0 类 net.sourceforge.pmd.rules.XPathRule 已重命名为 net.sourceforge.pmd.lang.rule.XPathRule

Maven PMD 插件版本 3.2 正在使用 PMD 5.1.2

任何感兴趣的人现在都可以从 github 中提取我的示例项目来查看它的工作情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2013-02-14
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多