maven - 无法设置不存在的属性 PMD sonarqube 插件

标签 maven sonarqube pmd

我为 sonarqube 版本 4.5.1 创建了一个自定义插件。该插件包含一个基于 PMD 的新自定义规则。我遵循了一些示例( https://github.com/SonarSource/sonar-examples/tree/master/plugins )以正确的方式开发此插件,但是在我构建必须使用 Sonar 检查的项目时,我遇到了相同的错误:java.lang.IllegalArgumentException:无法设置规则 MaximumMethodsCountCheckRule 上不存在属性“maxAuthorizedMethodsCount” 我的 Sonar 安装还安装了 sonar-pmd-plugin v2.2sonar-java-plugin v2.4。 下面是生成错误的代码

extensions.xml

<?xml version="1.0" encoding="UTF-8"?>
<rules>
    <rule>
    <key>MaximumMethodsCount</key>
    <name>Maximum Methods Count Check</name>
    <description>Massimo numero di metodi autorizzati per classe</description>

    <!-- path to definition -->
    <configKey>rulesets.xml/MaximumMethodsCountCheckRule</configKey>

    <!-- Default priority ("severity"). It can be changed while activating the rule in Quality profile -->
    <!-- Possible values are: INFO, MINOR, MAJOR, CRITICAL, BLOCKER. Default value is MAJOR -->
    <priority>MAJOR</priority>

    <!-- parameters available in administration console of Quality profiles -->
    <param>     
      <key>maxAuthorisedMethodsCount</key>
      <description>Numero massimo di metodi autorizzati per classe</description>
       <!-- default value is optional --> 
      <defaultValue>4</defaultValue>      
    </param>

  </rule>

</rules>

rulesets.xml

<?xml version="1.0"?>
<ruleset name="Custom 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>
        sonar plugin rulesets
    </description>

    <rule name="MaximumMethodsCountCheckRule"
          message="Superato Numero massimo di metodi autorizzati per classe. Trovati {0} metodi!"
          class="alm.plugin.rules.MaximumMethodsCountCheck">

      <description>
        Numero massimo di metodi autorizzati per classe
      </description>

      <priority>3</priority>

      <properties>
        <property name="maxAuthorisedMethodsCount" description="Maximum number of methods authorised">
        </property>
      </properties>      
      <example>
        <![CDATA[
                //Troppi metodi!
                public void metodo1(){}
                public void metodo2(){}
                public void metodo3(){}
                public void metodo4(){}
                .....
                public void metodo14(){}
        ]]>
      </example>      
    </rule>  
</ruleset>

MaximumMethodsCountCheck.java

import java.util.List;

import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Property;

public class MaximumMethodsCountCheck extends AbstractJavaRule {

    Logger logger = LoggerFactory.getLogger(getClass());

    private static final IntegerProperty propertyDescriptor = new IntegerProperty("maxAuthorisedMethodsCount", "Massimo numero di metodi", 0, 2, 4, 1.0f);

    @Override
    public Object visit(ASTClassOrInterfaceBody node, Object data) {    
        logger.info("---Analisi con MaximumMethodsCountCheck---");
        List<ASTMethodDeclaration> metodi = node.findChildrenOfType(ASTMethodDeclaration.class);
        if(metodi.size() > getProperty(propertyDescriptor)){            
            addViolation(data, node, ""+metodi.size());
        }
        return super.visit(node, data);
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ALM-soanrqube-plugin</groupId>
  <artifactId>ALM-soanrqube-plugin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>sonar-plugin</packaging>


  <name>SONARQUBE PLUGIN</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <dependencies>
        <dependency>
            <groupId>org.codehaus.sonar</groupId>
            <artifactId>sonar-plugin-api</artifactId>
            <version>4.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.sonar-plugins.java</groupId>
            <artifactId>sonar-java-plugin</artifactId>
            <type>sonar-plugin</type>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd</artifactId>
            <version>5.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>sonar-maven-plugin</artifactId>
          <version>2.4</version>
          <type>maven-plugin</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.sonar</groupId>
                <artifactId>sonar-packaging-maven-plugin</artifactId>
                <version>1.9</version>
                <extensions>true</extensions>
                <configuration>
                    <pluginClass>alm.plugin.PmdExtensionPlugin</pluginClass>
                    <basePlugin>pmd</basePlugin>
                    <pluginDescription>PlugIn per Sonar</pluginDescription>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- UTF-8 bundles are not supported by Java, so they must be converted during build -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>native2ascii-maven-plugin</artifactId>
                <version>1.0-beta-1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>native2ascii</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>  
</project>

有人知道如何修复该错误吗?请帮忙!非常感谢!!!

最佳答案

您需要在 MaximumMethodsCountCheck 类的构造函数中调用 definePropertyDescriptor(propertyDescriptor)

(我通过查看一些提供的具有属性的规则发现了这一点,例如 NPathComplexityRule )

关于maven - 无法设置不存在的属性 PMD sonarqube 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27703448/

相关文章:

java - Property [project] 标有相互矛盾的注释

java - 两个 Maven 错误 - 'Type Cannot Be Resolved' + aspect weaver.1.8.0.M1 missing

java - Maven 程序集为 tar 设置用户名

java - PMD 拦截器在 Eclipse IDE 上作为插件不可见

java - PMD规则 "Use Proper Class Loader"解释?

java - 在 Java : Cannot find symbol 中使用 Kotlin 类

sonarqube - Karma 为具有无效行号的角度项目生成 lcov 报告

jenkins - SonarQube 中未显示单元测试

ant - 使用 Jenkins、Perforce 和 Ant,我如何才能仅对自上次绿色构建以来已更改的文件运行 PMD?

java - Sonar 运行者抛出异常Ubuntu