gpt4 book ai didi

dynamic - 如何在gradle插件中动态创建任务?

转载 作者:行者123 更新时间:2023-12-03 03:05:28 25 4
gpt4 key购买 nike

我正在开发一个 gradle 插件,但在尝试动态创建任务时遇到了问题。

这是我的代码片段:

apply plugin: BootstrapPlugin

bootstrapConfig {
basedir = "C:/temp/unzip"
componentList = [
[
zipName: "test1",
zipVersion: "_2",
zipNetworkLocation: "temp",
zipUnpackLocation: "${basedir}"
],
[
zipName: "test2",
zipVersion: "_1",
zipNetworkLocation: "temp",
zipUnpackLocation: "${basedir}"
]
]
}

class BootstrapPlugin implements Plugin<Project> {
void apply(Project project){
project.extensions.create("bootstrapConfig",BootstrapConfigPluginExtension)
project.task("unzipAll", dependsOn: project.tasks.matching { Task task -> task.name.startsWith("unzipSingle")}){
println "DOIT!"
}

project.bootstrapConfig.componentList.each { element ->
project.task "unzipSingle${element.zipName}" (type: Copy) {
def artifactFile = new File(project.bootstrapConfig.shareUrl + "/" + element.zipNetworkLocation + "/" + element.zipName + element.zipVersion + ".zip")
def pathDestination = element.zipUnpackLocation + "/" + element.zipName + element.zipVersion
def touchFileName = pathDestination + "/" + element.zipName + element.zipVersion + ".pid"
from project.zipTree(artifactFile)
into pathDestination
}
}
}
}

class BootstrapConfigPluginExtension {
String basedir
def componentList = []
}

当我尝试运行它时,问题是我尝试创建的任务都不存在。运行 unzipAll 它只运行该任务,因为它不依赖于其他任务。

我做错了什么?

最佳答案

事情是这样的:

  1. 所有脚本语句都按照声明的顺序进行解析。
  2. apply plugin: BootstrapPlugin 被执行。
  3. 在应用 BootstrapPluginbootstrapConfig 闭包尚未解析。由于它未被解析,BootstrapPlugin 对配置一无所知,并迭代空集合。
  4. bootstrapConfig 闭包在没用的时候被解析。

解决方案是在afterEvaluate block 中创建动态任务:

apply plugin: BootstrapPlugin

bootstrapConfig {
basedir = 'C:/temp/unzip'
shareUrl = 'http://lol.com'
componentList = [
[
zipName: "test1",
zipVersion: "_2",
zipNetworkLocation: "temp",
zipUnpackLocation: "${basedir}",
],
[
zipName: "test2",
zipVersion: "_1",
zipNetworkLocation: "temp",
zipUnpackLocation: "${basedir}",
],
]
}

class BootstrapPlugin implements Plugin<Project> {
void apply(Project project){
project.extensions.create('bootstrapConfig', BootstrapConfigPluginExtension)
project.afterEvaluate {
project.task("unzipAll", dependsOn: project.tasks.matching { Task task -> task.name.startsWith("unzipSingle")}){
println "DOIT!"
}

project.bootstrapConfig.componentList.each { element ->
project.task("unzipSingle${element.zipName}",type: Copy) {
def artifactFile = new File(project.bootstrapConfig.shareUrl + "/" + element.zipNetworkLocation + "/" + element.zipName + element.zipVersion + ".zip")
def pathDestination = element.zipUnpackLocation + "/" + element.zipName + element.zipVersion
def touchFileName = pathDestination + "/" + element.zipName + element.zipVersion + ".pid"
from project.zipTree(artifactFile)
into pathDestination
}
}
}
}
}

class BootstrapConfigPluginExtension {
String basedir
String shareUrl
def componentList = []
}

现在任务确实存在并且可以运行,但是它们不会与 gradle tasks 一起列出。

Here是演示。

关于dynamic - 如何在gradle插件中动态创建任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34152407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com