Gradle: 在项目中找不到路径的任务

Gradle: Task with path not found in project

我有一个 gradle 项目,其结构如下:

rootDir
|--agent-v1.0.0
   |--agent.jar
|--proj1
   |-- // other project files
   |--build.gradle
|--proj2
   |-- // other project files
   |--build.gradle
|--build.gradle

我想运行 test.jvmArgs = ['javaagent:agent-v1.0.0/agent.jar']所有的子项目,所以我在根build.gradle写了下面的任务:

subprojects {
    task cs {
        outputs.upToDateWhen { false }
        dependsOn test.jvmArgs = ['javaagent:../agent-v1.0.0/agent.jar']
    }
}

但这失败了:

Could not determine the dependencies of task ':proj1'.

Task with path 'javaagent:../agent-v1.0.0/agent.jar' not found in project ':proj1'.

我已经尝试将 agent-v1.0.0 放在根目录和每个项目中,但仍然失败。我错过了什么?

为什么要在新的 task 中包含该逻辑?然后将 return 从 jvmArgs 传递给 dependsOn

只需正确配置测试任务:

subprojects {
    tasks.withType(Test) {
        jvmArgs "-javaagent:${project.rootDir}/agent-v1.0.0/agent.jar"
    }
}

一个任务可以依赖于另一个任务。所以 dependsOn 期望 task 作为参数。 test.jvmArgs = ['javaagent:../agent-v1.0.0/agent.jar'] 不是任务。

如果你想配置所有子项目的所有测试任务有额外的jvm args,那么语法是

subprojects {
    // this block of code runs for every subproject

    afterEvaluate {
        // this block of code runs after the subproject has been evaluated, and thus after 
        // the test task has been added by the subproject build script

        test {
            // this block of code is used to configure the test task of the subproject

            // this configures the jvmArgs property of the test task
            jvmArgs = ['javaagent:../agent-v1.0.0/agent.jar']
        }
    }
}

但不要复制粘贴此代码。阅读等级用户指南,了解其基本概念。