Links#
https://www.jacoco.org/jacoco/trunk/doc/maven.html
https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/scanners/sonarscanner-for-maven/
https://checkstyle.org/maven.html
https://spotbugs.github.io/spotbugs-maven-plugin/
https://maven.apache.org/surefire/maven-surefire-plugin/
1. Important Points#
quality gates:
tests pass
coverage is collected
static checks pass
scanner result is visible in CI
rule:
local build should stay fast
CI verify stage can run heavier checks
2. JaCoCo#
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
./mvnw clean verify
open target/site/jacoco/index.html
3. JaCoCo Multi Module#
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
report-aggregate:
aggregator module 生成跨模块 coverage report
4. SonarQube#
./mvnw -B clean verify sonar:sonar \
-Dsonar.host.url="$SONAR_HOST_URL" \
-Dsonar.token="$SONAR_TOKEN" \
-Dsonar.projectKey=order-api
<properties>
<sonar.coverage.jacoco.xmlReportPaths>
target/site/jacoco/jacoco.xml
</sonar.coverage.jacoco.xmlReportPaths>
</properties>
SonarQube:
code quality and security analysis platform
token must come from CI secret
5. Checkstyle#
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
6. SpotBugs / PMD#
SpotBugs:
bytecode-level bug pattern scanner
PMD:
source-level rules and code smell scanner
./mvnw spotbugs:check
./mvnw pmd:check
7. CI Artifact#
publish:
target/surefire-reports
target/failsafe-reports
target/site/jacoco
SonarQube analysis link