4

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

我们现有的 ANT 项目很少使用 Cobertura。在将其迁移到 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>

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

4

2 回答 2

6

我还没有得到任何人的任何答复。但我自己解决了这个问题,如

声纳导入 cobertura.ser

问题是 sonar-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 插件不生成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 声纳:声纳 -Dcobertura.report.format=xml

于 2016-06-29T08:54:36.380 回答
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>
于 2020-07-09T06:14:29.233 回答