如何使用 gradle 从 artifactory 下载 jar 到项目目录?
How to download a jar from artifactory with gradle to project directory?
编写从 artifactory 下载 .jar(或任何其他)文件到项目目录(如果文件不存在)的 gradle 任务的正确方法是什么?
例如,我有一个路径为 /root/project
的项目,其中包含文件 /root/project/build.gradle
。如果 /root/project/generator.jar
不存在,我想从 artifactory (com/company/project/generator/1.0) 下载 generator.jar 到 /root/project
目录。
tasks.register("downloadGeneratorJar") {
group = 'Download'
description = 'Downloads generator.jar'
doLast {
def f = new File('generator.jar')
if (!f.exists()) {
URL artifactoryUrl = new URL('myurl')
HttpURLConnection myURLConnection = (HttpURLConnection)artifactoryUrl.openConnection()
String userCredentials = gradle.ext.developerUser + ":" + gradle.ext.developerPassword
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()))
myURLConnection.setRequestProperty ("Authorization", basicAuth)
InputStream is = myURLConnection.getInputStream()
new FileOutputStream(f).withCloseable { outputStream ->
int read
byte[] bytes = new byte[1024]
while ((read = is.read(bytes)) != -1) {
outputStream.write(bytes, 0, read)
}
}
}
}
}
编写从 artifactory 下载 .jar(或任何其他)文件到项目目录(如果文件不存在)的 gradle 任务的正确方法是什么?
例如,我有一个路径为 /root/project
的项目,其中包含文件 /root/project/build.gradle
。如果 /root/project/generator.jar
不存在,我想从 artifactory (com/company/project/generator/1.0) 下载 generator.jar 到 /root/project
目录。
tasks.register("downloadGeneratorJar") {
group = 'Download'
description = 'Downloads generator.jar'
doLast {
def f = new File('generator.jar')
if (!f.exists()) {
URL artifactoryUrl = new URL('myurl')
HttpURLConnection myURLConnection = (HttpURLConnection)artifactoryUrl.openConnection()
String userCredentials = gradle.ext.developerUser + ":" + gradle.ext.developerPassword
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()))
myURLConnection.setRequestProperty ("Authorization", basicAuth)
InputStream is = myURLConnection.getInputStream()
new FileOutputStream(f).withCloseable { outputStream ->
int read
byte[] bytes = new byte[1024]
while ((read = is.read(bytes)) != -1) {
outputStream.write(bytes, 0, read)
}
}
}
}
}