Gradle:自定义任务只执行一次

时间:2013-06-30 09:16:50

标签: gradle checkstyle

我已将检查样式检查添加到我的gradle bulid中。 配置是:

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
    )
}

当我调用时:

gradle --daemon clean checkstyleMain checktyleTest

输出结果为:

...
: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

如您所见,checkstyleReport任务被调用两次,但只生成一次输出。我甚至试过outputs.upToDateWhen { false },但它不起作用。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

Gradle任务最多只执行一次。此外,不支持显式调用任务,并将导致各种问题。正确的方法是根据Checkstyle任务声明报告任务(例如,使用任务配置规则),并使其依赖于Checkstyle任务(或使用mustRunAfter)。

相关问题