简体   繁体   中英

How to upload an existing collection of 3rd-party Jars to a Maven server in Gradle?

How can I upload a collection of existing Jars to a Maven repository? The Jars are built from an ANT Task imported to Gradle, and used as a dependency to my task... The Jars don't have version tag, so they should ALL receive the same version number when they are uploaded...

apply plugin: 'java'
apply plugin: 'maven' 

version = "6.1.1"
group = "com.oahu" 

ant.importBuild "$projectDir/tools/ant/package.xml" 

uploadArchives(dependsOn: ["oahu-jar", "client-sdk-jar", "common-jar"]) << { 
    // the dependencies will generate "oahu.jar", "oahu_client_sdk.jar", "common.jar" 

    // UPLOAD THE DEPENDENCIES LISTED ABOVE LOCATED AT the subdirectory "build/" 

    description = "Uploads the generated jar ${archivesBaseName}-${version}.jar to ${cn_mvn_serverUrl}" 
    repositories.mavenDeployer { 
       repository(url: "${cn_mvn_releaseUrl}") { 
          authentication(userName: "${cn_mvn_username}", password: "${cn_mvn_password}") 
       } 
    } 
}

The tasks "oahu-jar", "client-sdk-jar", "common-jar" are the ones imported from ANT... I have the Maven repositories configuration already working from another project... But the Maven plugin uploads the Jar generated by the Jar task from the Java plugin... Considering the imported ANT tasks generates:

  • build.gradle
  • src
  • build |-"oahu.jar" |-"oahu_client_sdk.jar" |-"common.jar"

The result of this should be the upload of those Jars with the given version...

"oahu-6.1.1.jar", "oahu_client_sdk-6.1.1.jar", "common-6.1.1.jar"... all uploaded to the Maven repository...

Add sourceSets? Configuration? Artifacts?

currently that is not explicitly supported by gradle so you have to do some scripting for that. Based on your snippet above, I've created a sample snippet, that should be easy to adapt:

apply plugin:'java'
apply plugin:'maven'

import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact

version = "6.1.1"
group = "com.oahu" 

ant.importBuild "$projectDir/tools/ant/package.xml" 

// a list of the ant tasks that create a jar
// I assumed the following convention:
// ant task named "SampleAntJar-jar" creates the jar  "build/SampleAntJar.jar" 
def antJarTasks = ["SampleAntJar-jar", "SecondSampleAntJar-jar"]

artifacts{
    //for each ant task add a defaultpublishArtifact to the archives configuration
    antJarTasks.each{ taskName ->
        def artifactName = taskName - '-jar'
        archives new DefaultPublishArtifact(artifactName, "jar", "jar", null, new            
                       Date(), new File("$buildDir", "${artifactName}.jar"))    
    }
}

uploadArchives(){
    dependsOn: antJarTasks 
    repositories {
        mavenDeployer {
            repository(url: "file://{'/Users/Rene/.m2/repository/'}")
            antJarTasks.each{ antJarTask ->
                antJarName = antJarTask - "-jar"
                addFilter(antJarName) {artifact, file ->
                    artifact.name == antJarName
                }
                pom(antJarName).artifactId = antJarName
            }
        }
    }
}

regards, René

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM