Gradle动态生成任务

6l7fqoea  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(144)

这是我第一次尝试使用Clojure,我试图在build.gradle文件中生成一些任务,但遇到了复杂化错误,例如:

* Where:
  Build file '/Users/austin/Repositories/test/build.gradle' line: 47
* What went wrong:
  A problem occurred evaluating root project 'test'.
  Cannot set the value of read-only property 'src' for task ':downloadDriverfirefox' of type de.undercouch.gradle.tasks.download.Download.

我相当肯定这只是我对groovy语言缺乏经验,但我真的需要一些指针来指出我哪里出错了。

ext {
    // The drivers we want to use
    drivers = ["firefox", "chrome"]

    driversSrc = [:]
    driversSrc['firefox'] = 'https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-macos.tar.gz'
    driversSrc['chrome'] = 'https://chromedriver.storage.googleapis.com/2.29/chromedriver_mac64.zip'

    driverDest = new File("${buildDir}/webdrivers")
    }

    import org.gradle.api.tasks.*
    import de.undercouch.gradle.tasks.download.Download

    driversSrc.each { driver, path ->
    tasks.create(name: 'downloadDriver'+"${driver}", type: Download) {
        src = path
        dest ext.driverDest
        overwrite true
    }
    }

    driversSrc.each {driver ->
    tasks.create(name: 'unzipDriver'+"${driver}", dependsOn: 'downloadDriver'+"${driver}", type: Copy) {
        from zipTree(file('downloadDriver'+"${driver}").dest)
        into buildDir/'webdrivers'
    }
}
kyks70gy

kyks70gy1#

我做过的事

buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath "de.undercouch:gradle-download-task:3.2.0"
  }
}

allprojects {
  repositories {
    jcenter()
  }
}
import de.undercouch.gradle.tasks.download.Download

apply plugin: "de.undercouch.download"

ext {
  driversSrc = [
          "firefox": "https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-macos.tar.gz",
          "chrome" : "https://chromedriver.storage.googleapis.com/2.29/chromedriver_mac64.zip"
  ]
  driverDest = "${buildDir}/webdrivers"
}

driversSrc.each { driver, path ->
  def fileDest = new File(driverDest, new File(path).getName())

  tasks.create(name: "downloadDriver${driver}", type: Download) {
    src path
    dest fileDest
    overwrite true
  }

  tasks.create(name: "unzipDriver${driver}", dependsOn: "downloadDriver${driver}", type: Copy) {
    from path.endsWith("zip") ? zipTree(file(fileDest)) : tarTree(file(fileDest))
    into driverDest
  }
}

要运行任务:

./gradlew unzipDriverfirefox unzipDriverchrome*

相关问题