Custom configuration and resolving only compile dependencies

i’m currently writing a small plugin but i’m stuck when i want to get a list of all dependencies that are used.

what i’m doing
inside the plugin i create a new configuration

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

in the build.gradle that uses the plugin i add some dependencies to this configuration

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

and in module/build.gradle i have

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

when i now do the following in the plugin

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

i get the artifacts from both declarations in :module, but what i’m interested in is only the api dependency, means the one that is also used when compiling the project

it looks like the entire configurations is treated as a runtime configuration, so i have all artifacts including the transitive ones from both declarations, instead of only the api one including the transitive ones from api

until now i was not able to find any way to see if a resolved dependency / artifact is of type api which i do not want to have in my result list

Attributes are used to drive the variant aware dependency resolution.
https://docs.gradle.org/current/userguide/variant_model.html
https://docs.gradle.org/current/userguide/variant_attributes.html

At the very least you need to define org.gradle.usage=JAVA_API to get what you want.

// optional; you can also describe the intended usage of your configuration
config.canBeConsumed = false 
config.canBeResolved = true
config.attributes {
    attribute( Usage.USAGE_ATTRIBUTE, objects.named( Usage, Usage.JAVA_API ) )
}
1 Like