2016-04-26 80 views
1

我想從一個Nexus存儲庫下載一個工件,並使用Gradle將其上傳到另一個工作站。如何使用Gradle將依賴項Artifact上傳到Maven Repository(NEXUS)?

我的搖籃建立文件如下:

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和版本罐子。它還將其上傳到正確的位置。

問題:

上傳的罐子是將要上傳包含實際罐子一個歸檔。

例如,如果我想下載artifact.jar並上傳到另一個關係庫,腳本上傳的artifact.jar正確的關係庫,但如果我下載上傳artifact.jar並打開壓縮包,我發現在它的下載artifact.jar

回答

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」任務,以指定的神器被上傳,我直接通過文件作爲參數傳遞給maven_artifact任務的artifact方法。

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 
     }) 
    } 
} 
相關問題