【问题标题】:How to specify properties on groups of dependencies in Gradle?如何在 Gradle 中指定依赖组的属性?
【发布时间】:2012-05-20 05:05:43
【问题描述】:

使用 Gradle,我希望能够禁用一组依赖项的传递性,同时仍然允许其他依赖项。像这样的:

// transitivity enabled
compile(
  [group: 'log4j', name: 'log4j', version: '1.2.16'],
  [group: 'commons-beanutils', name: 'commons-beanutils', version: '1.7.0']
)

// transitivity disabled
compile(
  [group: 'commons-collections', name: 'commons-collections', version: '3.2.1'],
  [group: 'commons-lang', name: 'commons-lang', version: '2.6'],
) { 
  transitive = false
}

Gradle 不接受这种语法。如果我这样做,我可以让它工作:

compile(group: 'commons-collections', name: 'commons-collections', version: '3.2.1') { transitive = false }
compile(group: 'commons-lang', name: 'commons-lang', version: '2.6']) { transitive = false }

但这需要我指定每个依赖项的属性,而我宁愿将它们组合在一起。

有人对适用于此的语法有建议吗?

【问题讨论】:

    标签: build gradle dependency-management


    【解决方案1】:

    创建单独的配置,并在所需配置上设置transitive = false。 在依赖项中,只需将配置包含到 compile 或它们所属的任何其他配置中

    configurations {
        apache
        log {
            transitive = false
            visible = false //mark them private configuration if you need to
        }
    }
    
    dependencies {
        apache 'commons-collections:commons-collections:3.2.1'
        log 'log4j:log4j:1.2.16'
    
        compile configurations.apache
        compile configurations.log
    }
    

    上面的内容让我可以禁用日志相关资源的传递依赖项,同时我将默认传递 = true 应用于 apache 配置。

    根据 tair 的评论在下面编辑:

    这会解决吗?

    //just to show all configurations and setting transtivity to false
    configurations.all.each { config->
        config.transitive = true
        println config.name + ' ' + config.transitive
    }
    

    然后运行 gradle 依赖

    查看依赖项。我正在使用 Gradle-1.0,就显示依赖关系而言,当同时使用传递性 false 和 true 时,它​​的行为还可以。

    我有一个活动项目,当使用上述方法将传递性转换为 true 时,我有 75 个依赖项,而当传递到 false 时,我有 64 个。

    值得对构建工件进行类似的检查。

    【讨论】:

      【解决方案2】:

      首先,有一些方法可以简化(或至少缩短)您的声明。例如:

      compile 'commons-collections:commons-collections:3.2.1@jar'
      compile 'commons-lang:commons-lang:2.6@jar'
      

      或者:

      def nonTransitive = { transitive = false }
      
      compile 'commons-collections:commons-collections:3.2.1', nonTransitive
      compile 'commons-lang:commons-lang:2.6', nonTransitive
      

      为了一次创建、配置和添加多个依赖项,您必须引入一些抽象。比如:

      def deps(String... notations, Closure config) { 
          def deps = notations.collect { project.dependencies.create(it) }
          project.configure(deps, config)
      }
      
      dependencies {
          compile deps('commons-collections:commons-collections:3.2.1', 
                  'commons-lang:commons-lang:2.6') { 
              transitive = false
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 2020-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多