在自定义插件中有 gradle.projectsEvaluated

having gradle.projectsEvaluated in custom plugin

如何将以下代码片段移动到我的自定义 gradle 插件?

gradle.projectsEvaluated {
    assemble.dependsOn(myCustomPluginTask)
}

我在自定义 gradle 插件中尝试了以下操作,但得到了 "No such property: assemble for class: CustomGradlePlugin"。

void apply(Project project) {
    project.apply(plugin: 'java')

    project.gradle.projectsEvaluated {
        assemble.dependsOn(myCustomPluginTask)
    }

    ...
}

需要projectsEvaluated 内完成的原因吗?

开始时 Gradle Life Cycle 可能有点混乱。我建议您在尝试自己的插件之前仔细阅读并理解它。

您可以忽略 projectsEvaluated 部分(因为您在插件中而不是 build.gradle),只需执行以下操作:

void apply(Project project) {
    project.apply(plugin: 'java')

    project.task('myCustomPluginTask') {
        print "This is called in the Configuration Phase"
        doLast {
            print "This is called during the Execution phase"
        }
    }

    project.tasks.assemble.dependsOn project.tasks.myCustomPluginTask

}