简体   繁体   中英

How to upload a dependency Artifact to Maven Repository(NEXUS) using Gradle?

I am trying to download an artifact from one Nexus repository and upload it to another using Gradle.

My Gradle build file is as follows:

dependencies {
    compile group: ARTIFACT_GROUP_ID, name: ARTIFACT_ARTIFACT_ID, version: ARTIFACT_VERSION
}

// Get dependency Artifact file
task upload_artifact(type: Jar){
    from(file(project.configurations.compile.find { it.name.startsWith(ARTIFACT_ARTIFACT_ID+"-"+ARTIFACT_VERSION) }))
}

// Finally publish the artifact
publishing {
    repositories{
            maven{
                url NEXUS_URL
                credentials {
                    username NEXUS_USER
                    password NEXUS_PASSWORD
                }

            }
        }
    publications {
        maven_artifact(MavenPublication) {

            //GAV Co-ordinates to use to publish the artifact 
            artifact upload_artifact
            groupId ARTIFACT_GROUP_ID
            artifactId ARTIFACT_ARTIFACT_ID
            version ARTIFACT_UPLOAD_VERSION


        }
    }
}

The Upload works, it uploads a Jar with the correct group, artifact id and version. It also uploads it to the correct location.

PROBLEM:

The uploaded jar is an archive containing the actual jar to be uploaded.

For example if I want to download artifact.jar and upload it to another nexus repository, the script uploads an artifact.jar to the correct nexus repository, but if I download the uploaded artifact.jar and open the archive, I find the downloaded artifact.jar within it.

I got this fixed. The updated script is as follows:

dependencies {
    compile group: ARTIFACT_GROUP_ID, name: ARTIFACT_ARTIFACT_ID, version: ARTIFACT_VERSION
}

// Finally publish the artifact
publishing {
    repositories{
            maven{
                url NEXUS_URL
                credentials {
                    username NEXUS_USER
                    password NEXUS_PASSWORD
                }

            }
        }
    publications {
        maven_artifact(MavenPublication) {

            //GAV Co-ordinates to use to publish the artifact 
            artifact file(project.configurations.compile.find { it.name.startsWith(ARTIFACT_ARTIFACT_ID+"-"+ARTIFACT_VERSION) })
            groupId ARTIFACT_GROUP_ID
            artifactId ARTIFACT_ARTIFACT_ID
            version ARTIFACT_UPLOAD_VERSION


        }
    }
}

Instead of using the "upload_artifact" task to specify the artifact being uploaded, I directly passed the file as a parameter to the artifact method of the maven_artifact task.

I've since extended this solution to include all artifacts

apply plugin: 'java'
apply plugin: 'maven-publish'

dependencies {
    runtime 'log4j:log4j:1.2.17'
    runtime 'junit:junit:4.12'
}

repositories {
    flatDir {
        dirs project.projectDir
    }
}

publishing  {
    repositories {
        maven {
            url 'http://example.org/nexus/content/repositories/foo-releases'
            credentials {
                username 'username'
                password 'password'
            }
        }
    }

    configurations.runtime.allDependencies.each {
        def dep = it
        def file = file(configurations.runtime.find { it.name.startsWith("${dep.name}-${dep.version}") })

        publications.create(it.name, MavenPublication, {
            artifact file
            groupId dep.group
            artifactId dep.name
            version dep.version
        })
    }
}

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