【Java】 Maven多模塊項目上傳到Sonar掃描問題合集

上傳到Soanr時,項目有單元測試數,可是覆蓋率爲0java

修改pom.xml

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.4.2</version>
                    <configuration>
                        <skipTests>false</skipTests>
                        <testFailureIgnore>true</testFailureIgnore>
                        <includes>
                            <include>**/*Test.java</include>
                        </includes>
                        <excludes>
                            <!--<exclude>**/Abstract*.java</exclude>-->
                            <!--<exclude>**/*Service.java</exclude>-->
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.9</version>
                    <executions>
                        <execution>
                            <id>pre-test</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

必要時帶上參數

clean cobertura:cobertura -Dcobertura.report.format=xml package -Dmaven.test.failure.ignore=true sonar:sonar -Dsonar.language=java

多模塊項目經過Jenkins構建掃描上傳到Sonar時,代碼覆蓋率很低,只覆蓋了單模塊的代碼,這時能夠經過配置JaCoCo解決該問題。
參考資料apache

修改pom.xml

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

經過clean org.jacoco:jacoco-maven-plugin:prepare-agent jacoco:report-aggregate install 可在target下生成jacoco-aggregate覆蓋相關報告bash

注:jacoco版本低於0.7.9 jacoco:report-aggregate參數可能不存在
多模塊項目整合覆蓋率還能夠指定report目錄app

<properties>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
 </properties>        

 <build>
         <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.9</version>
                    <configuration>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <append>true</append>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
 </build>
相關文章
相關標籤/搜索