Consuming `java-platform` in non-standard configurations

Hi, I’m wondering what the recommended way is to use a dependency platform, as created by the java-platform gradle plugin, in configurations other than implementation or api. If I try to do so, I get an error like the following:

Cannot choose between the following variants of project :dependencies:
  - apiElements
  - runtimeElements

This, for example, makes it so I have to explicitly set versions of annotation processors that I would like to have resolved by the platform. I’m able to work around this by explicitly setting a usage attribute on the configuration, but this is a bit annoying when I want to apply the platform across all configurations. Any suggestions?

1 Like

I experienced the same issue. It is because java-platform plugin creates two configurations (apiElement, runtimeElements) and they differ by having different value in org.gradle.usage attribute. I found some documentation here: https://docs.gradle.org/current/userguide/dependency_management_attribute_based_matching.html#sec:abm_configuration_attributes
You could help the gradle to choose the right configuration by setting required attribute for the configuration you are using the dependency platform.

configurations {
    annotationProcessor {
        attributes {
            attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, Usage.JAVA_API))
        }
    }
}

This solves the problem for some cases, but similar issues may appear if some other module is using module with such modified configurations (there might be two configurations that have the same values in org.gradle.usage and Gradle will not be able to choose the right variant)
What did always work in my case is first publish the platform to maven repository and than consume it as platform from external dependency.

annotationProcessor platform("com.mygroup:my-platform-module:$platformVersion")

Would be nice if someone from Gradle will explain details how it is meant to be used. Maybe the better solution should set AttributeDisambiguationRule

Thanks for the reply. This is the workaround that I came up with, but it feels like I’m doing The Wrong Thing™ to do that to any ad-hoc configurations that want to consume the platform.