SonarQube-"The class '...' could not be matched to its original source file."

SonarQube - "The class '...' could not be matched to its original source file."

在我的 SonarQube Analysis 中,我发现了以下错误:

WARN: The class 'com.mycompany.bla.BlaTest' could not be matched to its original source file. It might be a dynamically generated class.
WARN: Failed to load the class file metadata
org.sonar.plugins.findbugs.resource.ClassMetadataLoadingException: ASM failed to load classfile metadata
    at org.sonar.plugins.findbugs.resource.DebugExtensionExtractor.getDebugExtFromClass(DebugExtensionExtractor.java:49)
    at org.sonar.plugins.findbugs.resource.ByteCodeResourceLocator.extractSmapLocation(ByteCodeResourceLocator.java:166)
    at org.sonar.plugins.findbugs.FindbugsSensor.execute(FindbugsSensor.java:171)
    at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
    at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:88)
    at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:82)
    at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:68)
    at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:88)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:180)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:302)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:297)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:295)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:271)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
    at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
    at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:121)
    at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:121)
    at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:71)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
    at com.sun.proxy.$Proxy0.execute(Unknown Source)
    at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:171)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:128)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:111)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalArgumentException: null
    at org.objectweb.asm.ClassReader.<init>(ClassReader.java:160)
    at org.objectweb.asm.ClassReader.<init>(ClassReader.java:143)
    at org.objectweb.asm.ClassReader.<init>(ClassReader.java:418)
    at org.sonar.plugins.findbugs.resource.DebugExtensionExtractor.getDebugExtFromClass(DebugExtensionExtractor.java:43)
... 35 common frames omitted

我发现错误发生在源位于 "testsrc/com/mycompany/..." 下的单元测试文件上。编译好的测试类位于"classes"

所以项目结构是:

myProject
    |- src
    |- testsrc
    |- classes

在我的 sonar-project.properties 中,我有以下配置:

sonar.tests=testsrc
sonar.sources=src

这里的正确解决方案是什么?从 FindBugs 分析中排除 Test-类 或将 testsrc 文件夹添加到分析源?

诀窍是向 sonarqube 添加 findbugsexclude.xml:

sonar.findbugs.excludesFilters=findbugsexclude.xml

并排除测试-类中的所有测试类和内部类:

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
    <Match>
        <Class name="~.*Test$"/>
    </Match>
    <Match>
        <Class name="~.*Test$.*$"/>
    </Match>
</FindBugsFilter>