我为sonarqube 4.5.1版创建了一个自定义插件。此插件包含基于PMD的新自定义规则。我已经按照一些例子(https://github.com/SonarSource/sonar-examples/tree/master/plugins)以正确的方式开发这个插件,但是我一直在构建我必须用声纳检查的项目,我有同样的错误: java.lang.IllegalArgumentException:无法设置不存在的属性' maxAuthorisedMethodsCount'在规则MaximumMethodsCountCheckRule 我的Sonar安装还安装了 sonar-pmd-plugin v2.2 和 sonar-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>
任何人都知道如何修复错误?请帮忙!非常感谢!!!
答案 0 :(得分:2)
您需要在definePropertyDescriptor(propertyDescriptor)
班级的构造函数中调用MaximumMethodsCountCheck
。
(我通过查看一些具有属性的规则来解决这个问题,例如NPathComplexityRule)