I have an attribute A defined with values A1 and A...
# community-support
s
I have an attribute A defined with values A1 and A2, where A1 is compatible with A2 but not vice versa. I have a configuration which requests A2 (so some dependencies provide A1, others A2) but in my task I need to handle A1s and A2s separately. I thought of using
configuration.incoming.artifactView { attribute(A, A1) }
and the same for A2 but when I filter for A2 I get all the artifacts (probably because of compatibility). Can I somehow filter a configuration based on the exact attribute value?
v
Yes, just a bit cumbersome as it is not exactly meant to be done. On the artifact view you can get
.artifacts.resolvedArtifacts
. On the result you can then get the actual attribute values and do your own filtering using
ResolvedArtifactResult#variant.attributes
. You get the key set on that, search the attribute by
name
and then get the value for the attribute. Remember to do it like I said and not use some ready-made attribute instance as key, because whether an artifact is coming for example from the currently running code or from an included build, or from a Gradle Module Metadata file makes big differences, but the
name
is stable to use here. The value can also be differently typed the same, for example if an
OperatingSystemFamily
attribute is coming from running code, the value will be
OperatingSystemFamily
, if it is coming from GMM, it will be
String
. So you should also thoroughly test all possible situations. 🙂
s
ok so it’s like “Gradle has an API to do what you want but it’s not going to do what you want so here’s another way” - and that twice 🙂 thanks!
(1. Gradle has artifact views but they won’t filter the way I want. 2. Gradle has attributes map but looking up by key won’t work the way I want in some situations.)
v
Yep, as I said, it's probably not intended to be used like that, but I had a similar need and found this way after much fiddling around 🙂
s
hmm yeah, I use a Kotlin enum and MyEnum.FOO does not equal MyEnum.FOO…