Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Dynamically Generate Tasks

Tags:

gradle

This is my first attempt using Clojure and I'm attempting to generate some tasks in my build.gradle file. I'm encountering complication errors such as:

* 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.

I'm fairly certain it's just my inexperience with the groovy language, but I could really use some pointers as to where I'm going wrong.

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'
    }
}
like image 777
Austin Avatar asked Oct 23 '25 15:10

Austin


1 Answers

Things I have done:

  • Cleaned up map to store data
  • Since you are using this plugin: https://github.com/michel-kraemer/gradle-download-task, I had to make sure the tasks were used correctly
  • Since you have a tar, you have to use tarTree as well as zipTree

Please try this:

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
  }
}

To run the tasks:

./gradlew unzipDriverfirefox unzipDriverchrome*
like image 52
Jared Burrows Avatar answered Oct 26 '25 07:10

Jared Burrows



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!