SonarQube 5.6是否在使用sonar-plugin-api 4.5.2版开发的插件中执行装饰器?

时间:2016-11-17 19:41:44

标签: java c# sonarqube sonarqube-web

我安装了sonarqube 5.6,并下载了C#插件。

我决定扩展C#插件,所以我继续下载安装的插件代码(版本5.3.2)。

C#插件项目引用了sonar-plugin-api版本4.5.2

我添加了需要为项目级别计算的新指标,因此,根据文档,我创建了一个Decorator

public class CSharpMyDecorator implements Decorator{
    private static final Logger LOG = LoggerFactory.getLogger(CSharpMyDecorator.class);

    @Override
    public boolean shouldExecuteOnProject(Project arg0) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void decorate(Resource resource, DecoratorContext context) {

        LOG.info(resource.getName() + " Is Project: " + Scopes.isProject(resource) +" Is project not Module:" + Qualifiers.isProject(resource, false));


        if (Scopes.isProject(resource) && Qualifiers.isProject(resource, false)) {
            double files = 0d;
            double percentage = 0d;
            LOG.info("files: "+context.getMeasure(CoreMetrics.FILES).getValue());

            for (Measure measure : context.getChildrenMeasures(MyMetrics.FILES_DONT_PASS_FUNCTION_THRESHOLD)) {
                files += measure.getValue();
            }

            percentage = files / context.getMeasure(CoreMetrics.FILES).getValue();
            context.saveMeasure(MyMetrics.PERC_FILES_DONT_PASS_FUNCTION_THRESHOLD, percentage);
        }

    }

}

我在插件中添加了这个类:

public class CSharpPlugin extends SonarPlugin {

  public static final String LANGUAGE_KEY = "cs";
  public static final String LANGUAGE_NAME = "C#";

  public static final String FILE_SUFFIXES_KEY = "sonar.cs.file.suffixes";
  public static final String FILE_SUFFIXES_DEFVALUE = ".cs";

  public static final String CSHARP_WAY_PROFILE = "Sonar way";

  public static final String REPOSITORY_KEY = "csharpsquid";
  public static final String REPOSITORY_NAME = "SonarQube";

  public static final String IGNORE_HEADER_COMMENTS = "sonar.cs.ignoreHeaderComments";
  public static final String NUMBER_FUNCTION_THRESHOLD = "sonar.cs.numberFunctionThreshold";
  public static final String NUMBER_LOC_THRESHOLD = "sonar.cs.numberLocThreshold";

  @Override
  public List getExtensions() {
    ImmutableList.Builder builder = ImmutableList.builder();

    builder.add(
      CSharp.class,
      CSharpSonarRulesDefinition.class,
      CSharpSonarWayProfile.class,
      CSharpCommonRulesEngine.class,
      CSharpCommonRulesDecorator.class,
      CSharpSourceCodeColorizer.class,
      RuleRunnerExtractor.class,
      CSharpSensor.class,
      CSharpCPDMapping.class,
      SonarLintProfileExporter.class,
      SonarLintFakeProfileImporter.class,
      RoslynProfileExporter.class,
      MyMetrics.class,
      CSharpMyDecorator.class
      );

    builder.addAll(CSharpFxCopProvider.extensions());
    builder.addAll(CSharpCodeCoverageProvider.extensions());
    builder.addAll(CSharpUnitTestResultsProvider.extensions());
    builder.addAll(CSharpMsBuildIntegrationProvider.extensions());
    builder.addAll(RoslynProfileExporter.sonarLintRepositoryProperties());

    return builder.build();
  }

}

执行分析时,既未创建指标,也未存储指标。

查看生成的日志(我启用了详细选项),我没有找到任何对Decorators执行的引用。甚至不是装饰器中代码所期望的LOG条目。

我正在使用MSBuild.SonarQube.Runner.exe版本2.0

为什么没有调用装饰器?这是SonarQube版本的问题吗?

1 个答案:

答案 0 :(得分:1)

扩展点Decorator在版本5.2中被删除,因为它不是为5.x系列中最想要的功能之一而设计的:从数据库中隔离扫描仪。

可以在http://docs.sonarqube.org/display/DEV/API+Changes和小组https://groups.google.com/forum/#!forum/sonarqube找到更多详细信息。