简体   繁体   English

如何在Jenkins PMD插件中添加自定义PMD规则?

[英]How to add a custom PMD rule in Jenkins PMD plugin?

I want to use a custom rule in PMD. 我想在PMD中使用自定义规则。 I installed the PMD-plugin in jenkins, I also created a jar with : 我在jenkins中安装了PMD插件,还创建了一个jar:

  1. A ruleset file 规则集文件
  2. The java code for the custom rule (the custom rule extends from AbstractJavaRule). 自定义规则的Java代码(自定义规则扩展自AbstractJavaRule)。

I added this jar in the lib directory of the PMD plugin (jenkins/plugins/pmd/WEB-INF/lib). 我将此罐子添加到了PMD插件的lib目录(jenkins / plugins / pmd / WEB-INF / lib)中。

I also added this in the pom.xml of a project (to test PMD): 我还将其添加到项目的pom.xml中(以测试PMD):

        <reporting>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-pmd-plugin</artifactId>
                    <version>2.7.1</version>
                    <configuration>
                        <linkXRef>false</linkXRef>
                        <targetJdk>1.6</targetJdk>
                        <rulesets>
                                 <ruleset>/rulesets/basic.xml</ruleset>
                        </rulesets>                  
                    </configuration>
                </plugin>
            </plugins>
    </reporting> 

How can I tell to the jenkins PMD-plugin to use my new JAVA rule ? 如何告诉詹金斯PMD插件使用我的新JAVA规则?

I think, the first step is, to get the custom PMD rule working without Jenkins. 我认为,第一步是在没有Jenkins的情况下使自定义PMD规则生效。 Jenkins will trigger the maven build, which will trigger PMD to create the report file pmd.xml and then the Jenkins PMD plugin will parse this pmd.xml file. Jenkins将触发Maven构建,这将触发PMD创建报告文件pmd.xml ,然后Jenkins PMD插件将解析此pmd.xml文件。

  1. Maven - you've added the maven-pmd-plugin in the reporting section. Maven-您已在报告部分中添加了maven-pmd-plugin。 This means, you'll need to generate the site, in order to get the PMD reports (run mvn site . It is also possible to run PMD during the build, eg run the pmd:pmd in phase verify . See also PMD Mojo Documentation . 这意味着,您需要生成站点以获取PMD报告(运行mvn site 。也可以在构建过程中运行PMD,例如,在阶段verify运行pmd:pmd 。另请参见PMD Mojo文档

  2. In order to have your custom rule and custom ruleset available, you'll need to add your jar as an additional dependency to the maven-pmd-plugin section, eg 为了使您的自定义规则和自定义规则集可用,您需要将jar作为附加依赖项添加到maven-pmd-plugin部分,例如

     ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>2.7.1</version> ... <dependencies> <dependency> <groupId>com.your.custom.rule</groupId> <artifactId>custom-rule</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies> </plugin> 
  3. Verify, that a mvn site will generate the pmd report, usually under target/pmd.xml . 验证mvn site通常在target/pmd.xml下会生成pmd报告。

  4. It might be, that the Jenkins PMD plugin now just shows your rule, but without the documentation like code examples and description. Jenkins PMD插件现在可能只是显示您的规则,而没有代码示例和说明之类的文档。 This is implemented in the PMDMessages class and uses "RegisteredRuleSets" feature of PMD, which is not good documented, but the sourcecode is here (for PMD 4.3). 这是在PMDMessages类中实现的,并使用了PMD的“ RegisteredRuleSets”功能,虽然没有很好的文档说明,但是源代码在这里 (对于PMD 4.3)。

  5. Add a properties file named rulesets.properties in the directory rulesets in your jar. 在jar的目录rulesets集中添加一个名为rulesets.properties的属性文件。 You would place this file in the source tree usually under src/main/resources/rulesets/rulesets.properties . 您通常将此文件放置在源树中的src/main/resources/rulesets/rulesets.properties The file needs to have the following content 该文件需要具有以下内容

     rulesets.filenames=rulesets/custom-ruleset-1.xml,rulesets/custom-ruleset-2.xml 

It contains a comma-separated list of ruleset files. 它包含逗号分隔的规则集文件列表。 If you add this file in your jar, then the Jenkins PMD Report should contain the info you want. 如果将此文件添加到jar中,则Jenkins PMD报告应包含所需的信息。

Please note, that there is already a newer version of the Maven PMD Plugin available, currently the latest version is 3.5. 请注意,已经有较新版本的Maven PMD插件可用,当前最新版本为3.5。 This version is based on PMD 5 which introduced multiple languages and the path to the rulesets.properties file changes to rulesets/java/rulesets.properties in case of java. 该版本基于PMD 5,该版本引入了多种语言,如果使用Java,则rulesets.properties文件的路径将更改为rulesets/java/rulesets.properties

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

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