2015-03-03 180 views
4

我正在使用Gradle構建Android庫項目並將其作爲aar部署到Maven存儲庫。POM與Gradle缺少Android庫依賴關係

圖書館具有一定的相關性,這應包括在POM

隨着apply plugin: 'maven'沒有POM文件不存在,只是產生一個POM文件中的神器

隨着apply plugin: 'maven-publish',但不包括任何依賴關係

任何想法?這只是不支持?

搖籃2.2和Android搖籃插件1.1.0

第一種方法:

configurations { 
    archives { 
     extendsFrom configurations.default 
    } 
} 

afterEvaluate { project -> 
    uploadArchives { 

     configuration = configurations.archives 

     repositories { 
      mavenDeployer { 
       repository(url: "http://nexus-url") { 
        authentication(userName: nexusUsername, password: nexusPassword) 

       pom.groupId = 'com.example' 
       pom.version = '123-SNAPSHOT' 
       pom.artifactId = 'foo' 
       pom.packaging = 'aar' 

       pom.project { 
        artifactId = 'bar' 
        packaging 'aar' 
        description 'baz' 
      } 
     } 
    } 
} 

也試過它沒有afterEvaluate

第二條本辦法包裹它:

更新

問題的根本原因是該項目使用了口味。如果不使用香精apply plugin: 'maven'

回答

10

這是最終爲我工作的解決方案:

publishing { 
    publications { 
     sdk(MavenPublication) { 
      artifactId libName 
      artifact "${project.buildDir}/outputs/aar/${libName}-${project.version}.aar" 

      //The publication doesn't know about our dependencies, so we have to manually add them to the pom 
      pom.withXml { 
       // for dependencies and exclusions 
       def dependenciesNode = asNode().appendNode('dependencies') 
       configurations.compile.allDependencies.withType(ModuleDepend‌​ency) { ModuleDependency dp -> 
        def dependencyNode = dependenciesNode.appendNode('dependency') 
        dependencyNode.appendNode('groupId', dp.group) 
        dependencyNode.appendNode('artifactId', dp.name) 
        dependencyNode.appendNode('version', dp.version) 

        // for exclusions 
        if (dp.excludeRules.size() > 0) { 
         def exclusions = dependencyNode.appendNode('exclusions') 
         dp.excludeRules.each { ExcludeRule ex -> 
          def exclusion = exclusions.appendNode('exclusion') 
          exclusion.appendNode('groupId', ex.group) 
          exclusion.appendNode('artifactId', ex.module) 
         } 
        } 
       } 
      } 
     } 
    } 

    repositories { 
     maven { 
      name 'myrepo' 
      url 'https://maven.foo.com' 

      credentials { 
       username mavenUsername 
       password mavenPassword 
      }  
     } 
    } 
} 
+1

爲了避免出現異常,最好將'configurations.compile.allDependencies.each'替換爲'configurations.compile.allDependencies.withType(ModuleDependency)'。你使用'compile fileTree(dir:'libs',include:['* .jar'])'' – 2017-10-26 14:25:21

0

嘗試mavenDeployer當POM是正確生成:http://gradle.org/docs/current/userguide/maven_plugin.html

uploadArchives { 
    repositories { 
     mavenDeployer { 
      repository(url: "file://localhost/tmp/myRepo/") 
      pom.version = '1.0Maven' 
      pom.artifactId = 'myMavenName' 
     } 
    } 
} 

在這裏你可以設置POM細節。你會得到一個新的目標叫做uploadArchives。執行時,它將部署到給定的回購。

+0

謹防快照的相關性,搖籃不遵循此理念行家,這意味着如果部署快照的依賴,gradle這個不會對其進行更新如果它已經得到了相同的版本(AFAIK) – sschrass 2015-03-03 10:07:05

+0

謝謝,但多數民衆贊成我的意思是當我提到'申請插件:'maven'' 我會發布一些代碼的兩個變種 – 2015-03-03 13:18:09

+0

需要再次閱讀 - >你不要在這個pom中有依賴關係。沒有理由,因爲你的依賴被烘焙到工件中。這就是編譯的過程。這pom只是你的神器的描述。 – sschrass 2015-03-03 13:31:52