Slackbot
12/19/2023, 11:51 PMVampire
12/20/2023, 9:15 AMAdam
12/20/2023, 9:18 AMartifactView {}, but it's pretty flaky.
Example:
/** Some specific type of a 'Foo' configuration */
interface FooArtifactTypeAttribute : Named
val fooArtifactTypeAttribute = Attribute.of(FooArtifactTypeAttribute::class.java)
val fooConfigurationResolver by configurations.registering {
description = "fetches 'Foo' files which have a 'Foo' attribute (should include all 'FooArtifactTypes')"
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named("foo-artifacts"))
}
}
val gatherFooFilesWithTypeJar by tasks.registering(Sync::class) {
val fooJarFiles: Provider<List<File>> =
fooConfigurationResolver.flatMap { conf ->
conf.incoming
.artifactView {
attributes {
// only find 'jars'
attribute(fooArtifactTypeAttribute, objects.named("jars"))
}
}
.artifacts
.resolvedArtifacts
.map { it.map(ResolvedArtifactResult::getFile) }
}
from(fooJarFiles)
into(temporaryDir)
}
But there are a tonne of hidden problems, because Gradle randomly doesn't consider attributes or will decide it doesn't need to do variant re-selection and so you end up with files from Configurations that have completely the wrong attributes.
What I've been experimenting with recently is manually filtering the discovered artifacts, because if you use resolvedArtifacts then you can view the variant that provided each file, and then filter based on the available attributes.
val gatherFooFilesWithTypeJar by tasks.registering(Sync::class) {
val fooJarFiles: Provider<List<File>> =
fooConfigurationResolver.flatMap { conf ->
conf.incoming
// .artifactView {
// attributes {
// attribute(fooArtifactTypeAttribute, objects.named("jars"))
// }
// }
.artifacts
.resolvedArtifacts
.map { artifacts ->
artifacts
.filter { artifact ->
// only find 'jars'
artifact.variant.attributes.getAttribute(fooArtifactTypeAttribute) == objects.named("jars")
}
.map { it.file }
}
}
from(fooJarFiles)
into(temporaryDir)
}
I haven't used it in anger yet, so I'm curious whether it might help you :)Vampire
12/20/2023, 9:20 AMChris Doré
12/20/2023, 4:54 PMAdam
12/20/2023, 5:26 PMChris Doré
12/20/2023, 5:27 PMall variant that includes each of the artifacts. The consumer can then request all instead of enumerating the possible variants. New variants can be added without having to update the consumer. This is new stuff and I don't have to deal with any legacy modules that would be missing the all variant.Vampire
12/20/2023, 7:14 PMVampire
12/20/2023, 7:16 PM