Jacoco和Tycho surefire的Eclipse RCP插件代码介绍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jacoco和Tycho surefire的Eclipse RCP插件代码介绍相关的知识,希望对你有一定的参考价值。

我有一个使用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>esa.open.configuration.*</includes>
    </configuration>
    <executions>
         <execution>
             <id>prepare-agent</id>
                <goals>
                  <goal>prepare-agent</goal>
                </goals>
           </execution>
     </executions>
   </plugin>

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

答案

本文中的解决方案可能会帮助您:http://www.lorenzobettini.it/2017/02/jacoco-code-coverage-and-report-of-multiple-eclipse-plug-in-projects/

以上是关于Jacoco和Tycho surefire的Eclipse RCP插件代码介绍的主要内容,如果未能解决你的问题,请参考以下文章

jacoco 插件不适用于 sunfire 插件.. org.apache.maven.surefire.booter.SurefireBooterForkException

不可解析的构建扩展:插件org.eclipse.tycho:tycho-maven

在集成 Maven、Tycho 和 Eclipse 时处理非 OSGi 依赖项

如何在非 Tycho 项目中使用 pomless tycho 工件

无法使用 Tycho 和 Java 9 构建 Eclipse RCP 项目

Tycho build 3: 创建一个全局构建项目