【问题标题】:How to upload a dependency Artifact to Maven Repository(NEXUS) using Gradle?如何使用 Gradle 将依赖项工件上传到 Maven 存储库(NEXUS)?
【发布时间】:2016-04-26 19:36:57
【问题描述】:

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

我的 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

【问题讨论】:

    标签: maven gradle nexus


    【解决方案1】:

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

    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”任务来指定要上传的工件,而是直接将文件作为参数传递给maven_artifact任务的artifact方法。

    【讨论】:

      【解决方案2】:

      我已经扩展了这个解决方案以包含所有工件

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

      【讨论】:

        猜你喜欢
        • 2014-11-28
        • 1970-01-01
        • 2015-04-22
        • 2014-03-21
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        • 2012-05-20
        相关资源
        最近更新 更多