如何在Gradle构建脚本中解析依赖项扩展

时间:2014-08-19 10:31:08

标签: gradle build.gradle

我正在使用maven-publish中的build.gradle插件。对于每个compile依赖项,groupnameversion用于在生成的pom.xml文件中生成相应的标记 - 这样可以正常工作

以下是我的示例依赖项:

dependencies {
  compile (
    [group: 'com.actionbarsherlock', name: 'actionbarsherlock', version: '4.4.0',  ext: 'aar'],
    [group: 'com.android.support',   name: 'support-v4',        version: '19.0.+']
  )}

这是我的发布元素:

publishing {
  publications {
    // A publication named 'mavenAndroid'
    mavenPublishLibrary(MavenPublication) {
      // Artifact using the aar from the build output
      artifact "${project.projectDir}/build/outputs/aar/${project.name}-${project.version}.aar"

      // Generate dependencies manually for POM
      pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')

        //Iterate over the compile dependencies, 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)

          // This is what I'd want to do...
          if (it.ext != '') {
            dependencyNode.appendNode('type', it.ext)
          }
          // ...but this doesn't work!
        }
      }
    }
  }
}

我希望能够将ext值(例如aar)与pom.xmlgroup和{name一起添加到version {1}},就像我在上面代码段中所拥有的那样。

1 个答案:

答案 0 :(得分:2)

当我经历了几个小时的挣扎时,我会问这些问题,然后我总是在问完后不久就回答自己!

我使用以下内容获取依赖项扩展,替换问题中的if()

it.getArtifacts().each {
  dependencyNode.appendNode('type', it.getExtension())
}