This message was deleted.
# community-support
s
This message was deleted.
t
Copy code
class DirectMetadataAccessVariantRule implements ComponentMetadataRule {
    @javax.inject.Inject
    ObjectFactory getObjects() { }
    void execute(ComponentMetadataContext context) {
        def id = context.details.id
        context.details.addVariant("moduleMetadata") {
            attributes {
                attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
                attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.REGULAR_PLATFORM))
                attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "platform-metadata"))
            }
            withFiles {
                removeAllFiles()
                addFile("${id.name}-${id.version}.module")
            }
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    components {
        all(DirectMetadataAccessVariantRule)
    }
}

task runThing {
    doLast {
        def dep = project.dependencies.create('org.junit:junit-bom:5.10.1')
        def detached = project.configurations.detachedConfiguration(dep)

        def view = detached.getIncoming().artifactView(viewConfiguration -> {
            viewConfiguration.withVariantReselection()
            viewConfiguration.attributes(attributeContainer -> {
                attributeContainer.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, "platform-metadata"));
            });
        });
        view.files.each {
            println it
        }
    }
}
Minimal reproducing example
Weirdly, if I request the attribute on the configuration rather than the artifactView, it works. That seems like a bug.
v
Sounds like the configuration already needs to select one unambiguous variant even if the view is used and selects one. Also for me sounds like a bug. @Jendrik Johannes can you maybe enlighten us here. Besides that it does not make much sense to use an artifact view here if you create a dedicated configuration anyway. Artifact views are handy to do something with existing configurations, or if you need lenient behavior.
t
I need lenient behavior. I just have it set to non lenient to more easily show the bug. The error is slightly more useful then the resolved files just being empty for testing purposes.
👌 1
I get the wild feeling this and the other issue I had above are related (I actually found the other one trying to debug this one). Something is forcing that configuration to be forced to a specific variant. Also interesting, is that is the only dependency out of my list of like 100 dependencies that has the bug. All other ones just work.
j
This is not well documented, but the
artifactView
, by default, only operates on the variants that were selected before. It the uses attributes to decide for an artifact (format/type) (NOT to decide for a Variant). This is for triggering artifact transforms. There is a new feature in artifact views since a few versions:
withVariantReselection
If you turn that on for your view, I think you can also use the view to select different variants. But I have never actively used that and do not know how the details work there. But maybe you do not need this at all. See my answer in the other thread.
v
Well, he has
withVariantReselection
in the upper example. Shouldn't it then work?
t
Yup, I'm doing variant reselection. And at least for this example, I tried a custom usage attribute too. I tried a whole lot of things, nothing worked. The attributes on the view were just completely ignored no matter what.
v
Und suggests you open an issue, then an expert can have a look and either fix it or tell where we are wrong
t
j
Sorry, I missed that you are already using "withVariantReselection". The issue may be that the initial matching fails in this case. I assume that a limitation of the mechanism (currently) is that the first resolution needs to succeed so that Gradle even gets to the point where it re-evaluates the result for an artifact view based on that result. (just guessing here)
v
Well, for "lenient" it works iirc. The "viewed" configuration cannot resolve while a lenient artifact view can. We'll see what the Gradle folks say to it. Probably really just a current limitation.