Gradle 来自插件的任务不会在 "Build" 上 运行,但会在 "Clean" 上 运行
Gradle Task from Plugin Doesn't Run on "Build", But Does Run on "Clean"
我们有一个 Android 项目,在我们构建 APK 之前,它需要 Gradle 插件任务到 运行。 (插件是我们自己写的)
我们希望在每次构建之前自动运行任务。
如果我们使用已弃用的 task.execute()
那么我们会收到一条警告,从 5.0 或类似版本开始它将不可用。
如果我们按照推荐使用dependsOn
,那么testTask1
不是在BUILD之前,而是在CLEAN之后。 (全部在下面的评论中解释)
我已经看过 gradle 文档和许多其他 SO 线程,但我还没有找到解决方案。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
flatDir { dirs 'libs' }
jcenter()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:3.1.3"
// our platform-tools plugin, in charge of some gradle tasks
classpath 'sofakingforevre:test-plugin:1.0-SNAPSHOT'
}
}
apply plugin: 'test-plugin'
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// OPTION 1 - USING EXECUTE()
// this task works as expected when calling "clean", but also when calling "assemble".
// the problem here is that the "execute" method has been deprecated, and we want to prepare for Gradle 5.0
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is called :)
// DEPRECATION WARNING :(
task buildPlatformExecute {
println("executing")
// this task is created by the plugin
tasks.getByName("testTask1").execute()
}
clean.dependsOn buildPlatformExecute
// OPTION 2 - USING DEPENDSON()
// this tasks works as expected when calling "clean", but DOES NOT WORK when calling "assemble".
// If we call we call assemble, the "executing" text does print, but "testTask1" would not run.
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is NOT CALLED :(
task buildPlatformDependency {
println("executing")
// this task is created by the plugin
dependsOn 'testTask1'
}
clean.dependsOn buildPlatformDependency
您的选项 1 解决方案存在问题
- 您正在使用已弃用的
task.execute()
API(您已经知道)
- 你混合了配置和执行阶段(一个常见的Gradle错误...):
由于您没有将 tasks.getByName("testTask1").execute()
包装在 doFirst {}
块的 doLast {}
中 buildPlatformExecute
任务中:testTask1
任务将始终执行,无论如何您将调用的任务。您甚至不需要在 clean
任务和您的自定义任务之间创建依赖关系(例如:尝试使用 ./gradlew help
执行简单的 "help" 任务,您会看到 testTask1
也被执行: 这肯定不是你想要的)
这里有更多信息:https://docs.gradle.org/current/userguide/build_lifecycle.html
您的 OPTION2 解决方案存在问题
您在 clean
任务和 buildPlatformDependency
任务之间创建了依赖关系:
- 执行
clean
个任务时,testTask1
个任务会按预期执行,但是
build
(或assemble
)任务和clean
任务之间没有依赖关系:这就是为什么当你执行build
任务时,clean
任务未执行(因此 testTask1
不会被触发)
解决方案
最好的方法是使用 Task.dependsOn
API 将自定义任务 testTask1
挂接到项目构建生命周期的正确位置。 "correct" 位置取决于您的任务在构建过程中负责什么:例如,如果您的任务需要在 assemble
任务之前执行,只需创建依赖项 assemble.dependsOn testTask1
我们有一个 Android 项目,在我们构建 APK 之前,它需要 Gradle 插件任务到 运行。 (插件是我们自己写的)
我们希望在每次构建之前自动运行任务。
如果我们使用已弃用的 task.execute()
那么我们会收到一条警告,从 5.0 或类似版本开始它将不可用。
如果我们按照推荐使用dependsOn
,那么testTask1
不是在BUILD之前,而是在CLEAN之后。 (全部在下面的评论中解释)
我已经看过 gradle 文档和许多其他 SO 线程,但我还没有找到解决方案。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
flatDir { dirs 'libs' }
jcenter()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:3.1.3"
// our platform-tools plugin, in charge of some gradle tasks
classpath 'sofakingforevre:test-plugin:1.0-SNAPSHOT'
}
}
apply plugin: 'test-plugin'
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// OPTION 1 - USING EXECUTE()
// this task works as expected when calling "clean", but also when calling "assemble".
// the problem here is that the "execute" method has been deprecated, and we want to prepare for Gradle 5.0
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is called :)
// DEPRECATION WARNING :(
task buildPlatformExecute {
println("executing")
// this task is created by the plugin
tasks.getByName("testTask1").execute()
}
clean.dependsOn buildPlatformExecute
// OPTION 2 - USING DEPENDSON()
// this tasks works as expected when calling "clean", but DOES NOT WORK when calling "assemble".
// If we call we call assemble, the "executing" text does print, but "testTask1" would not run.
// CLEAN - testTask1 is called :)
// BUILD - testTask1 is NOT CALLED :(
task buildPlatformDependency {
println("executing")
// this task is created by the plugin
dependsOn 'testTask1'
}
clean.dependsOn buildPlatformDependency
您的选项 1 解决方案存在问题
- 您正在使用已弃用的
task.execute()
API(您已经知道) - 你混合了配置和执行阶段(一个常见的Gradle错误...):
由于您没有将 tasks.getByName("testTask1").execute()
包装在 doFirst {}
块的 doLast {}
中 buildPlatformExecute
任务中:testTask1
任务将始终执行,无论如何您将调用的任务。您甚至不需要在 clean
任务和您的自定义任务之间创建依赖关系(例如:尝试使用 ./gradlew help
执行简单的 "help" 任务,您会看到 testTask1
也被执行: 这肯定不是你想要的)
这里有更多信息:https://docs.gradle.org/current/userguide/build_lifecycle.html
您的 OPTION2 解决方案存在问题
您在 clean
任务和 buildPlatformDependency
任务之间创建了依赖关系:
- 执行
clean
个任务时,testTask1
个任务会按预期执行,但是 build
(或assemble
)任务和clean
任务之间没有依赖关系:这就是为什么当你执行build
任务时,clean
任务未执行(因此testTask1
不会被触发)
解决方案
最好的方法是使用 Task.dependsOn
API 将自定义任务 testTask1
挂接到项目构建生命周期的正确位置。 "correct" 位置取决于您的任务在构建过程中负责什么:例如,如果您的任务需要在 assemble
任务之前执行,只需创建依赖项 assemble.dependsOn testTask1