configurations {
codequality
}
dependencies {
codequality 'com.puppycrawl.tools:checkstyle:6.1'
}
task checkstyle(type: AndroidCheckstyleTask) {
ignoreFailures true
showViolations false
configFile file("$project.rootDir/tools/checkstyle.xml")
xslFile file("$project.rootDir/tools/checkstyle-noframes-sorted.xsl")
}
task checkstyleCmdLine(type: AndroidCheckstyleTask) {
ignoreFailures false
showViolations true
configFile file("$project.rootDir/tools/checkstyle.xml")
xslFile file("$project.rootDir/tools/checkstyle-noframes-sorted.xsl")
}
check.dependsOn(checkstyle)
///////////////////////////////////////////////
////////////// Groovy Task Class //////////////
///////////////////////////////////////////////
import org.gradle.api.internal.project.IsolatedAntBuilder
/**
* See parameters at http://checkstyle.sourceforge.net/anttask.html
*/
class AndroidCheckstyleTask extends DefaultTask {
@InputFile
@Optional
File configFile = new File("$project.rootDir/config/checkstyle/checkstyle.xml")
@InputFile
@Optional
File xslFile = new File("$project.rootDir/config/checkstyle/checkstyle-noframes-sorted.xsl")
@OutputFile
@Optional
File outputFile = new File("$project.buildDir/reports/checkstyle/checkstyle-${project.name}.xml")
def outputHtmlFile = outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html")
FileCollection checkstyleClasspath = project.configurations.codequality
Boolean ignoreFailures = false
Boolean showViolations = true
Project gradleProject = project
def AndroidCheckstyleTask() {
description = 'Runs checkstyle against Android sourcesets.'
group = 'Code Quality'
}
@TaskAction
def runCheckstyle() {
outputFile.parentFile.mkdirs()
def antBuilder = services.get(IsolatedAntBuilder)
antBuilder.withClasspath(checkstyleClasspath).execute {
ant.taskdef(name: 'checkstyle', classname: 'com.puppycrawl.tools.checkstyle.CheckStyleTask')
// see also, maxWarnings and failureProperty arguments
ant.checkstyle(config: configFile, failOnViolation: !ignoreFailures) {
gradleProject.allprojects.each { submodule ->
submodule.android.sourceSets.each { sourceSet ->
sourceSet.java.each { file ->
file.getSrcDirs().each {
if (it.exists()) {
fileset(dir: it) {
exclude (name: '**/com/google/android/apps/iosched/util/*.java')
}
}
}
}
}
}
if (showViolations) {
formatter(type: 'plain', useFile: false)
}
formatter(type: 'xml', toFile: outputFile)
}
if (outputFile.exists()) {
ant.xslt(in: outputFile,
style: xslFile,
out: outputHtmlFile
)
}
}
}
}