warch
warch

Reputation: 2609

gradle + kotlin-dsl: download file

How can I download a file within a gradle task with kotlin-dsl?

I want to download a file from an external source (URL) and store + rename it inside my projectfolder.

I tried to find solutions online I only found solutions written in groovy.

Thanks in advance!

Upvotes: 6

Views: 3878

Answers (2)

Saurabh Padwekar
Saurabh Padwekar

Reputation: 4074

Simple with no external dependency.

buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath("com.android.tools.build:gradle:4.0.1")
    }
}

tasks.register("downloadPdf"){
    val path = "myfile.pdf"
    val sourceUrl = "https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf"
    download(sourceUrl,path)
}

fun download(url : String, path : String){
    val destFile = File(path)
    ant.invokeMethod("get", mapOf("src" to url, "dest" to destFile))
}

Upvotes: 8

warch
warch

Reputation: 2609

I found a solution using https://github.com/michel-kraemer/gradle-download-task:

import de.undercouch.gradle.tasks.download.Download

...

plugins {
  ....
  id("de.undercouch.download").version("3.4.3")
}

...

task<DefaultTask>("my-download-task") {
  ...
  val url = " ... "
  val dest = File("...")
  task<Download>("download-task") {
    src(url)
    dest(dest)
  }
  dependsOn("download-task")
}

Upvotes: 9

Related Questions