0

I have added checkstyle checks to my gradle bulid. The configuration is :

checkstyle {
    configFile = new File("${project.projectDir}/config/checkstyle/sun_checks.xml")
    showViolations = false
}

checkstyleMain {
    doLast{
        println("checkstyle main")
        project.ext.checkType = "main"
        tasks.checkstyleReport.execute()
    }
}


checkstyleTest {
    doLast{
        println("checkstyle test")
        project.ext.checkType = "test"
        tasks.checkstyleReport.execute()
    }
}

task checkstyleReport{
    checkstyleReport.outputs.upToDateWhen { false }
}

checkstyleReport << {
    logger.info("Producing checkstyle html report")
    final source = "${project.projectDir}/build/reports/checkstyle/${project.checkType}.xml"
    final xsl = "${project.projectDir}/config/checkstyle/checkstyle-simple.xsl"
    final output = "$buildDir/reports/checkstyle/${project.checkType}.html"
    println(source)
    println(xsl)
    println(output)
    ant.xslt(in: source,
             style: xsl,
             out: output
    )
}

When I invoke :

gradle --daemon clean checkstyleMain checktyleTest

The output is :

...
:clean
:compileJava
:processResources
:classes
:checkstyleMain
checkstyle main
/<root_path_here>/build/reports/checkstyle/main.xml
/<root_path_here>/config/checkstyle/checkstyle-simple.xsl
/<root_path_here>/build/reports/checkstyle/main.html
:compileTestJava
:processTestResources
:testClasses
:checkstyleTest
checkstyle test

As you see, the checkstyleReport task is invoked twice, but produces output only once. I even tried outputs.upToDateWhen { false } but it doesn't work.

Thank you in advance for help.

StKiller
  • 7,631
  • 10
  • 43
  • 56

1 Answers1

2

A Gradle task will execute at most once. Also, invoking tasks explicitly is not supported, and will lead to all sorts of problems. The correct approach is to declare a report task per Checkstyle task (e.g. using a task configuration rule), and make it depend on that Checkstyle task (or use mustRunAfter).

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Thank you. But what if one of the task fails ? How to execute in any case custom task that depends on failed tasks ? – StKiller Jun 30 '13 at 10:04
  • For now you can set `checkstyle.ignoreFailures = true`. Gradle 1.7 (or 1.8) will introduce a generic solution to this problem (*finalizer tasks*). – Peter Niederwieser Jun 30 '13 at 10:48