使用Gradle将多个现有JAR上传到Maven存储库 [英] Uploading multiple existing JARs to Maven repository with Gradle

查看:355
本文介绍了使用Gradle将多个现有JAR上传到Maven存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个Gradle任务,它将多个现有的JAR上传到Maven仓库。



棘手的部分是JAR的列表事先不知道。我希望它的工作方式是首先下载某些.tar.gz文件,解压缩它,然后扫描JAR并使用某种命名约定上传它们(如使用JAR名称作为artifactId和.tar的版本。 gz as version)。



用Gradle做这件事最简单的方法是什么?目前,它是一个简单的bash脚本,它搜索JAR并运行Maven deploy:为每个脚本部署文件,但我需要将该功能合并到gradle构建脚本中。



理想情况下,我需要像deployJars这样的任务来上传所有内容,并依赖于downloadTarGz任务。

更新:



如何在没有任何JAR的情况下上传POM?我需要生成几个POM,这些POM将依赖于这些动态检测到的JAR并上传它们。 artifacts需要我指定要上传的文件。

解决方案

要通过gradle上传jar,您必须将该jar声明为发布artifact并使用artifacts闭包将其添加到特定配置中:

  apply plugin:'maven'

配置{
allJars
}

artifacts {
allJars file(path / to / jarFile.jar)
}

现在您可以配置动态创建的uploadAllJars任务:

<$ p $ uploadAllJars {
repositories {
mavenDeployer {
repository(url:'http:// localhost:8081 / artifactory / acme'){
authentication (userName:'admin',密码:'password');



$ / code $ / pre

问题是你想要上传多个工件。为了实现这一点,您需要在构建脚本中更加动态。所有发现的jar的动态创建publishartifacts可以包装在一个任务中。在我的示例中,discoverAllJars任务只是查找指定文件夹中的jar文件。在这里你需要实现你自己的逻辑来查找你的tgz压缩包中的jar包。

  group =org.acme
version =1.0-SNAPSHOT

任务discoverAllJars {
ext.discoveredFiles = []
doLast {
file(jars)。eachFile {file - >
if(file.name.endsWith(jar)){
printlnfound file $ {file.name}
discoveredFiles<<文件
artifacts {
allJars文件
}
}
}
}
}

为了能够在uploadAllJars任务中上传多个工件,您必须使用pom过滤器。有关pom过滤器的详细信息,请参阅gradle用户指南,网址为 http:// www.gradle.org/docs/current/userguide/maven_plugin.html#uploading_to_maven_repositories



由于我们将发布的构件的配置移至执行阶段gradle,我们也必须在执行阶段配置uploadAllJars。因此我会创建一个configureUploadAllJars任务。注意我们如何引用通过使用'discoverAllJars.discoveredFiles'发现的jar文件:

 任务configureUploadAllJars {
dependsOn discoverAllJars
doLast {
uploadAllJars {
repositories {
mavenDeployer {
repository(url:'http:// yourrepository /'){
authentication(userName:' admin',密码:'password');
}
discoverAllJars.discoveredFiles.each {discoveredFile - >
def filterName = discoveredFile.name - .jar
addFilter(filterName){artifact,file - >
file.name == discoveredFile.name
}
pom(filterName).artifactId = filterName
}
}
}
}




$ b现在你只需要在uploadAllJars和configureUploadAllJars之间添加一个依赖关系:

  uploadAllJars.dependsOn configureUploadAllJars 

此示例对所有发现的jar文件使用相同的组和版本,将jar名称作为artifactId。你可以使用pom过滤机制来改变它。



希望有帮助,

干杯,
René


I need to implement a Gradle task that will upload multiple existing JARs to the Maven repository.

The tricky part is that list of JARs is not known in advance. The way I want it to work is to dowload certain ".tar.gz" file first, untar it, then scan for JARs and upload them with some sort of naming convention (like use JAR names as artifactId's and version of the .tar.gz as version).

What would be the easiest way to do that with Gradle? Currently it is a simple bash script which searches for JARs and runs Maven deploy:deploy-file for each of them, but I need to incorporate that functionality in gradle build script.

Ideally, I need task like "deployJars" that would upload everything and depend on "downloadTarGz" task.

Update:

How about uploading POMs without any JAR attached to it? I need to generate several POMs that will depend on these dynamically detected JARs and upload them, too. "artifacts" requires me to specify file for upload.

解决方案

To upload a jar via gradle you must declare that jar as a publish artifact and add it to a specific configuration using the artifacts closure:

apply plugin:'maven'

configurations{
    allJars
}

artifacts{
    allJars file("path/to/jarFile.jar")
}

now you can configure the dynamically created uploadAllJars task:

uploadAllJars {
   repositories {  
        mavenDeployer {  
            repository(url: 'http://localhost:8081/artifactory/acme') {  
                authentication(userName: 'admin', password: 'password');  
            }
       }
 }  

The problem is that you want to upload multiple artifacts. To achieve that you need some more dynamic in your build script. The dynamic creation of publishartifacts for all discovered jars can be wrapped in a task. In my example the discoverAllJars task simply looks in a specified folder for jar files. Here you need to implement your own logic to lookup the jars in your tgz archive.

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

To be able to upload multiple artifacts within the uploadAllJars task you have to use pom filter. For details about pom filter have a look at the gradle userguide at http://www.gradle.org/docs/current/userguide/maven_plugin.html#uploading_to_maven_repositories

Since we moved the configuration of the published artifacts into the execution phase of gradle we must configure the uploadAllJars in the execution phase too. Therefore I'd create a configureUploadAllJars task. Note how we reference the jar files discovered by using 'discoverAllJars.discoveredFiles':

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

Now you just need to add a dependency between uploadAllJars and configureUploadAllJars:

uploadAllJars.dependsOn configureUploadAllJars    

This example uses the same group and version for all discovered jar files and the jar name as artifactId. you can change this as you like using the pom filter mechanism.

hope that helped,

cheers, René

这篇关于使用Gradle将多个现有JAR上传到Maven存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