【问题标题】:Custom configuration and resolving only compile dependencies自定义配置和解析仅编译依赖项
【发布时间】:2021-02-01 16:31:36
【问题描述】:

我目前正在编写一个小插件,但是当我想获取所有使用的依赖项的列表时我被卡住了。

我在做什么 在插件内部我创建了一个新配置

def config = project.configurations.create(sourceSet.getTaskName('foo', 'bar'))

在使用插件的build.gradle 中,我为此配置添加了一些依赖项

dependencies {
  fooTestBar(project(':module'))
}

module/build.gradle 我有

plugins {
  id 'java-library'
}
dependencies {
  implementation('org.apache.commons:commons-collections4:4.4')
  api('org.springframework:spring-context:5.2.11.RELEASE')
}

当我现在在插件中执行以下操作时

List<ResolvedArtifact> = config.resolvedConfiguration.firstLevelModuleDependencies.allModuleArtifacts.flatten()

我从:module 中的两个声明中获得了工件,但我感兴趣的只是 api 依赖项,即在编译项目时也使用的那个

看起来整个配置都被视为runtime 配置,所以我拥有所有工件,包括两个声明中的传递性,而不是只有 api 一个,包括来自 api 的传递性

直到现在我无法找到任何方法来查看已解决的依赖项/工件是否属于我不想在结果列表中包含的 api 类型

【问题讨论】:

    标签: gradle groovy plugins configuration dependencies


    【解决方案1】:

    我必须为用法添加属性

    config.attributes {
        attribute( Usage.USAGE_ATTRIBUTE, objects.named( Usage, Usage.JAVA_API ) )
    }
    

    https://docs.gradle.org/current/userguide/variant_model.html https://docs.gradle.org/current/userguide/variant_attributes.html

    感谢 Chris Doré https://discuss.gradle.org/t/custom-configuration-and-resolving-only-compile-dependencies/38891

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2013-09-19
      相关资源
      最近更新 更多