Apply condition on clover coverage report

Need to apply a condition in my Jenkins pipeline if the coverage report is less than 80 build will fail. I am using the Clover plugin to generate coverage reports.

stage(‘Code Coverage’) {
steps {
// Parse the code coverage report
def coverageReport = ./coverage/clover.xml// Path to your coverage report file
def xml = new XmlSlurper().parse(file: coverageReport)

            // Extract coverage metrics from the report
            def totalElements = xml.project.metrics.find { it.@type == 'METHOD' }.@elements.toInteger()
            def coveredElements = xml.project.metrics.find { it.@type == 'METHOD' }.@coveredelements.toInteger()

            // Calculate coverage percentage
            def coveragePercentage = (coveredElements * 100) / totalElements

            // Fail the build if coverage percentage is less than 80%
            if (coveragePercentage < 80) {
                error "Code coverage is less than 80%. Failing the build."


This would work out-of-the-box (without scripting) in the Coverage plugin. It would make sense if someone with XML and Java knowledge would write a corresponding parser.