如何在同一行中分组多个Gradle依赖项

huangapple go评论75阅读模式
英文:

How to group multiple dependency in same line - Gradle

问题

在下面的示例中,我有三个具有相同组和相同版本的依赖项。我想要将这三个依赖项合并成一行

compile group: 'org.apache.ws.commons.axiom', name: 'axiom-api', version: '1.2.7'
compile group: 'org.apache.ws.commons.axiom', name: 'axiom-dom', version: '1.2.7'
compile group: 'org.apache.ws.commons.axiom', name: 'axiom-impl', version: '1.2.7'

期望的输出如下:

group("axiom-api", "axiom-dom", "axiom-impl", :under=>"org.apache.ws.commons.axiom", :version=>"1.2.7")

gradle中是否有可能实现这一点?

英文:

In the following example, I have 3 dependencies with the same group and the same version. I want to group these three dependencies in one line.

compile group: 'org.apache.ws.commons.axiom', name: 'axiom-api', version: '1.2.7'
compile group: 'org.apache.ws.commons.axiom', name: 'axiom-dom', version: '1.2.7'
compile group: 'org.apache.ws.commons.axiom', name: 'axiom-impl', version: '1.2.7'

Expected like below

group("axiom-api", "axiom-dom", "axiom-impl", :under=>"org.apache.ws.commons.axiom", :version=>"1.2.7")

Is it possible in gradle?

答案1

得分: 1

以下是您提供的代码的翻译部分:

def groupDependencies(group, names, version) {
  def deps = []
  names.each { it ->
    deps += [group: group, name: it, version: version]
  }

  return deps
}

dependencies {
  compile(groupDependencies('org.apache.ws.commons.axiom', ['axiom-api', 'axiom-dom', 'axiom-impl'], '1.2.7'))
}
英文:

Well it's actually more lines in total, but if this (same group, same version and multiple artifacts) is a repeating pattern, it might still be handy:

def groupDependencies ( group, names, version ) {
  def deps = []
  names.each { it -> 
    deps += [group: group, name: it, version: version]
  }

  return deps
}

dependencies {
  compile(groupDependencies('org.apache.ws.commons.axiom', ['axiom-api', 'axiom-dom', 'axiom-impl'], '1.2.7'))
}

答案2

得分: 0

这是一个示例,您可以在其中添加具有不同版本的不同构件。

def groupDependencies(group, artifact) {
    def dependencies = []

    artifact.each { it ->
        dependencies += [group: group, name: it["name"], version: it["version"]]
    }

    return dependencies
}

dependencies {
    implementation(groupDependencies("org.apache.commons", [
            [name: "commons-lang3", version: "3.12.0"],
            [name: "commons-collections4", version: "4.4"],
            [name: "commons-compress", version: "1.21"]
    ]))
}

另一个示例用于在组和名称相同时(您可以在同一文件中同时使用它们):

def groupDependencies(artifact) {
    def dependencies = []

    artifact.each { it ->
        dependencies += [group: it["name"], name: it["name"], version: it["version"]]
    }

    return dependencies
}

dependencies {
    implementation(groupDependencies([
            [name: "commons-cli", version: "1.5.0"],
            [name: "commons-io", version: "2.11.0"]
    ]))
}
英文:

This is an example where you can add different artifacts each with a different version.

def groupDependencies (group, artifact) {
    def dependencies = []

    artifact.each { it ->
        dependencies += [group: group, name: it["name"], version: it["version"]]
    }

    return dependencies
}

dependencies {
    implementation(groupDependencies("org.apache.commons", [
            [name: "commons-lang3", version: "3.12.0"],
            [name: "commons-collections4", version: "4.4"],
            [name: "commons-compress", version: "1.21"]
    ]))
}

And this other is for when the group and the name is the same (you can use them both on the same file):

def groupDependencies (artifact) {
    def dependencies = []

    artifact.each { it ->
        dependencies += [group: it["name"], name: it["name"], version: it["version"]]
    }

    return dependencies
}

dependencies {
    implementation(groupDependencies([
            [name: "commons-cli", version: "1.5.0"],
            [name: "commons-io", version: "2.11.0"]
    ]))
}

huangapple
  • 本文由 发表于 2020年7月28日 23:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63137895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定