jacoco 項目包含單模塊和多模塊

本地調(diào)試執(zhí)行


mvn  -Dmaven.test.skip=false -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -U clean verify org.jacoco:jacoco-maven-plugin:0.8.8:report-aggregate sonar:sonar -Dsonar.projectKey=code-diff         -Dsonar.host.url=https://test-sonarqube.internal.com         -Dsonar.token=sqa_37c31****         -Dsonar.coverage.jacoco.xmlReportPaths=${pwd}/aggregate-report/target/site/jacoco/jacoco.xml #這里傳絕對路徑

1、多模塊需要創(chuàng)建一個新的子模塊,名字【aggregate-report】

a、新的子模塊添加需要掃描的模塊依賴【dependency】


image.png
<dependencies>
        <!-- 顯式依賴所有子模塊,確保聚合報告能收集到數(shù)據(jù) -->
        <dependency>
            <groupId>com.app.engine</groupId>
            <artifactId>app-engine-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.app.engine</groupId>
            <artifactId>app-engine-service</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.app.engine</groupId>
            <artifactId>app-engine-web</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

b、新的子模塊pom中添加jacoco的依賴

<build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.11</version>
                <executions>
                    <execution>
                        <id>jacoco-report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2、父pom中添加org.apache.maven和surefire依賴

image.png
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <testFailureIgnore>true</testFailureIgnore>
                    <argLine>${jacocoArgLine}</argLine> <!-- ?關(guān)鍵 -->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.11</version>
                <executions>
                    <execution>
                        <id>my-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jacocoArgLine</propertyName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>${sonar-maven-plugin.version}</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

        </plugins>
image.png
<properties>
        <sonar.coverage.jacoco.xmlReportPaths>
            aggregate-report/target/site/jacoco-aggregate/jacoco.xml
        </sonar.coverage.jacoco.xmlReportPaths>
</properties>
可執(zhí)行shell腳本,自動區(qū)分單模塊還是多模塊

!/bin/bash

set -e

========================

固定參數(shù)(可改成從外部傳入)

========================

SONAR_PROJECT_KEY="app-engine-test"
SONAR_HOST_URL="https://test-sonarqube.internal.com"
SONAR_TOKEN="squ_***"
SONAR_TIMEOUT=300

========================

獲取當(dāng)前項目根目錄絕對路徑

========================

PROJECT_ROOT=$(pwd)

========================

判斷是否是多模塊項目

========================

if grep -q "<modules>" pom.xml; then
echo "? 檢測到多模塊項目"
REPORT_GOAL="org.jacoco:jacoco-maven-plugin:0.8.8:report-aggregate"

多模塊使用絕對路徑

REPORT_PATH="${PROJECT_ROOT}/aggregate-report/target/site/jacoco-aggregate/jacoco.xml"
else
echo "? 檢測到單模塊項目"
REPORT_GOAL="org.jacoco:jacoco-maven-plugin:0.8.8:report"

單模塊使用相對路徑

REPORT_PATH="target/site/jacoco/jacoco.xml"
fi

========================

執(zhí)行 Maven + Jacoco + Sonar

========================

echo "?? 開始執(zhí)行: REPORT_GOAL" echo "?? 覆蓋率報告路徑:REPORT_PATH"

mvn -Dmaven.test.skip=false -B
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
-U clean verify {REPORT_GOAL} sonar:sonar \ -Dsonar.projectKey={SONAR_PROJECT_KEY}
-Dsonar.host.url={SONAR_HOST_URL} \ -Dsonar.token={SONAR_TOKEN}
-Dsonar.coverage.jacoco.xmlReportPaths={REPORT_PATH} \ -Dsonar.ws.timeout={SONAR_TIMEOUT}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容