gradle-download-task: download files with progress

I really like how Gradle dis­plays pro­gress while it’s fetch­ing ar­ti­facts from Maven re­pos­it­or­ies, but I was al­ways won­der­ing why you can­not use the same func­tion­al­ity when you’re down­load­ing ar­bit­rary files in your build script. You can of course use An­t’s down­load task to fetch files, but it does not dis­play pro­gress in­form­a­tion. Since I could­n’t find a solu­tion on the In­ter­net I de­cided to write a plug­in.

gradle-down­load-task has ini­tially been re­leased in Septem­ber 2013 and is now avail­able in ver­sion 1.0. The plug­in con­trib­utes a down­load task that shows pro­gress in­form­a­tion just like Gradle does when it fetches ar­ti­facts from a re­pos­it­ory.

You can use it like this:

task downloadFile(type: Download) {
    src 'http://www.example.com/index.html'
    dest buildDir
}

The out­put will look like this:

Download http://www.example.com/index.html
> Building > :downloadFile > 320 KB/6.78 MB downloaded

In or­der to use the plug­in you have to add the fol­low­ing lines to your build file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'de.undercouch:gradle-download-task:1.0'
    }
}

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

You can also use the down­load ex­ten­sion to re­trieve a file any­where in your build script:

apply plugin: 'download-task'

task myTask << {
    //do something ...
    //... then download a file
    download {
        src 'http://www.example.com/index.html'
        dest buildDir
    }
    //... do something else
}

With ver­sion 1.0 you can now also se­quen­tially down­load a list of files to a dir­ect­ory. Please note that you have to spe­cify a dir­ect­ory as des­tin­a­tion if you down­load mul­tiple files. Oth­er­wise the plug­in will fail.

task downloadMultipleFiles(type: Download) {
    src([
        'http://www.example.com/index.html',
        'http://www.example.com/test.html'
    ])
    dest buildDir
}

Many thanks go to Bright Fulton who con­trib­uted sup­port for cus­tom HTTP re­quest head­ers. You can use this new fea­ture as fol­lows:

task downloadWithCustomHeader(type: Download) {
    src 'http://download.oracle.com/otn-pub/java/jdk/8-b132/jre-8-windows-x64.exe'
    dest buildDir
    header 'Cookie', 'oraclelicense=accept-securebackup-cookie'
}

The plug­in sup­ports a wide range of other op­tions. Please read the README file for more in­form­a­tion.

Happy down­load­ing!


Posted by Michel Krämer
on March, 22nd 2014.