Pet*_*vic 27 http unzip download gradle
从url(http)下载和解压缩文件的正确方法是什么?
如果可能的话,我想防止每次运行任务时重新下载(ant.get可以通过实现skipexisting: 'true').
我目前的解决方案是:
task foo {
ant.get(src: 'http://.../file.zip', dest: 'somedir', skipexisting: 'true')
ant.unzip(src: 'somedir' + '/file.zip', dest: 'unpackdir')
}
Run Code Online (Sandbox Code Playgroud)
不过,我期待无蚂蚁解决方案.有机会实现这一目标吗?
RaG*_*aGe 53
@CMPS:
因此,假设您要将此zip文件作为依赖项下载:
https://github.com/jmeter-gradle-plugin/jmeter-gradle-plugin/archive/1.0.3.zip
Run Code Online (Sandbox Code Playgroud)
您将常春藤回购定义为:
repositories {
ivy {
url 'https://github.com/'
layout 'pattern', {
artifact '/[organisation]/[module]/archive/[revision].[ext]'
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后将其用作:
dependencies {
compile 'jmeter-gradle-plugin:jmeter-gradle-plugin:1.0.3@zip'
//This maps to the pattern: [organisation]:[module]:[revision]:[classifier]@[ext]
}
Run Code Online (Sandbox Code Playgroud)
借用@Matthias的解压缩任务,除了从gradle缓存中获取zip:
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
}
Run Code Online (Sandbox Code Playgroud)
目前没有可从URL下载的Gradle API.您可以使用Ant,Groovy实现此功能,或者,如果您确实希望从Gradle的依赖项解析/缓存功能中受益,可以假装它是带有自定义工件URL的Ivy存储库.解压缩可以通常的Gradle方式(copy方法或Copy任务)完成.
使用复制任务解压缩的方式如下:
task unzip(type: Copy) {
def zipFile = file('src/dists/dist.zip')
def outputDir = file("${buildDir}/unpacked/dist")
from zipTree(zipFile)
into outputDir
}
Run Code Online (Sandbox Code Playgroud)
http://mrhaki.blogspot.de/2012/06/gradle-goodness-unpacking-archive.html
这适用于 Gradle 5(使用 5.5.1 测试):
task download {
doLast {
def f = new File('file_path')
new URL('url').withInputStream{ i -> f.withOutputStream{ it << i }}
}
}
Run Code Online (Sandbox Code Playgroud)
调用gradle download将文件从 下载url到file_path。
如有必要,您可以使用其他答案中的其他方法来解压缩文件。
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19932 次 |
| 最近记录: |