如何使用Gradle将依赖项工件上传到Maven存储库(NEXUS)?

时间:2016-04-26 19:36:57

标签: maven gradle nexus

我正在尝试从一个Nexus存储库下载工件,并使用Gradle将其上传到另一个工具库。

My Gradle构建文件如下:

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


        }
    }
}

上传工作,它上传一个具有正确组,工件ID和版本的Jar。它还会将其上传到正确的位置。

问题:

上传的jar是一个包含要上传的实际jar的存档。

例如,如果我想下载artifact.jar并将其上传到另一个nexus存储库,脚本会将artifact.jar上传到正确的nexus存储库,但如果我下载上传的artifact.jar并且打开存档,我会在其中找到已下载的artifact.jar

2 个答案:

答案 0 :(得分:0)

我解决了这个问题。更新后的脚本如下:

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


        }
    }
}

而不是使用" upload_artifact"任务指定要上传的工件,我直接将该文件作为参数传递给artifact任务的maven_artifact方法。

答案 1 :(得分:0)

我自从扩展此解决方案以包含所有工件

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
        })
    }
}
相关问题