简体   繁体   English

SonarQube无法识别自定义PomCheck规则

[英]SonarQube doesn't recognize custom PomCheck rule

Running SonarQube 6.4 using Java plugin 4.9.0.9858.I have written a rule to confirm that maven projects developed internally are importing a parent POM file that contains standard version numbers for various libraries. 使用Java插件运行SonarQube 6.4 4.9.0.9858.I编写了一条规则来确认内部开发的maven项目正在导入包含各种库的标准版本号的父POM文件。 I've coded, unit tested, deployed, and activated the rule in the Quality Profile successfully. 我已成功编码,单元测试,部署和激活质量配置文件中的规则。 However when I run a scan against a maven project using that Quality Profile, the rule is not fired. 但是,当我使用该质量配置文件对maven项目运行扫描时,不会触发该规则。

I can see class org.sonar.java.xml.XmlAnalyzer decides which rules are fired for xml files in general and pom files in particular. 我可以看到类org.sonar.java.xml.XmlAnalyzer决定为一般的xml文件和特别是pom文件触发哪些规则。 Specifically, XMLAnalyzer selects all rules that are instances of org.sonar.java.xml.maven.PomChecks to apply to pom.xml files. 具体来说,XMLAnalyzer选择所有作为org.sonar.java.xml.maven.PomChecks实例的org.sonar.java.xml.maven.PomChecks来应用于pom.xml文件。

Problem is, my custom rule is an instance of the class org.sonar.java.xml.maven.PomCheck , as follows 问题是,我的自定义规则org.sonar.java.xml.maven.PomCheck一个实例,如下所示

import org.sonar.java.xml.maven.PomCheck;

...snip...

@Rule( key = "UseParentPOM" )

public class UseParentPOM implements PomCheck {

...snip...

This "implements PomCheck" method is exactly how java-plugin supplied rules like GroupIdNamingConventionCheck define themselves as PomChecks - and they are being are selected during my scanning runs. 这种“实现PomCheck”的方法是完全一样的Java插件提供的规则如何GroupIdNamingConventionCheck自己定义为PomChecks -他们正在被在我的扫描操作中选出的。 I've checked that I'm compiling my custom rules using the same plugin library version that the SQ installation is using. 我已经检查过我正在使用SQ安装所使用的相同插件库版本编译我的自定义规则。 And running sonar-scanner-debug and attaching to the running process shows my UseParentPOM rule is in fact in the visitors list XMLAnalyzer uses to search for candidate rules. 并且运行sonar-scanner-debug并附加到正在运行的进程显示我的UseParentPOM规则实际上是XMLAnalyzer用于搜索候选规则的访问者列表。 But the specific condition of "visitor instanceof PomCheck" returns false. 但是“访问者实例PomCheck”的具体情况返回false。 Ergo, my rule isn't added to the pom rules list and isn't fired when the pom.xml is scanned. 因此,我的规则未添加到pom规则列表中,并且在扫描pom.xml时不会触发。

Clearly my rule class isn't the PomCheck that XMLAnalyzer is expecting, but what am I doing wrong to make it an instance of PomCheck? 很明显,我的规则类不是XMLAnalyzer所期望的PomCheck,但是我把它作为PomCheck的实例做错了什么?

UPDATE UPDATE

Some further digging shows that the .jar file providing the class, java-frontend-4.9.0.9858.jar, is in both the base sonar-java-plugin-4.9.0.9858.jar and my custom-java-rules-1.0-SNAPSHOT.jar. 进一步挖掘显示提供类java-frontend-4.9.0.9858.jar的.jar文件位于基本声纳-java-plugin-4.9.0.9858.jar和我的custom-java-rules-1.0-SNAPSHOT 。罐。 My debugging runs show that SonarQube appears to spin up separate classloaders for each plugin .jar found in the extensions directory. 我的调试运行显示,SonarQube似乎为扩展目录中的每个插件.jar启动了单独的类加载器。 So there are indeed, as far as SQ is concerned, 2 'org.sonar.java.xml.maven.PomCheck' classes. 因此,就SQ而言,确实有2'org.sonar.java.xml.maven.PomCheck'类。 Hence my original issue. 因此我原来的问题。

I therefor attempted to remove java-frontend-4.9.0.9858.jar (and thus the PomCheck class) from my custom rules .jar by scoping all the sonar related dependencies as <provided>. 我因此尝试从我的自定义规则.jar中删除java-frontend-4.9.0.9858.jar(以及PomCheck类),将所有与声纳相关的依赖关系作为<provided>进行定义。 My hope was the resulting SQ process would then have 1 PomCheck class to rule them all. 我希望得到的SQ进程会有1个PomCheck类来统治它们。 However the actual result is SQ doesn't even start up, with the web server process failing when attempting to load my custom rule classes due to not being able to find (TA DA) class PomCheck. 然而,实际结果是SQ甚至没有启动,因为无法找到(TA DA)类PomCheck而尝试加载我的自定义规则类时Web服务器进程失败。

I appear therefor to have reached an insoluble dilemma - if I include PomCheck in my custom .jar, SQ starts fine but doesn't recognize my custom rule as a "real" PomCheck. 因为我在我的自定义.jar中包含了PomCheck,所以我觉得它已经达到了一个不可解决的困境 - SQ开始很好但是我不认识我的自定义规则是一个“真正的”PomCheck。 If I don't include PomCheck in my custom .jar, SQ won't start at all. 如果我在我的自定义.jar中不包含PomCheck,则SQ将无法启动。 So now I'm really in a quandary - pls help. 所以现在我真的陷入了困境 - 请帮忙。

ADDITIONAL DETAILS 额外细节

Re: Nicholas B's request, the code registering my custom rule is as follows Re:Nicholas B的请求,注册我的自定义规则的代码如下

public final class MyRulesList {

...snip...

public static List<Class<? extends JavaCheck>> getChecks() {
   return ImmutableList.<Class<? extends JavaCheck>>builder().addAll(getJavaChecks()).addAll(getJavaTestChecks()).build();
}
public static List<Class<? extends JavaCheck>> getJavaChecks() {
    return ImmutableList.<Class<? extends JavaCheck>>builder()
        .add(PackageNaming.class)
        .add(LoggingLevels.class)
        .add(UseParentPOM.class)
        .build();
}
... snip ...

} }

