SonarQube + JaCoCo + TeamCity 错误的模块名称

SonarQube + JaCoCo + TeamCity wrong modules names

我有一个多模块 Maven 项目,其中使用了带有最新 JaCoCo 的 SonarQube v 5.6.6。

配置如下:

<properties>
    <sonar-maven-plugin.version>3.3.0.603</sonar-maven-plugin.version>
    <jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
    <sonar-jacoco-listeners.version>4.10.0.10260</sonar-jacoco-listeners.version>
    <maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
    <sonar.java.binaries>${basedir}</sonar.java.binaries>
    <!--sonar properties-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <sonar.projectName>GR</sonar.projectName>
    <!-- Use the following property to force single language analysis, omit it to perform Multilanguage analysis -->
    <sonar.language>java</sonar.language>

    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>

    <jacoco.outputDir>${project.build.directory}</jacoco.outputDir>
    <!-- Jacoco output file for UTs -->
    <jacoco.out.ut.file>jacoco-ut.exec</jacoco.out.ut.file>
    <!-- Tells Sonar where the Jacoco coverage result file is located for Unit Tests -->
    <sonar.jacoco.reportPath>${jacoco.outputDir}/${jacoco.out.ut.file}</sonar.jacoco.reportPath>
    <!--sonar properties-->

</properties>

<profiles>
    <profile>
        <id>sonar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                    <configuration>
                        <argLine>${jacoco.agent.ut.arg}</argLine>
                        <properties>
                           <property>
                               <name>listener</name>
                            <value>org.sonar.java.jacoco.JUnitListener</value>
                            </property>
                        </properties>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco-maven-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>prepare-ut-agent</id>
                            <phase>process-test-classes</phase>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <destFile>${sonar.jacoco.reportPath}</destFile>
                                <propertyName>jacoco.agent.ut.arg</propertyName>
                                <append>true</append>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.sonarsource.java</groupId>
                <artifactId>sonar-jacoco-listeners</artifactId>
                <version>${sonar-jacoco-listeners.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

问题是当项目由 TeamCity 构建并将结果传递给 SonarQube 时,Measures -> Coverage Measures -> Tree 中的 Coverage 结果如图所示,例如所有模块名称都相同:项目名称+分支名称。但我希望他们有模块名称。

配置有问题吗? 谢谢!

还有,如果我用官方最简单的配置github根本就没有代码覆盖率分析

我认为这与代码覆盖率无关,您只是通过在父 pom.xml 中设置 属性 sonar.projectName 来强制 SonarQube 中的所有模块具有相同的名称。所以只需删除它或确保它为不同的模块指定不同的名称。