Gradle with GMetrics

by Matt Cholick

I'm a fan of GMetrics as a quick check to highlight problem classes and methods in a project. It didn't want to nicely with a Gradle build though. Here's a minimal build.gradile file with working GMetrics. Hopefully it saves someone out there a bit of time.

//Minimal configuration needed for Gradle build file with GMetrics integration
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

configurations {
    gmetrics
}

dependencies {
    gmetrics 'org.gmetrics:GMetrics:0.5'
}

task gmetrics << {
    //use GMetrics ant task - http://gmetrics.sourceforge.net/gmetrics-ant-task.html
    ant.taskdef(name: 'gmetrics', classname: 'org.gmetrics.ant.GMetricsTask', classpath: configurations.gmetrics.asPath)
    //ensure reporting directory created
    ant.mkdir(dir: "${project.reporting.baseDir.path}/gmetrics")
    ant.gmetrics() {
        report(type: 'org.gmetrics.report.BasicHtmlReportWriter') {
            option(name: 'outputFile', value: "${project.reporting.baseDir.path}/gmetrics/gmetrics.html")
        }
        report(type: 'org.gmetrics.report.XmlReportWriter') {
            option(name: 'outputFile', value: "${project.reporting.baseDir.path}/gmetrics/gmetrics.xml")
        }
        fileset(dir: 'src') {
            include(name: '**/*.groovy')
            //exclusions to filter out any classes with inner classes - sonar doesn't support
            exclude(name: '**/*Config*')
        }
    }
}