This code was copied straight from the Writing Custom Java Rules 101 site. 此代码直接从编写自定义Java规则101站点复制。 All 3 custom classes are properly registered as far as I can see - I can see them in the web console UI and add them to a Quality Gate there. 据我所知,所有3个自定义类都已正确注册 - 我可以在Web控制台UI中看到它们并将它们添加到Quality Gate。 The only difference between rules PackageNaming/LoggingLevels and rule UseParentPOM is that UseParentPOM implements the PomCheck interface, not the JavaCheck. 规则PackageNaming / LoggingLevels和规则UseParentPOM之间的唯一区别是UseParentPOM实现PomCheck接口,而不是JavaCheck。 However since class PomCheck is nothing but a wrapper around class JavaCheck, this seemed to be the correct way to register the UseParentPom class, eg just like any other JavaCheck. 然而,由于类PomCheck只是类JavaCheck的包装器,这似乎是注册UseParentPom类的正确方法,例如就像任何其他JavaCheck一样。 But maybe not? 但也许不是吗?

In short: SonarJava does not support custom checks for POM files (ie custom PomCheck ). 简而言之:SonarJava不支持自定义检查POM文件(即自定义PomCheck )。

More details 更多细节

Per Java custom rules tutorial , custom rules must be activated by feeding them to getJavaChecks (checked against source files) or getJavaTestChecks (checked against test files). 根据Java自定义规则教程 ,必须通过将它们提供给getJavaChecks (针对源文件进行检查)或getJavaTestChecks (针对测试文件进行检查)来​​激活自定义规则。 The thing is that pom.xml files don't fall into any of those two categories, they rather belong to the 'XML files bucket' against which are checked specific rules. 问题是pom.xml文件不属于这两个类别中的任何一个,它们属于“XML文件桶”,对其进行特定的规则检查。

A good way to visualize this is to glance at SonarJava's CheckList.java , note the dedicated getXmlChecks and getMavenChecks . 想象这样的一个好方法是在SonarJava的目光CheckList.java ,注意专用getXmlChecksgetMavenChecks Those are the checks that are actually ran against XML files indexed by the Scanner. 这些是实际针对Scanner索引的XML文件运行的检查。

Concretely speaking 具体来说

While you have freedom to add custom rules to getJavaChecks or getJavaTestChecks , SonarJava APIs do not support adding rules to getMavenChecks (you can try but it'll effectively do nothing). 虽然您可以自由地为getJavaChecksgetJavaTestChecks添加自定义规则,但SonarJava API不支持向getMavenChecks添加规则(您可以尝试但它实际上什么都不做)。 Your overall analysis is quite on spot, but the fact is that only packages containing api are accessible through the classloader ( example ), not the case of PomCheck . 您的整体分析非常明确,但事实是只有包含api的软件包才能通过类加载器( 示例 )访问,而不是PomCheck的情况。

I'm not aware of any plans for changes on this front. 我不知道在这方面有任何改变的计划。

'Outside the box thinking' suggestion “开箱即用思考”的建议

SonarXML supports custom rules using XPath expressions (see extension guide ). SonarXML使用XPath表达式支持自定义规则(请参阅扩展指南 )。 With a bit of XPath gymnastic, you could consider extending rule xml:XPathCheck - Track breaches of an XPath rule . 有了一点XPath体操,你可以考虑扩展规则xml:XPathCheck - 跟踪违反XPath规则的行为

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

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