2

我有一个使用 Tycho 通过 Maven 构建的 Eclipse RCP 应用程序。它有一些位于插件片段中的测试,因此每个带有操作代码的插件都有自己的片段,其中包含测试。测试是在使用 Tycho Surefire 的 maven 构建期间执行的。我想测量代码覆盖率,所以我包含了 Sonarqube 插件,然后使用 Jacoco 收集覆盖率数据。这就是问题所在:记录了代码覆盖率,但仅针对测试片段。带有被测代码的插件被报告为具有 0% 的代码覆盖率。我怀疑问题是 Jacoco 没有检测不包含测试的插件,但我找不到任何关于如何强制检测代码的文档。

这是具有相关 Sonarqube/Jacoco 配置的父 pom:

<?xml version="1.0" encoding="UTF-8"?>
    <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>my-project</groupId>
      <artifactId>my-project-parent</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <packaging>pom</packaging>
      <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
        <tycho-version>1.4.0</tycho-version>
        <tycho-eclipserun-version>${tycho-version}</tycho-eclipserun-version>
        <maven-surefire-plugin-version>2.22.2</maven-surefire-plugin-version>
        <maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
        <maven-clean-plugin-version>2.6.1</maven-clean-plugin-version>
        <maven-dependency-plugin-version>2.10</maven-dependency-plugin-version>
        <maven-resources-plugin-version>2.7</maven-resources-plugin-version>
        <m2e-lifecycle-mapping-plugin-version>1.0.0</m2e-lifecycle-mapping-plugin-version>
        <junit-version>4.12</junit-version>
        <maven-checkstyle-plugin-version>3.1.0</maven-checkstyle-plugin-version>
        <spotbugs-maven-plugin-version>3.1.12</spotbugs-maven-plugin-version>
        <jacoco-maven-plugin-version>0.8.4</jacoco-maven-plugin-version>
        <findbugs-maven-plugin-version>3.0.5</findbugs-maven-plugin-version> 
      </properties>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>${maven-deploy-plugin.version}</version>
          </plugin>
          <plugin>
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <version>${nexus-staging-maven-version}</version>
            <executions>
              <execution>
                <id>default-deploy</id>
                <phase>deploy</phase>
                <goals>
                  <goal>deploy</goal>
                </goals>
              </execution>
            </executions>
            <extensions>true</extensions>
            <configuration>
              <stagingProfileId>${stagingProfileId}</stagingProfileId>
              <serverId>${project.staging.repository.id}</serverId>
              <nexusUrl>${project.staging.repository.url}</nexusUrl>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-maven-plugin</artifactId>
            <version>${tycho-version}</version>
            <extensions>true</extensions>
          </plugin>
          <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>target-platform-configuration</artifactId>
            <version>${tycho-version}</version>
            <configuration>
              <resolver>p2</resolver>
              <environments>
                <environment>
                  <os>linux</os>
                  <ws>gtk</ws>
                  <arch>x86_64</arch>
                </environment>
                <environment>
                  <os>win32</os>
                  <ws>win32</ws>
                  <arch>x86_64</arch>
                </environment>
                <environment>
                  <os>macosx</os>
                  <ws>cocoa</ws>
                  <arch>x86_64</arch>
                </environment>
              </environments>
              <target>
                <artifact>
                  <groupId>${project.groupId}</groupId>
                  <artifactId>${project.artifactId}</artifactId>
                  <version>${project.version}</version>
                  <classifier>${root-path}/dev/org.my-project/targetPlatforms/my-project</classifier>
                </artifact>
              </target>
            </configuration>
          </plugin>
          <plugin>
              <groupId>org.jacoco</groupId>
              <artifactId>jacoco-maven-plugin</artifactId>
              <version>${jacoco-maven-plugin-version}</version>
            </plugin>
        </plugins>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>build-helper-maven-plugin</artifactId>
              <version>${build-helper-maven-plugin-version}</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-dependency-plugin</artifactId>
              <version>${maven-dependency-plugin-version}</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-clean-plugin</artifactId>
              <version>${maven-clean-plugin-version}</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-resources-plugin</artifactId>
              <version>${maven-resources-plugin-version}</version>
            </plugin>
            <plugin>
              <groupId>org.eclipse.tycho.extras</groupId>
              <artifactId>tycho-eclipserun-plugin</artifactId>
              <version>${tycho-eclipserun-version}</version>
            </plugin>
            <plugin>
              <groupId>org.eclipse.m2e</groupId>
              <artifactId>lifecycle-mapping</artifactId>
              <version>${m2e-lifecycle-mapping-plugin-version}</version>
            </plugin>
            
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-site-plugin</artifactId>
              <version>${maven-site-plugin-version}</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>${maven-project-info-reports-plugin-version}</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
              <version>${maven-checkstyle-plugin-version}</version>
              <configuration>
                <configLocation>${project.basedir}/${root-path}/dev/templates/checkstyle/Project Checkstyle Set v1.0.xml</configLocation>
                <propertyExpansion>samedir=${project.basedir}/${root-path}/dev/templates/checkstyle</propertyExpansion>
                <sourceDirectories>
                  <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
                  <sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
                </sourceDirectories>
                <omitIgnoredModules>true</omitIgnoredModules>
                <encoding>UTF-8</encoding>
                <consoleOutput>false</consoleOutput>
                <failsOnError>false</failsOnError>
                <linkXRef>false</linkXRef>
                <outputFileFormat>xml</outputFileFormat>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>findbugs-maven-plugin</artifactId>
              <version>${findbugs-maven-plugin-version}</version>
              <configuration>
                <xmlOutput>true</xmlOutput>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    
      <profiles> 
         <profile>
          <id>sonarqube-analysis</id>
          <properties>
            <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <sonar.java.checkstyle.reportPaths>${project.build.directory}/checkstyle-result.xml</sonar.java.checkstyle.reportPaths>
            <sonar.java.spotbugs.reportPaths>${project.build.directory}/spotbugsXml.xml</sonar.java.spotbugs.reportPaths>
            <sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
          </properties>
          <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <configuration>
                  <generatedSiteDirectory>false</generatedSiteDirectory>
                  <generateProjectInfo>false</generateProjectInfo>
                  <generateReports>true</generateReports>
                  <generateSitemap>false</generateSitemap>
                </configuration>
              </plugin>
              <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-surefire-plugin</artifactId>
                <configuration>
                  <argLine>${tycho.testArgLine}</argLine>
                </configuration>
              </plugin>
              <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <configuration>
                    <includes>my.project.base.package.*</includes>
                </configuration>
                <executions>
                  <execution>
                    <id>prepare-agent</id>
                    <goals>
                      <goal>prepare-agent</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
          <reporting>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <configuration></configuration>
                <reportSets>
                  <reportSet>
                    <reports></reports>
                  </reportSet>
                </reportSets>
              </plugin>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <reportSets>
                  <reportSet>
                    <id>checkstyle-report</id>
                    <reports>
                      <report>checkstyle</report>
                    </reports>
                  </reportSet>
                </reportSets>
              </plugin>
              <plugin>
                <groupId>com.github.spotbugs</groupId>
                <artifactId>spotbugs-maven-plugin</artifactId>
                <reportSets>
                  <reportSet>
                    <id>spotbugs-report</id>
                    <reports>
                      <report>spotbugs</report>
                    </reports>
                  </reportSet>
                </reportSets>
              </plugin>
              <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <reportSets>
                  <reportSet>
                    <id>jacoco-report</id>
                    <reports>
                      <report>report</report>
                    </reports>
                  </reportSet>
                </reportSets>
              </plugin>
            </plugins>
          </reporting>
        </profile>
      </profiles>
    
    </project>

我尝试在 jacoco 插件配置中添加用于检测的基本包...

<plugin>
   <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
         <includes>my.base.package.*</includes>
    </configuration>
    <executions>
         <execution>
             <id>prepare-agent</id>
                <goals>
                  <goal>prepare-agent</goal>
                </goals>
           </execution>
     </executions>
   </plugin>

...但 Sonarqube 仍然报告 0% 的覆盖率。正如我所说,似乎没有任何关于如何配置它的文档。有人知道如何进行这项工作吗?

4

0 回答 0