2013-06-30 46 views
0

我已将Checkstyle检查添加到我的Gradle实体中。 配置是:Gradle:自定义任务只执行一次

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 }但它不起作用。

非常感谢您的帮助。

回答

2

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

+0

谢谢。但如果其中一项任务失败呢?如何在任何情况下执行取决于失败任务的自定义任务? – StKiller

+0

现在你可以设置'checkstyle.ignoreFailures = true'。 Gradle 1.7(或1.8)将为此问题引入一个通用解决方案(*终结器任务*)。 –