Gradle不在发布的pom.xml中包含依赖项

我有一个Gradle项目我正在使用maven-publisher插件来安装我的android库到maven本地和一个maven回购。

这工作,但生成的pom.xml不包括任何依赖信息。 有没有解决方法来包含这些信息,还是我不得不回到maven插件并进行所有需要的手动configuration?


研究我意识到,我没有告诉出版物的依赖关系是什么,我只是指定的输出/神器,所以我需要一种方法来连接这个MavenPublication的依赖,但我还没有find如何做到这一点文件。

 -------------------------------------------------- ----------
 Gradle 1.10
 -------------------------------------------------- ----------

生成时间:2013-12-17 09:28:15 UTC
内部版本号:无
修订:36ced393628875ff15575fa03d16c1349ffe8bb6

 Groovy:1.8.6
 Ant:2013年7月8日编译的Apache Ant(TM)1.9.2版
常春藤:2.2.0
 JVM:1.7.0_60(Oracle Corporation 24.60-b09)
操作系统:Mac OS X 10.9.2 x86_64

相关的build.gradle部分

 //... apply plugin: 'android-library' apply plugin: 'robolectric' apply plugin: 'maven-publish' //... repositories { mavenLocal() maven { name "myNexus" url myNexusUrl } mavenCentral() } //... android.libraryVariants publishing { publications { sdk(MavenPublication) { artifactId 'my-android-sdk' artifact "${project.buildDir}/outputs/aar/${project.name}-${project.version}.aar" } } repositories { maven { name "myNexus" url myNexusUrl credentials { username myNexusUsername password myNexusPassword } } } } 

生成的pom.xml:

 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>org.example.android</groupId> <artifactId>my-android-sdk</artifactId> <version>gradle-SNAPSHOT</version> <packaging>aar</packaging> </project> 

我能够通过让脚本直接使用pom.withXml添加依赖到pom来解决pom.withXml

 //The publication doesn't know about our dependencies, so we have to manually add them to the pom pom.withXml { def dependenciesNode = asNode().appendNode('dependencies') //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each configurations.compile.allDependencies.each { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) } } 

这适用于我的项目,可能会在其他人有不可预见的后果。

我升级了C.Ross解决scheme。 这个例子将生成pom.xml,其中包含来自编译configuration的依赖关系以及特殊的构buildtypes依赖关系,例如,如果您对版本或debugging版本(debugCompile和releaseCompile)使用不同的依赖关系。 而且它增加了exlusions

 publishing { publications { // Create different publications for every build types (debug and release) android.buildTypes.all { variant -> // Dynamically creating publications name "${variant.name}Aar"(MavenPublication) { def manifest = new XmlSlurper().parse(project.android.sourceSets.main.manifest.srcFile); def libVersion = manifest['@android:versionName'].text() def artifactName = project.getName() // Artifact properties groupId GROUP_ID version = libVersion artifactId variant.name == 'debug' ? artifactName + '-dev' : artifactName // Tell maven to prepare the generated "*.aar" file for publishing artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar") pom.withXml { //Creating additional node for dependencies def dependenciesNode = asNode().appendNode('dependencies') //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile) def configurationNames = ["${variant.name}Compile", 'compile'] configurationNames.each { configurationName -> configurations[configurationName].allDependencies.each { if (it.group != null && it.name != null) { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) //If there are any exclusions in dependency if (it.excludeRules.size() > 0) { def exclusionsNode = dependencyNode.appendNode('exclusions') it.excludeRules.each { rule -> def exclusionNode = exclusionsNode.appendNode('exclusion') exclusionNode.appendNode('groupId', rule.group) exclusionNode.appendNode('artifactId', rule.module) } } } } } } } } } } 

我想这与from components.java指令有关,如指南中所见。 我有一个类似的设置,它有所不同,将行添加到发布块:

 publications { mavenJar(MavenPublication) { artifactId 'rest-security' artifact jar from components.java } }