问题
What would be the proper gradle way of downloading and unzipping the file from url (http
)?
If possible, I'd like to prevent re-downloading each time I run the task (in ant.get
can be achieved by skipexisting: 'true'
).
My current solution would be:
task foo {
ant.get(src: 'http://.../file.zip', dest: 'somedir', skipexisting: 'true')
ant.unzip(src: 'somedir' + '/file.zip', dest: 'unpackdir')
}
still, I'd expect ant-free solution. Any chance to achieve that?
回答1:
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).
回答2:
@CMPS:
So lets 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/'
layout 'pattern', {
artifact '/[organisation]/[module]/archive/[revision].[ext]'
}
}
}
and then use it as:
dependencies {
compile 'jmeter-gradle-plugin:jmeter-gradle-plugin:1.0.3@zip'
//This maps to the pattern: [organisation]:[module]:[revision]:[classifier]@[ext]
}
Borrowing @Matthias' unzip task, except picking up the zip from gradle cache:
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
}
回答3:
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
回答4:
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
}
回答5:
I got @RaGe's answer working, but I had to adapt it since the ivy layout method has been depreciated see https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.repositories.IvyArtifactRepository.html#org.gradle.api.artifacts.repositories.IvyArtifactRepository:layout(java.lang.String,%20groovy.lang.Closure)
So to get it working I had to adjust it to this for a tomcat keycloak adapter:
ivy {
url 'https://downloads.jboss.org/'
patternLayout {
artifact '/[organization]/[revision]/adapters/keycloak-oidc/[module]-[revision].[ext]'
}
}
dependencies {
// https://downloads.jboss.org/keycloak/4.8.3.Final/adapters/keycloak-oidc/keycloak-tomcat8-adapter-dist-4.8.3.Final.zip
compile "keycloak:keycloak-tomcat8-adapter-dist:$project.ext.keycloakAdapterVersion@zip"
}
task unzipKeycloak(type: Copy) {
def zipPath = project.configurations.compile.find {it.name.startsWith("keycloak") }
println zipPath
def zipFile = file(zipPath)
def outputDir = file("${buildDir}/tomcat/lib")
from zipTree(zipFile)
into outputDir
}
回答6:
This works with Gradle 5 (tested with 5.5.1):
task download {
doLast {
def f = new File('file_path')
new URL('url').withInputStream{ i -> f.withOutputStream{ it << i }}
}
}
Calling gradle download
downloads the file from url
to file_path
.
You can use the other methods from other answers to unzip the file if necessary.
来源:https://stackoverflow.com/questions/23023069/gradle-download-and-unzip-file-from-url