1️⃣. Add this script to the root your the project directory
#!/bin/bash
REPORT="target/site/jacoco/jacoco.xml"
README="README.md"
TMPFILE=".coverage.tmp"
if [ ! -f "$REPORT" ]; then
echo "❌ JaCoCo XML report not found at $REPORT"
exit 1
fi
# Step 1: Generate new coverage report content
{
echo "## 📊 Code Coverage Report"
echo ""
echo "| Metric | Covered | Missed | Total | Coverage |"
echo "|-------------|---------|--------|--------|----------|"
METRICS=("INSTRUCTION" "LINE" "BRANCH" "METHOD" "CLASS" "COMPLEXITY")
for METRIC in "${METRICS[@]}"; do
COVERED=$(xmllint --xpath "string(//report/counter[@type='$METRIC']/@covered)" "$REPORT")
MISSED=$(xmllint --xpath "string(//report/counter[@type='$METRIC']/@missed)" "$REPORT")
TOTAL=$((COVERED + MISSED))
if [ "$TOTAL" -eq 0 ]; then
PERCENT=0
else
PERCENT=$(awk "BEGIN {printf \"%.2f\", ($COVERED / $TOTAL) * 100}")
fi
if (( $(echo "$PERCENT < 100.0" | bc -l) )); then
DISPLAY_PERCENT="${PERCENT}% ⚠️"
else
DISPLAY_PERCENT="${PERCENT}% ✅"
fi
echo "| $METRIC | $COVERED | $MISSED | $TOTAL | $DISPLAY_PERCENT |"
done
} > "$TMPFILE"
# Step 2: Remove any old coverage section and replace with the new one
# If markers exist, clean them out
if grep -q "<!-- coverage start -->" "$README"; then
sed -i.bak '/<!-- coverage start -->/,/<!-- coverage end -->/d' "$README"
fi
# Step 3: Append the new section at the end of the README
{
echo ""
echo "<!-- coverage start -->"
cat "$TMPFILE"
echo "<!-- coverage end -->"
} >> "$README"
rm "$TMPFILE"
echo "✅ README.md updated with full coverage report (overwriting any existing section)."
2️⃣. Update Maven pom.xml for the project
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>update-readme</id>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>${project.basedir}/update-coverage.sh</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>