Skip to content

Instantly share code, notes, and snippets.

@marchpig
Last active March 21, 2021 21:29
Show Gist options
  • Save marchpig/b75fdae13f9c31298285ca1b49e66a63 to your computer and use it in GitHub Desktop.
Save marchpig/b75fdae13f9c31298285ca1b49e66a63 to your computer and use it in GitHub Desktop.
How to display Jacoco summary in Maven console output
def reportFile = new File("target/site/jacoco/index.html")
if (!reportFile.exists() || !reportFile.canRead()) {
println "[JacocoPrinter] Skipped due to missing report file."
return
}
reportFile.withReader('UTF-8') { reader ->
def html = getParser().parseText(reader.readLine())
def totalRow = html.body.table.tfoot.tr
def instructionMissed = totalRow.td[1]
def instructionRatio = totalRow.td[2]
def branchMissed = totalRow.td[3]
def branchRatio = totalRow.td[4]
println "[JacocoPrinter] Instructions ${instructionRatio} (Missed ${instructionMissed})"
println "[JacocoPrinter] Branches ${branchRatio} (Missed ${branchMissed})"
}
XmlSlurper getParser() {
parser = new XmlSlurper()
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
return parser
}
...
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>JacocoPrinter.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
@linuxpizzacats
Copy link

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment