代码覆盖范围:Cobertura和SonarQube 5.3 Maven集成

时间:2016-06-27 13:23:50

标签: maven-3 code-coverage cobertura sonarqube5.3 maven-cobertura-plugin

我们正在将所有项目从ANT迁移到MAVEN,作为持续集成的一部分。我们正在使用JaCoCo进行新项目来衡量代码覆盖率并与SonarQube成功集成。

我们现在很少有使用Cobertura的ANT项目。在将其迁移到Maven时,我得到以下异常

异常

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.2:sonar (default-cli) on project CoberturaMaven: com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 start byte 0xac (at char #1, byte #-1)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 start byte 0xac (at char #1, byte #-1)
at org.sonarsource.scanner.maven.bootstrap.ExceptionHandling.handle(ExceptionHandling.java:36)
at org.sonarsource.scanner.maven.bootstrap.ScannerBootstrapper.execute(ScannerBootstrapper.java:81)
at org.sonarsource.scanner.maven.SonarQubeMojo.execute(SonarQubeMojo.java:112)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
... 19 more
Caused by: org.sonar.api.utils.XmlParserException: com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 start byte 0xac (at char #1, byte #-1)
at org.sonar.plugins.cobertura.CoberturaReportParser.parse(CoberturaReportParser.java:71)
at org.sonar.plugins.cobertura.CoberturaReportParser.parseReport(CoberturaReportParser.java:57)
at org.sonar.plugins.cobertura.CoberturaSensor.parseReport(CoberturaSensor.java:69)
at org.sonar.plugins.cobertura.CoberturaSensor.analyse(CoberturaSensor.java:64)
at org.sonar.batch.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:58)
at org.sonar.batch.phases.SensorsExecutor.execute(SensorsExecutor.java:50)
at org.sonar.batch.phases.PhaseExecutor.execute(PhaseExecutor.java:98)
at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:185)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:132)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:117)
at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:243)
at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:238)
at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:228)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:132)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:117)
at org.sonar.batch.task.ScanTask.execute(ScanTask.java:55)
at org.sonar.batch.task.TaskContainer.doAfterStart(TaskContainer.java:86)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:132)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:117)
at org.sonar.batch.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:122)
at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:119)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy15.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:240)
at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:151)
at org.sonarsource.scanner.maven.bootstrap.ScannerBootstrapper.execute(ScannerBootstrapper.java:78)
... 22 more
Caused by: com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 start byte 0xac (at char #1, byte #-1)
at com.ctc.wstx.sr.StreamScanner.constructFromIOE(StreamScanner.java:633)
at com.ctc.wstx.sr.StreamScanner.loadMore(StreamScanner.java:1007)
at com.ctc.wstx.sr.StreamScanner.getNext(StreamScanner.java:762)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2002)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
at org.codehaus.staxmate.in.SMHierarchicCursor.getNext(SMHierarchicCursor.java:71)
at org.codehaus.staxmate.in.SMInputCursor.advance(SMInputCursor.java:1631)
at org.sonar.plugins.cobertura.CoberturaReportParser$1.stream(CoberturaReportParser.java:65)
at org.sonar.api.utils.StaxParser.parse(StaxParser.java:113)
at org.sonar.api.utils.StaxParser.parse(StaxParser.java:93)
at org.sonar.api.utils.StaxParser.parse(StaxParser.java:83)
at org.sonar.plugins.cobertura.CoberturaReportParser.parse(CoberturaReportParser.java:69)
... 52 more
Caused by: java.io.CharConversionException: Invalid UTF-8 start byte 0xac (at char #1, byte #-1)
at com.ctc.wstx.io.UTF8Reader.reportInvalidInitial(UTF8Reader.java:303)
at com.ctc.wstx.io.UTF8Reader.read(UTF8Reader.java:189)
at com.ctc.wstx.io.ReaderSource.readInto(ReaderSource.java:86)
at com.ctc.wstx.io.BranchingReaderSource.readInto(BranchingReaderSource.java:56)
at com.ctc.wstx.sr.StreamScanner.loadMore(StreamScanner.java:1001)
... 62 more

的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>com.test</groupId>
<artifactId>CoberturaMaven</artifactId>
<version>1.0</version>
<name>CoberturaMaven</name>
<description>This is maven version of cobertura sonarqube integration</description>

<properties>
    <!-- JAVA Version -->
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>

    <!-- Cobertura SonarQube Setup -->
    <sonar.core.codeCoveragePlugin>cobertura</sonar.core.codeCoveragePlugin>
    <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>

    <!-- SONAR -->
    <sonar.login>changeIt</sonar.login>
    <sonar.password>changeIt</sonar.password>
    <sonar.projectKey>cobertura-maven</sonar.projectKey>
    <sonar.projectName>CoberturaMaven</sonar.projectName>
    <sonar.projectVersion>27Jun2016</sonar.projectVersion>
    <sonar.sources>src/main/java</sonar.sources>
    <sonar.java.binaries>target/classes</sonar.java.binaries>
    <sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <sonar.language>java</sonar.language>
</properties>

<dependencies>
    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

<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>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
        </plugin>
    </plugins>
</reporting>

</project>

专家,我在这里遗漏了什么吗?

2 个答案:

答案 0 :(得分:6)

我还没有得到任何人的回答。但是我按照

中的描述自己解决了这个问题

sonar importing cobertura.ser

问题是声纳-maven-plugin期待coverage.xml文件,但我指的是cobertura.ser。

我把它改成了

<sonar.cobertura.reportPath>${project.basedir}/target/site/cobertura/coverage.xml</sonar.cobertura.reportPath>

其次,我发现cobertura-maven-plugin(版本:2.7)存在一个问题,即使在报告格式中我指定了xml,它也没有生成coverage.xml文件。这个问题已经在下面的stackoverflow链接中指出了

Maven Cobertura plugin not generating coverage.xml

<plugin>
    <!-- use mvn cobertura:cobertura to generate cobertura reports -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <formats>
            <format>xml</format>
        </formats>
</plugin>

我必须传递 -Dcobertura.report.format = xml 此属性才能生成coverage.xml

最终的插件配置如下所示

<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>
                <encoding>UTF-8</encoding>
            </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>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </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>
            <configuration>
                <formats>
                    <format>xml</format>
                </formats>
            </configuration>
        </plugin>
    </plugins>
</reporting>

我使用以下命令执行代码覆盖并发布到SonarQube

清理安装cobertura:cobertura声纳:sonar -Dcobertura.report.format = xml

答案 1 :(得分:0)

您好,下面的代码可以生成xml和html文件

<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>
                    <encoding>Cp1252</encoding>
                </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>
                        </goals>
                        <configuration>
                            <encoding>Cp1252</encoding>
                        </configuration>
                    </execution>
                    <execution>
                        <id>cobertura</id>
                        <phase>test</phase>
                        <goals>
                            <goal>cobertura</goal>
                        </goals>
                        <configuration>
                            <formats>
                                <format>xml</format>
                                <format>html</format>
                            </formats>
                            <encoding>Cp1252</encoding>
                        </configuration>
                    </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>
            <!-- Normally, we take off the dependency report, saves time. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                </configuration>
            </plugin>
        </plugins>
    </reporting>