gradle - download and unzip file from url
Let's say you want to download this zip file as a dependency:
https://github.com/jmeter-gradle-plugin/jmeter-gradle-plugin/archive/1.0.3.zip
You define your ivy repo as:
repositories {
ivy {
url 'https://github.com/'
patternLayout {
artifact '/[organisation]/[module]/archive/[revision].[ext]'
}
// This is required in Gradle 6.0+ as metadata file (ivy.xml)
// is mandatory. Docs linked below this code section
metadataSources { artifact() }
}
}
reference for required metadata here
The dependency can then be used as:
dependencies {
compile 'jmeter-gradle-plugin:jmeter-gradle-plugin:1.0.3@zip'
//This maps to the pattern: [organisation]:[module]:[revision]:[classifier]@[ext]
}
To unzip:
task unzip(type: Copy) {
def zipPath = project.configurations.compile.find {it.name.startsWith("jmeter") }
println zipPath
def zipFile = file(zipPath)
def outputDir = file("${buildDir}/unpacked/dist")
from zipTree(zipFile)
into outputDir
}
optional:
If you have more than one repository in your project, it may also help (for build time and somewhat security) to restrict dependency search with relevant repositories.
Gradle 6.2+:
repositories {
mavenCentral()
def github = ivy {
url 'https://github.com/'
patternLayout {
artifact '/[organisation]/[module]/archive/[revision].[ext]'
}
metadataSources { artifact() }
}
exclusiveContent {
forRepositories(github)
filter { includeGroup("jmeter-gradle-plugin") }
}
}
Earlier gradle versions:
repositories {
mavenCentral {
content { excludeGroup("jmeter-gradle-plugin") }
}
ivy {
url 'https://github.com/'
patternLayout {
artifact '/[organisation]/[module]/archive/[revision].[ext]'
}
metadataSources { artifact() }
content { includeGroup("jmeter-gradle-plugin") }
}
}
Unzipping using the copy task works like this:
task unzip(type: Copy) {
def zipFile = file('src/dists/dist.zip')
def outputDir = file("${buildDir}/unpacked/dist")
from zipTree(zipFile)
into outputDir
}
http://mrhaki.blogspot.de/2012/06/gradle-goodness-unpacking-archive.html
plugins {
id 'de.undercouch.download' version '4.0.0'
}
/**
* The following two tasks download a ZIP file and extract its
* contents to the build directory
*/
task downloadZipFile(type: Download) {
src 'https://github.com/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
}
https://github.com/michel-kraemer/gradle-download-task
There isn't currently a Gradle API for downloading from a URL. You can implement this using Ant, Groovy, or, if you do want to benefit from Gradle's dependency resolution/caching features, by pretending it's an Ivy repository with a custom artifact URL. The unzipping can be done in the usual Gradle way (copy
method or Copy
task).