gradle - 从url下载并解压缩文件

时间:2014-04-11 21:40:54

标签: http download gradle unzip

从网址(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')
}

仍然,我期待无蚂蚁解决方案。有机会实现这个目标吗?

7 个答案:

答案 0 :(得分:47)

@CMPS:

因此,假设您要将此zip文件作为依赖项下载:

https://github.com/jmeter-gradle-plugin/jmeter-gradle-plugin/archive/1.0.3.zip

您将常春藤回购定义为:

repositories {
    ivy {
        url 'https://github.com/'
        layout 'pattern', {
            artifact '/[organisation]/[module]/archive/[revision].[ext]'
        }
    }
}

然后将其用作:

dependencies {
    compile 'jmeter-gradle-plugin:jmeter-gradle-plugin:1.0.3@zip'
    //This maps to the pattern: [organisation]:[module]:[revision]:[classifier]@[ext]         
}

借用@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
}

答案 1 :(得分:7)

目前没有可从网址下载的Gradle API。您可以使用Ant,Groovy实现此功能,或者,如果您确实希望从Gradle的依赖项解析/缓存功能中受益,可以假装它是带有自定义工件URL的Ivy存储库。解压缩可以通常的Gradle方式(copy方法或Copy任务)完成。

答案 2 :(得分:7)

使用复制任务解压缩的方式如下:

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

答案 3 :(得分:5)

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
}

答案 4 :(得分:1)

我得到了@RaGe的答案,但是由于常春藤布局方法已被贬值,因此我不得不对其进行调整,请参见 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)

所以要使其正常工作,我必须将其调整为tomcat keycloak适配器:

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
}

答案 5 :(得分:1)

这适用于Gradle 5(已通过5.5.1测试):

task download {
    doLast {
        def f = new File('file_path')
        new URL('url').withInputStream{ i -> f.withOutputStream{ it << i }}
    }
}

调用gradle download会将文件从url下载到file_path

如有必要,您可以使用其他答案中的其他方法来解压缩文件。

答案 6 :(得分:0)

“本机gradle”,只有下载部分(有关解压缩,请参见其他答案)

addArgumentResolvers