要通过 gradle 上传 jar,您必须将该 jar 声明为发布工件,并使用 artifacts 闭包将其添加到特定配置中:
apply plugin:'maven'
configurations{
allJars
}
artifacts{
allJars file("path/to/jarFile.jar")
}
现在您可以配置动态创建的 uploadAllJars 任务:
uploadAllJars {
repositories {
mavenDeployer {
repository(url: 'http://localhost:8081/artifactory/acme') {
authentication(userName: 'admin', password: 'password');
}
}
}
问题是您要上传多个工件。为了实现这一点,您需要在构建脚本中增加一些动态。可以将所有发现的 jar 的发布工件的动态创建包装在一个任务中。在我的示例中,discoverAllJars 任务只是在指定文件夹中查找 jar 文件。在这里,您需要实现自己的逻辑来查找 tgz 存档中的 jar。
group = "org.acme"
version = "1.0-SNAPSHOT"
task discoverAllJars{
ext.discoveredFiles = []
doLast{
file("jars").eachFile{file ->
if(file.name.endsWith("jar")){
println "found file ${file.name}"
discoveredFiles << file
artifacts{
allJars file
}
}
}
}
}
为了能够在 uploadAllJars 任务中上传多个工件,您必须使用 pom 过滤器。有关 pom 过滤器的详细信息,请查看http://www.gradle.org/docs/current/userguide/maven_plugin.html#uploading_to_maven_repositories上的 gradle 用户指南
由于我们将已发布工件的配置移至 gradle 的执行阶段,因此我们也必须在执行阶段配置 uploadAllJars。因此我会创建一个 configureUploadAllJars 任务。请注意我们如何引用使用 'discoverAllJars.discoveredFiles' 发现的 jar 文件:
task configureUploadAllJars{
dependsOn discoverAllJars
doLast{
uploadAllJars {
repositories {
mavenDeployer {
repository(url: 'http://yourrepository/') {
authentication(userName: 'admin', password: 'password');
}
discoverAllJars.discoveredFiles.each{discoveredFile ->
def filterName = discoveredFile.name - ".jar"
addFilter(filterName) { artifact, file ->
file.name == discoveredFile.name
}
pom(filterName).artifactId = filterName
}
}
}
}
}
}
现在您只需要在 uploadAllJars 和 configureUploadAllJars 之间添加一个依赖项:
uploadAllJars.dependsOn configureUploadAllJars
此示例对所有发现的 jar 文件使用相同的组和版本,并将 jar 名称用作 artifactId。您可以使用 pom 过滤器机制随意更改它。
希望有帮助,
干杯,勒内