Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading multiple existing JARs to Maven repository with Gradle

Tags:

gradle

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.

like image 593
Ivan Dubrov Avatar asked Apr 25 '13 21:04

Ivan Dubrov


1 Answers

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é

like image 86
Rene Groeschke Avatar answered Oct 02 '22 11:10

Rene Groeschke