【问题标题】:ClassNotFoundException: Using custom java rule for PMD rulesetClassNotFoundException:为 PMD 规则集使用自定义 java 规则
【发布时间】:2017-09-21 22:26:12
【问题描述】:

我使用官方文档 (http://pmd.sourceforge.net/pmd-5.1.0/howtowritearule.html) 添加了自定义 PMD 规则

规则:

package com.comp.www.lty.awards.service.pmd;

import net.sourceforge.pmd.lang.ast.*;
import net.sourceforge.pmd.lang.java.ast.*;
import net.sourceforge.pmd.lang.java.rule.*;

public class WhileLoopsMustUseBracesRule extends AbstractJavaRule {
    public Object visit(ASTWhileStatement node, Object data) {
        Node firstStmt = node.jjtGetChild(1);
        if (!hasBlockAsFirstChild(firstStmt)) {
            addViolation(data, node);
        }
        return super.visit(node,data);
    }
    private boolean hasBlockAsFirstChild(Node node) {
        return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock));
    }
}

这是我的规则集 xml 文件:

<?xml version="1.0"?>
<ruleset name="My custom rules"
         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>While loop desc</description>
    <rule name="WhileLoopsMustUseBracesRule"
          message="Avoid using 'while' statements without curly braces"
          class="com.comp.www.lty.awards.service.pmd.WhileLoopsMustUseBracesRule">
        <description>
            Avoid using 'while' statements without using curly braces
        </description>
        <priority>3</priority>

        <example>
            <![CDATA[
    public void doSomething() {
      while (true)
          x++;
    }
]]>
        </example>
    </rule>
</ruleset>

我正在使用 mvn 来运行这些 pmd 规则。当我尝试使用 pmd jar 中的现有规则时工作正常,但每当我尝试使用上述自定义规则时,我都会收到 ClassNotFoundException。

堆栈跟踪:

java.lang.ClassNotFoundException: com.comp.www.lty.awards.service.pmd.WhileLoopsMustUseBracesRule
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
    at net.sourceforge.pmd.RuleSetFactory.parseSingleRuleNode(RuleSetFactory.java:377)
    at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:291)
    at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:242)
    at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:176)
    at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:171)
    at net.sourceforge.pmd.RuleSetFactory.parseRuleSetReferenceNode(RuleSetFactory.java:331)
    at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:289)
    at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:242)
    at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:176)
    at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:171)
    at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:135)
    at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:119)
    at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSets(RulesetsFactoryUtils.java:31)
    at net.sourceforge.pmd.processor.AbstractPMDProcessor.createRuleSets(AbstractPMDProcessor.java:54)
    at net.sourceforge.pmd.processor.MultiThreadProcessor.processFiles(MultiThreadProcessor.java:38)
    at net.sourceforge.pmd.PMD.processFiles(PMD.java:352)
    at org.apache.maven.plugin.pmd.PmdReport.executePmd(PmdReport.java:377)
    at org.apache.maven.plugin.pmd.PmdReport.executePmdWithClassloader(PmdReport.java:280)
    at org.apache.maven.plugin.pmd.PmdReport.canGenerateReport(PmdReport.java:254)
    at org.apache.maven.reporting.AbstractMavenReport.execute(AbstractMavenReport.java:119)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.executeForkedExecutions(MojoExecutor.java:365)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:199)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

虽然不是一个理想的解决方案,但即使我花时间将这个自定义规则类文件包含在 pmd jar (Custom Java PMD rule: Can't find the class CustomRule) 中,也无济于事。

pom.xml: http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 1.4.0.发布 lty-奖励服务 com.comp.www 战争 lty-奖励服务 Spring Boot 服务示例 1.0-快照 http://maven.apache.org 1.8

    <findbugs-maven-plugin.version>3.0.3</findbugs-maven-plugin.version>
    <findbugs.include.filter.location>buildtools/findbugs/include.xml</findbugs.include.filter.location>
    <findbugs.exclude.filter.location>buildtools/findbugs/exclude.xml</findbugs.exclude.filter.location>
    <maven-pmd-plugin.version>3.5</maven-pmd-plugin.version>
    <pmd.version>5.3.2</pmd.version>
    <pmdRule.version>5.1.0</pmdRule.version>
    <pmd.ruleset.location>buildtools/pmd-rules/ltyawardsservicepmd.xml</pmd.ruleset.location>
    <pmd.skip>false</pmd.skip>
    <pmd.typeResolution>true</pmd.typeResolution>

    <!-- spring boot -->
    <debug.port>5000</debug.port>
    <run.jvmArguments>-Dspring.profiles.active=dev -Dapplication.home=. -Dapplication.name=${project.name}
        -Dapplication.environment=dev -Dproject.name=${project.name} -Xdebug
        -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debug.port}
    </run.jvmArguments>

    <error-inspector.version>0.1.9</error-inspector.version>
    <platform-diagnostics.version>0.0.43</platform-diagnostics.version>

</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>net.sourceforge.pmd</groupId>
        <artifactId>pmd</artifactId>
        <version>${pmdRule.version}</version>
        <scope>system</scope>
        <systemPath>/Users/babu/.m2/repository/net/sourceforge/pmd/pmd/5.1.0/test.jar</systemPath>
    </dependency>
</dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.comp.prime.errorcatalog</groupId>
                                <artifactId>error-inspector</artifactId>
                                <version>${error-inspector.version}</version>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}/error-inspect</outputDirectory>
                                <destFileName>inspect.jar</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.comp.www.platform</groupId>
                                <artifactId>platform-diagnostics-main</artifactId>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}/error-inspect/lib</outputDirectory>
                                <destFileName>platform-diagnostics-main.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>enforce-versions</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireMavenVersion>
                                <message>.*** ERROR: Detected Maven version ${maven.version}, We need Maven 3.0.3 or
                                    higher ***.
                                </message>
                                <version>[3.0.3,)</version>
                            </requireMavenVersion>
                            <requireJavaVersion>
                                <message>.*** ERROR: Detected JDK version ${java.version}, We need JDK
                                    ${project.jdk.version} or higher ***.
                                </message>
                                <version>${project.jdk.version}</version>
                            </requireJavaVersion>
                        </rules>
                    </configuration>
                </execution>
                <execution>
                    <id>enforce-dont-exist</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireFilesDontExist>
                                <files>
                                    <file>${project.basedir}/src/main/scala</file>
                                </files>
                            </requireFilesDontExist>
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${project.jdk.version}</source>
                <target>${project.jdk.version}</target>
                <encoding>UTF-8</encoding>
            </configuration>
            <version>3.3</version>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>${scala-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <scalaCompatVersion>${scala.major.minor.version}</scalaCompatVersion>
                <jvmArgs>
                    <jvmArg>-Xms512m</jvmArg>
                    <jvmArg>-Xmx2048m</jvmArg>
                </jvmArgs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>${maven-pmd-plugin.version}</version>
            <dependencies>
                <dependency>
                    <groupId>net.sourceforge.pmd</groupId>
                    <artifactId>pmd-core</artifactId>
                    <version>${pmd.version}</version>
                </dependency>
                <dependency>
                    <groupId>net.sourceforge.pmd</groupId>
                    <artifactId>pmd-java</artifactId>
                    <version>${pmd.version}</version>
                </dependency>
                <dependency>
                    <groupId>WhileLoopsMustUseBracesRule</groupId>
                    <artifactId>pmd.ruleset</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <scope>system</scope>
                    <systemPath>/Users/babu/Downloads/cache.jar</systemPath>
                </dependency>
                <dependency>
                    <groupId>com.comp.www.platform</groupId>
                    <artifactId>platform-build-tools</artifactId>
                    <version>${platform-build-tools.version}</version>
                </dependency>
            </dependencies>
            <configuration>
                <targetJdk>${project.jdk.version}</targetJdk>
                <includeTests>true</includeTests>
                <skip>${pmd.skip}</skip>
                <sourceEncoding>${project.sourceEncoding}</sourceEncoding>
                <rulesets>
                    <ruleset>${pmd.ruleset.location}</ruleset>
                </rulesets>
            </configuration>
            <executions>
                <execution>
                    <id>cpd-report</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>check</goal>
                        <goal>cpd-check</goal>
                        <goal>pmd</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


    </plugins>
</build>
<!--<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jxr-plugin</artifactId>
            <version>2.3</version>
        </plugin>
    </plugins>
</reporting>-->

Here test.jar is my jar bundled (i.e. pmd jar unbundled, added class file & ruleset and again bundled it as test.jar). Although this is not how I want to use the rule. But if there is a better way to use pmd jar as it is & add a mvn dependency for new rule that is the ideal solution I'm looking for. Thanks!

【问题讨论】:

    标签: java maven classpath classnotfoundexception pmd


    【解决方案1】:

    您需要在 PMD 运行时将自定义规则 jar 添加到类路径中。您应该将您的自定义 PMD 规则捆绑在其自己的 Maven 依赖项中,然后您可以通过向插件配置添加依赖项将其包含在 PMD 插件类路径中,如下所示:

           <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-pmd-plugin</artifactId>
              <version>3.7</version>
              <dependencies>
                <dependency>
                  <groupId>com.foo.bar</groupId>
                  <artifactId>my-custom-pmd-rule</artifactId>
                  <version>1.0.0</version>
                </dependency>
              </dependencies>
              ...
            </plugin>
    

    有关插件类加载的更多信息,请参阅this page。看看它是怎么说的:

    请注意,插件类加载器既不包含当前项目的依赖项,也不包含其构建输出

    尝试将您的 pom.xml 文件更改为以下内容。请注意,我将您的 test.jar 依赖项移至插件依赖项,以便将其加载到插件的类路径中。请注意,您不必将规则与 PMD 捆绑在一起。您只需要将规则打包到自己的 jar 中,并将其包含在插件依赖项部分中。

    <findbugs-maven-plugin.version>3.0.3</findbugs-maven-plugin.version>
    
        <findbugs.include.filter.location>buildtools/findbugs/include.xml</findbugs.include.filter.location>
        <findbugs.exclude.filter.location>buildtools/findbugs/exclude.xml</findbugs.exclude.filter.location>
        <maven-pmd-plugin.version>3.5</maven-pmd-plugin.version>
        <pmd.version>5.3.2</pmd.version>
        <pmdRule.version>5.1.0</pmdRule.version>
        <pmd.ruleset.location>buildtools/pmd-rules/ltyawardsservicepmd.xml</pmd.ruleset.location>
        <pmd.skip>false</pmd.skip>
        <pmd.typeResolution>true</pmd.typeResolution>
    
        <!-- spring boot -->
        <debug.port>5000</debug.port>
        <run.jvmArguments>-Dspring.profiles.active=dev -Dapplication.home=. -Dapplication.name=${project.name}
            -Dapplication.environment=dev -Dproject.name=${project.name} -Xdebug
            -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debug.port}
        </run.jvmArguments>
    
        <error-inspector.version>0.1.9</error-inspector.version>
        <platform-diagnostics.version>0.0.43</platform-diagnostics.version>
    
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
    
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.comp.prime.errorcatalog</groupId>
                                    <artifactId>error-inspector</artifactId>
                                    <version>${error-inspector.version}</version>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${project.build.directory}/error-inspect</outputDirectory>
                                    <destFileName>inspect.jar</destFileName>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.comp.www.platform</groupId>
                                    <artifactId>platform-diagnostics-main</artifactId>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${project.build.directory}/error-inspect/lib</outputDirectory>
                                    <destFileName>platform-diagnostics-main.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>enforce-versions</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <message>.*** ERROR: Detected Maven version ${maven.version}, We need Maven 3.0.3 or
                                        higher ***.
                                    </message>
                                    <version>[3.0.3,)</version>
                                </requireMavenVersion>
                                <requireJavaVersion>
                                    <message>.*** ERROR: Detected JDK version ${java.version}, We need JDK
                                        ${project.jdk.version} or higher ***.
                                    </message>
                                    <version>${project.jdk.version}</version>
                                </requireJavaVersion>
                            </rules>
                        </configuration>
                    </execution>
                    <execution>
                        <id>enforce-dont-exist</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireFilesDontExist>
                                    <files>
                                        <file>${project.basedir}/src/main/scala</file>
                                    </files>
                                </requireFilesDontExist>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${project.jdk.version}</source>
                    <target>${project.jdk.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>${scala-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaCompatVersion>${scala.major.minor.version}</scalaCompatVersion>
                    <jvmArgs>
                        <jvmArg>-Xms512m</jvmArg>
                        <jvmArg>-Xmx2048m</jvmArg>
                    </jvmArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
    
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${maven-pmd-plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-core</artifactId>
                        <version>${pmd.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-java</artifactId>
                        <version>${pmd.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>WhileLoopsMustUseBracesRule</groupId>
                        <artifactId>pmd.ruleset</artifactId>
                        <version>0.0.1-SNAPSHOT</version>
                        <scope>system</scope><systemPath>/Users/babu/Downloads/cache.jar</systemPath>
                    </dependency>
                    <dependency>
                        <groupId>com.comp.www.platform</groupId>
                        <artifactId>platform-build-tools</artifactId>
                        <version>${platform-build-tools.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd</artifactId>
                        <version>${pmdRule.version}</version>
                        <scope>system</scope>
    
      <systemPath>/Users/babu/.m2/repository/net/sourceforge/pmd/pmd/5.1.0/test.jar</systemPath>
                    </dependency>
                </dependencies>
                <configuration>
                    <targetJdk>${project.jdk.version}</targetJdk>
                    <includeTests>true</includeTests>
                    <skip>${pmd.skip}</skip>
                    <sourceEncoding>${project.sourceEncoding}</sourceEncoding>
                    <rulesets>
                        <ruleset>${pmd.ruleset.location}</ruleset>
                    </rulesets>
                </configuration>
                <executions>
                    <execution>
                        <id>cpd-report</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>check</goal>
                            <goal>cpd-check</goal>
                            <goal>pmd</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    
    
        </plugins>
    </build>
    <!--<reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.3</version>
            </plugin>
        </plugins>
    </reporting>-->
    

    【讨论】:

    • 就我而言,我尝试将自定义规则类捆绑到现有的 pmd jar 中,并且该 jar 依赖项包含在我的 pom.xml 中(因为我能够从同一个 jar 中运行其他规则)。当我解开 jar 包时,自定义规则类文件存在,但在构建过程中没有被拾取。是的,我也做了 mvn clean。
    • 您是否将 PMD 作为 maven 插件运行?如果是这样,您捆绑在一起的自定义规则类 + PMD jar 需要在插件的依赖项中(如我的 sn-p 所示),而不是您的项目依赖项。查看 pom.xml 的相关部分会很有帮助
    • 是的,我正在为 PMD 使用 maven 插件。这是我正在使用的插件的 sn-p:
    • net.sourceforge.pmdpmd-core5.3.2com.comp.www.lty.awards.service.pmdWhileLoopsMustUseBracesRule1.0.0
    • 我不知道来自 pom.xml 的哪个部分。你能编辑你的答案并给出整个 pom.xml 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2016-02-12
    相关资源
    最近更新 更多