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

huangapple 未分类评论58阅读模式
英文:

How to group multiple dependency in same line - Gradle

问题

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

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

期望的输出如下:

  1. 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.

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

Expected like below

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

Is it possible in gradle?

答案1

得分: 1

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

  1. def groupDependencies(group, names, version) {
  2. def deps = []
  3. names.each { it ->
  4. deps += [group: group, name: it, version: version]
  5. }
  6. return deps
  7. }
  8. dependencies {
  9. compile(groupDependencies('org.apache.ws.commons.axiom', ['axiom-api', 'axiom-dom', 'axiom-impl'], '1.2.7'))
  10. }
英文:

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:

  1. def groupDependencies ( group, names, version ) {
  2. def deps = []
  3. names.each { it ->
  4. deps += [group: group, name: it, version: version]
  5. }
  6. return deps
  7. }
  8. dependencies {
  9. compile(groupDependencies('org.apache.ws.commons.axiom', ['axiom-api', 'axiom-dom', 'axiom-impl'], '1.2.7'))
  10. }

答案2

得分: 0

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

  1. def groupDependencies(group, artifact) {
  2. def dependencies = []
  3. artifact.each { it ->
  4. dependencies += [group: group, name: it["name"], version: it["version"]]
  5. }
  6. return dependencies
  7. }
  8. dependencies {
  9. implementation(groupDependencies("org.apache.commons", [
  10. [name: "commons-lang3", version: "3.12.0"],
  11. [name: "commons-collections4", version: "4.4"],
  12. [name: "commons-compress", version: "1.21"]
  13. ]))
  14. }

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

  1. def groupDependencies(artifact) {
  2. def dependencies = []
  3. artifact.each { it ->
  4. dependencies += [group: it["name"], name: it["name"], version: it["version"]]
  5. }
  6. return dependencies
  7. }
  8. dependencies {
  9. implementation(groupDependencies([
  10. [name: "commons-cli", version: "1.5.0"],
  11. [name: "commons-io", version: "2.11.0"]
  12. ]))
  13. }
英文:

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

  1. def groupDependencies (group, artifact) {
  2. def dependencies = []
  3. artifact.each { it ->
  4. dependencies += [group: group, name: it["name"], version: it["version"]]
  5. }
  6. return dependencies
  7. }
  8. dependencies {
  9. implementation(groupDependencies("org.apache.commons", [
  10. [name: "commons-lang3", version: "3.12.0"],
  11. [name: "commons-collections4", version: "4.4"],
  12. [name: "commons-compress", version: "1.21"]
  13. ]))
  14. }

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

  1. def groupDependencies (artifact) {
  2. def dependencies = []
  3. artifact.each { it ->
  4. dependencies += [group: it["name"], name: it["name"], version: it["version"]]
  5. }
  6. return dependencies
  7. }
  8. dependencies {
  9. implementation(groupDependencies([
  10. [name: "commons-cli", version: "1.5.0"],
  11. [name: "commons-io", version: "2.11.0"]
  12. ]))
  13. }

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

发表评论

匿名网友

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

确定