gradle - 在Gradle构建生命周期的后期配置任务

标签 gradle

我有一个多项目构建。构建中的某些项目会产生测试结果。一个项目产生一个安装程序,我希望该项目的 Artifact 包括所有其他项目的测试结果。我试图这样做(在产生安装程序的项目中):

// Empty test task, so that we can make the gathering of test results here depend on the tests' having
// been run in other projects in the build
task test << {
}

def dependentTestResultsDir = new File( buildDir, 'dependentTestResults' )

task gatherDependentTestResults( type: Zip, dependsOn: test ) {

    project.parent.subprojects.each { subproject ->

        // Find projects in this build which have a testResults configuration
        if( subproject.configurations.find { it.name == 'testResults' } ) {

            // Extract the test results (which are in a zip file) into a directory
            def tmpDir = new File( dependentTestResultsDir, subproject.name )
            subproject.copy {
                from zipTree( subproject.configurations['testResults'].artifacts.files.singleFile )
                into tmpDir
            }
        }
    }

    // Define the output of this task as the contents of that tree
    from dependentTestResultsDir
}

问题在于,在配置此任务时,其他项目中的测试任务尚未运行,因此它们的 Artifact 不存在,并且我在构建过程中收到如下消息:
The specified zip file ZIP 'C:\[path to project]\build\distributions\[artifact].zip' does not exist and will be silently ignored. This behaviour has been deprecated and is scheduled to be removed in Gradle 2.0

因此,我需要做的事情将延迟我的任务的配置,直到实际生成测试 Artifact 为止。实现此目的的惯用方式是什么?

我似乎需要就Gradle经常回答这种性质的问题。我认为我在概念上缺少一些东西。

最佳答案

在这种情况下,您应该可以通过添加<<使其成为一个 Action ,如下所示:

task gatherDependentTestResults( type: Zip, dependsOn: test ) << {
    // your task code here
}

关于gradle - 在Gradle构建生命周期的后期配置任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742540/

相关文章:

linux - lessc exec 任务未在 Gradle 中执行

gradle - 将 Volley(或其他库)合并到 Android Studio 项目中的最佳方法

java - 应用程序 :transformClassesWithDesugarForMockDebug using Android Studio 3 canary 1

gradle - Gradle:buildscript的解决策略

maven - 生成工具(ant,maven和gradle)中的显着项目?

gradle - 如何通过Maven Central上的导入路径查找软件包?

android - gradlew appengineEndpointsInstallClientLibs不会安装客户端库(但会生成它们)

java - IntelliJ Spring Gradle "Run all tests"不工作(单个测试运行良好)

android - Facebook android sdk 依赖冲突

java - 如何将动态输入参数传递给gradle java exec任务?