gradle-download-task: download files with progress

I really like how Gradle displays progress while it’s fetching artifacts from Maven repositories, but I was always wondering why you cannot use the same functionality when you’re downloading arbitrary files in your build script. You can of course use Ant’s download task to fetch files, but it does not display progress information. Since I couldn’t find a solution on the Internet I decided to write a plugin.

gradle-download-task has initially been released in September 2013 and is now available in version 1.0. The plugin contributes a download task that shows progress information just like Gradle does when it fetches artifacts from a repository.

You can use it like this:

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

The output will look like this:

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

In order to use the plugin you have to add the following 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 download extension to retrieve a file anywhere 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 version 1.0 you can now also sequentially download a list of files to a directory. Please note that you have to specify a directory as destination if you download multiple files. Otherwise the plugin 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 contributed support for custom HTTP request headers. You can use this new feature as follows:

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 plugin supports a wide range of other options. Please read the README file for more information.

Happy downloading!