gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:05:01 28 4
gpt4 key购买 nike

从 url (http) 下载和解压缩文件的正确 gradle 方法是什么?

如果可能的话,我想防止每次运行任务时都重新下载(在ant.get中可以通过skipexisting: 'true'实现) .

我目前的解决方案是:

task foo {
ant.get(src: 'http://.../file.zip', dest: 'somedir', skipexisting: 'true')
ant.unzip(src: 'somedir' + '/file.zip', dest: 'unpackdir')
}

不过,我还是期待无 Ant 的解决方案。是否有机会实现这一目标?

最佳答案

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

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

您将 ivy 存储库定义为:

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() }
}
}

所需元数据引用 here

然后可以将依赖项用作:

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

解压:

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

}

可选:

如果您的项目中有多个存储库,它也可能有助于(为了构建时间和某种程度上的安全性)限制相关存储库的依赖项搜索。

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") }
}
}

早期的 gradle 版本:

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") }
}
}

关于http - gradle - 从 url 下载并解压文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23023069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com