This is a download task for Gradle. It displays progress information just as Gradle does when it retrieves an artifact from a repository.
The plugin has been successfully tested with Gradle 2.0 up to 6.4. It should work with newer versions as well.
plugins {
id "de.undercouch.download" version "4.0.4"
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'de.undercouch:gradle-download-task:4.0.4'
}
}
apply plugin: 'de.undercouch.download'
After you have applied the plugin configuration (see above), you can use the
Download
task as follows:
task downloadFile(type: Download) {
src 'http://www.example.com/index.html'
dest buildDir
}
By default, the plugin always performs a download even if the destination file
already exists. If you want to prevent files from being downloaded again, use
the overwrite
flag (see description below).
task downloadFile(type: Download) {
src 'http://www.example.com/index.html'
dest buildDir
overwrite false
}
As an alternative to the Download
task, you may also use the download
extension to retrieve a file anywhere in your build script:
task myTask {
doLast {
//do something ...
//... then download a file
download {
src 'http://www.example.com/index.html'
dest buildDir
overwrite false
}
//... do something else
}
}
The plugin requires:
If you need to run the plugin with Gradle 1.x or Java 6, use gradle-download-task version 3.4.3.
task downloadFile(type: Download) {
src 'http://www.example.com/index.html'
dest buildDir
onlyIfModified true
}
Note that this feature depends on the server and whether it supports the
If-Modified-Since
request header and provides a Last-Modified
timestamp in its response.
task downloadMultipleFiles(type: Download) {
src([
'http://www.example.com/index.html',
'http://www.example.com/test.html'
])
dest buildDir
}
Please note that you have to specify a directory as destination if you download multiple files. Otherwise, the plugin will fail.
If you want to download all files from a directory and the server provides a simple directory listing, you can use the following code:
task downloadDirectory {
doLast {
def dir = 'http://central.maven.org/maven2/de/undercouch/gradle-download-task/1.0/'
def urlLister = new org.apache.ivy.util.url.ApacheURLLister()
def files = urlLister.listFiles(new URL(dir))
download {
src files
dest buildDir
}
}
}
To download and unpack a ZIP file, you can combine the download task plugin with Gradle's built-in support for ZIP files:
task downloadZipFile(type: Download) {
src 'https://github.com/michel-kraemer/gradle-download-task/archive/1.0.zip'
dest new File(buildDir, '1.0.zip')
}
task downloadAndUnzipFile(dependsOn: downloadZipFile, type: Copy) {
from zipTree(downloadZipFile.dest)
into buildDir
}
Please have a look at the examples
directory for more code samples. You can
also read my blog post about
common recipes for gradle-download-task.
The download task and the extension support the following properties.
true
if progress information should not be displayed
(default: false
)true
if existing files should be overwritten (default:
true
)true
if the file should only be downloaded if it
has been modified on the server since the last download (default:
false
)Tip! You may provide Groovy Closures to the src
and dest
properties to calculate their value at runtime.
true
if HTTPS certificate verification errors should be ignored
and any certificate (even an invalid one) should be accepted.
(default: false
)true
if compression should be used during download (default:
true
)0
(zero) means infinite timeout. A negative value
is interpreted as undefined. (default: 30 seconds
)0
(zero) means infinite timeout. A negative value
is interpreted as undefined. (default: 30 seconds
)0
,
failed requests are retried regardless of the actual error. This includes
failed connection attempts and file-not-found errors (404). A negative value
means infinite retries. (default: 0
)Basic
or Digest
authentication
(optional)Basic
or Digest
authentication
(optional)Basic
and
Digest
). If username
and password
are
set, the default value of this property will be Basic
. Otherwise,
this property has no default value. (optional)${buildDir}/download-task
)true
if the file should be downloaded to a temporary location
and, upon successful execution, moved to the final location. If
overwrite
is set to false
, this flag is useful to
avoid partially downloaded files if Gradle is forcefully closed or the system
crashes. Note that the plugin always deletes partial downloads on connection
errors, regardless of the value of this flag. The default temporary location
can be configured with the downloadTaskDir
property. (default:
false
)onlyIfModified
. If both
flags are true
, the plugin will check a file's timestamp as well
as its entity tag (ETag) and only download it if it has been modified on the
server since the last download. The plugin can differentiate between
strong and weak
ETags. Possible values are:
false
(default)true
"all"
"strongOnly"
${downloadTaskDir}/etags.json
)MD5
)