Fail build on pmd violation threshold breach

I am currently using the below configuration to ignore pmd violations.

pmdMain {
ignoreFailures = true

Is there a way to define maxAllowedViolations . So that build fails only if the violation crosses this limit.
Any pointers in this direction will be greatly appreciated.

Achieved this by creating the following custom task

task checkPMDReport  {
    doLast {
        def pmdReport = rootProject.file('reports/pmd/main.xml')
        def rootNode = new XmlSlurper().parse("$pmdReport")
        def bugsFound = rootNode.children().children().findAll({node -> == 'violation'}).size()
        if (bugsFound > <BUG_THRESHOLD>) {
            throw new GradleException("$bugsFound PMD rule violations were found. See the report at: $pmdReport")