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

How to dynamically create a task in gradle plugin?

我正在开发 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块中创建动态任务:

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 是演示。