SonarQube:使用 Maven 上传 Cobertura 结果

SonarQube: uploading Cobertura results using Maven

我将 Maven 与 Cobertura 和 SonarQube 插件一起使用。我希望将 cobertura.ser 中的结果上传到 SonarQube。这是我的 pom.xml:

<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>delta.coverage.mvp</groupId>
  <artifactId>delta.coverage.mvp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <sonar.host.url>http://192.168.1.10:9000</sonar.host.url>
    <sonar.jdbc.driverClassName>org.h2.Driver</sonar.jdbc.driverClassName>
    <sonar.language>java</sonar.language>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.login>admin</sonar.login>
    <sonar.password>adminpwd</sonar.password>
    <sonar.projectKey>delta.coverage.mvp</sonar.projectKey>
    <sonar.projectName>delta.coverage.mvp</sonar.projectName>
    <sonar.projectVersion>test</sonar.projectVersion>
    <sonar.sources>src/main/java</sonar.sources>
    <sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin>
    <sonar.junit.reportsPath>${project.basedir}/target/surefire-reports</sonar.junit.reportsPath>
    <sonar.surefire.reportsPath>${project.basedir}/target/surefire-reports</sonar.surefire.reportsPath>
    <sonar.cobertura.reportPath>${project.basedir}/target/cobertura/cobertura.ser</sonar.cobertura.reportPath>
  </properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <instrumentation>
                    <includes>
                        <include>**/*.class</include>
                    </includes>
                </instrumentation>
            </configuration>
            <executions>
                <execution>
                    <id>clean</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
                <execution>
                    <id>instrument</id>
                    <phase>site</phase>
                    <goals>
                        <goal>instrument</goal>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- SonarQube Plugin -->
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.0.2</version>
        </plugin>

    </plugins>
</build>

<reporting>
    <plugins>
        <plugin>
            <!-- use mvn cobertura:cobertura to generate cobertura reports -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
        </plugin>
    </plugins>
</reporting>
</project>

我 运行 mvn clean cobertura:cobertura sonar:sonar 为了构建,单元测试(覆盖率)和分析代码+上传单元测试结果。分析(SonarQube 扫描)已成功上传。但是,Cobertura 结果不会上传到 SonarQube。相反,日志显示 Maven 实际上查找了 JaCoCo 文件:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building delta.coverage.mvp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ delta.coverage.mvp ---
[INFO] Deleting /home/phil/git/delta-coverage-mvp/target
[INFO] 
[INFO] >>> cobertura-maven-plugin:2.7:cobertura (default-cli) > [cobertura]test @ delta.coverage.mvp >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ delta.coverage.mvp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ delta.coverage.mvp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/phil/git/delta-coverage-mvp/target/classes
[INFO] 
[INFO] --- cobertura-maven-plugin:2.7:instrument (default-cli) @ delta.coverage.mvp ---
[INFO] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[INFO] Cobertura: Saved information on 1 classes.
[INFO] Cobertura: Saved information on 1 classes.

[INFO] Instrumentation was successful.
[INFO] NOT adding cobertura ser file to attached artifacts list.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ delta.coverage.mvp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ delta.coverage.mvp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/phil/git/delta-coverage-mvp/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ delta.coverage.mvp ---
[INFO] Surefire report directory: /home/phil/git/delta-coverage-mvp/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running delta.coverage.mvp.package1.Test1
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.202 sec
[INFO] Cobertura: Loaded information on 1 classes.
[INFO] Cobertura: Saved information on 1 classes.

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] <<< cobertura-maven-plugin:2.7:cobertura (default-cli) < [cobertura]test @ delta.coverage.mvp <<<
[INFO] 
[INFO] --- cobertura-maven-plugin:2.7:cobertura (default-cli) @ delta.coverage.mvp ---
[INFO] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[INFO] Cobertura: Loaded information on 1 classes.
Report time: 108ms

[INFO] Cobertura Report generation was successful.
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building delta.coverage.mvp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- sonar-maven-plugin:3.0.2:sonar (default-cli) @ delta.coverage.mvp ---
[INFO] User cache: /home/phil/.sonar/cache
[INFO] Load global repositories
[INFO] Load global repositories (done) | time=517ms
[INFO] User cache: /home/phil/.sonar/cache
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=116ms
[INFO] SonarQube version: 5.6.1
[INFO] Default locale: "en_US", source code encoding: "UTF-8"
[INFO] Process project properties
[INFO] Load project repositories
[INFO] Load project repositories (done) | time=193ms
[INFO] Load quality profiles
[INFO] Load quality profiles (done) | time=143ms
[INFO] Load active rules
[INFO] Load active rules (done) | time=1824ms
[WARNING] 'sonar.dynamicAnalysis' is deprecated since version 4.3 and should no longer be used.
[INFO] Publish mode
[INFO] -------------  Scan delta.coverage.mvp
[INFO] Language is forced to java
[INFO] Load server rules
[INFO] Load server rules (done) | time=966ms
[INFO] Base dir: /home/phil/git/delta-coverage-mvp
[INFO] Working dir: /home/phil/git/delta-coverage-mvp/target/sonar
[INFO] Source paths: src/main/java
[INFO] Test paths: src/test/java
[INFO] Binary dirs: target/classes
[INFO] Source encoding: UTF-8, default locale: en_US
[INFO] Index files
[INFO] 2 files indexed
[INFO] Quality profile for java: Cloud Managed Services - 201503
[INFO] JaCoCoSensor: JaCoCo report not found : /home/phil/git/delta-coverage-mvp/target/jacoco.exec
[INFO] JaCoCoItSensor: JaCoCo IT report not found: /home/phil/git/delta-coverage-mvp/target/jacoco-it.exec
[INFO] Sensor JavaSquidSensor
[INFO] Configured Java source version (sonar.java.source): none
[INFO] JavaClasspath initialization...
[INFO] JavaClasspath initialization done: 12 ms
[INFO] JavaTestClasspath initialization...
[INFO] JavaTestClasspath initialization done: 4 ms
[INFO] Java Main Files AST scan...
[INFO] 1 source files to be analyzed
[INFO] Java Main Files AST scan done: 193 ms
[INFO] 1/1 source files have been analyzed
[INFO] Java bytecode scan...
[INFO] Java bytecode scan done: 18 ms
[INFO] Java Test Files AST scan...
[INFO] 1 source files to be analyzed
[INFO] Java Test Files AST scan done: 56 ms
[INFO] 1/1 source files have been analyzed
[INFO] Package design analysis...
[INFO] Package design analysis done: 4 ms
[INFO] Sensor JavaSquidSensor (done) | time=560ms
[INFO] Sensor Lines Sensor
[INFO] Sensor Lines Sensor (done) | time=1ms
[INFO] Sensor SurefireSensor
[INFO] parsing /home/phil/git/delta-coverage-mvp/target/surefire-reports
[INFO] Sensor SurefireSensor (done) | time=54ms
[INFO] Sensor SCM Sensor
[INFO] SCM provider for this project is: git
[INFO] 2 files to be analyzed
[INFO] 0/2 files analyzed
[WARNING] Missing blame information for the following files:
[WARNING]   * /home/phil/git/delta-coverage-mvp/src/main/java/delta/coverage/mvp/package1/Class1.java
[WARNING]   * /home/phil/git/delta-coverage-mvp/src/test/java/delta/coverage/mvp/package1/Test1.java
[WARNING] This may lead to missing/broken features in SonarQube
[INFO] Sensor SCM Sensor (done) | time=112ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=5ms
[INFO] Sensor Code Colorizer Sensor
[INFO] Sensor Code Colorizer Sensor (done) | time=1ms
[INFO] Sensor CPD Block Indexer
[INFO] JavaCpdBlockIndexer is used for java
[INFO] Sensor CPD Block Indexer (done) | time=159ms
[INFO] Calculating CPD for 1 files
[INFO] CPD calculation finished
[INFO] Analysis report generated in 57ms, dir size=13 KB
[INFO] Analysis reports compressed in 14ms, zip size=8 KB
[INFO] Analysis report uploaded in 218ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://192.168.1.10:9000/dashboard/index/delta.coverage.mvp
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://192.168.1.10:9000/api/ce/task?id=AVbYSBOBTyY3I5vbl2iO
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.175 s
[INFO] Finished at: 2016-08-29T16:50:17-05:00
[INFO] Final Memory: 31M/666M
[INFO] ------------------------------------------------------------------------

问题是SQ服务器上的Cobertura插件没有安装。我安装了它,一切正常。

不幸的是,mvn 日志中没有任何内容显示调用了 Cobertura,我假设 SQ 服务器默认支持 Cobertura,因为它是 Java 的主要覆盖工具之一。非常违反直觉,但至少有一个简单的解决方案。