I have a "custom variant" in my published java library, called "releaseElements". I'm really confused between gradle "configurations", "variants" and "classifiers" to be honest, it's an utter mess, but here is how the modules file looks like, as published in the package repo:
{
"formatVersion": "1.1",
"component": {
"group": "com.grp",
"module": "lib-java",
"version": "1.5.1-temp-SNAPSHOT",
"attributes": {
"org.gradle.status": "integration"
}
},
"createdBy": {
"gradle": {
"version": "6.8.3",
"buildId": "aykb2f3zszg23fxd3kwybg7mfe"
}
},
"variants": [
{
"name": "apiElements",
....................
....................
....................
{
"name": "releaseElements",
"attributes": {
"org.gradle.category": "library",
"org.gradle.dependency.bundling": "embedded",
"org.gradle.jvm.version": 8,
"org.gradle.libraryelements": "jar",
"org.gradle.usage": "java-runtime"
},
"files": [
{
"name": "lib-java-1.5.1-temp-SNAPSHOT-release.jar",
"url": "lib-java-1.5.1-temp-SNAPSHOT-release.jar",
"size": 4512712,
My consumer app tries to explicitly use that variant, "release", using:
implementation (group:"com.grp", name: "app-java", version: "1.5.1-temp-SNAPSHOT", configuration: "releaseElements")
I also tried "classifier" insteaed of configuration and using "release" instead of "releaseElements", no luck.
What I am expecting as per gradle docs is for variant-aware selection to be bypassed "whenever the consumer explicitly selects a target configuration" (that's literally the text of the docs). However the gradle build of the consumer keeps giving me either this:
> Could not resolve all dependencies for configuration ':runtimeClasspath'.
> Could not resolve com.grp:lib-java:1.5.1-temp-SNAPSHOT.
Required by:
project :
> Project : declares a dependency from configuration 'implementation' to configuration 'releaseElements' which is not declared in the descriptor for com.grp:lib-java:1.5.1-temp-SNAPSHOT:20210322.000411-5.
or this:
> The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally. However we cannot choose between the following variants of com.grp:lib-java:1.5.1-temp-SNAPSHOT:2021032
2.000411-5:
- releaseElements
- runtimeElements
All of them match the consumer attributes:
- Variant 'releaseElements' capability com.grp:lib-java:1.5.1-temp-SNAPSHOT declares a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally:
- Unmatched attribute:
- Provides integration status but the consumer didn't ask for it
- Variant 'runtimeElements' capability com.grp:lib-java:1.5.1-temp-SNAPSHOT declares a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally:
- Unmatched attribute:
- Provides integration status but the consumer didn't ask for it
To note, I also tried to use different dependency.bundling values - external, embedded and shadowed. No success.
Question: Why does Gradle not bypass variant-aware resolution when given a specific variant in the consumer??
The overall goal is to add a variant of my library which is fat and obfuscated, and which I can publish together with the rest of the artifacts.
Edit: more info after more experimentation
I now believe I should be using this in my consumer:
implementation (group:"com.grp", name: "app-java", version: "1.5.1-temp-SNAPSHOT", configuration: "releaseElements")
i.e. configuration, not classifier, although classifier apparently gets me closer to what I want. the reason is that the configuration is the one containing deps (in Gradle metadata different configurations may have different deps), while specifying the classifier simply nominates a jar file stripped of metadata information, which is a last-resort thing as per Advanced dependency configuration here.
The build of the consumer project continues to fail with the ... which is not declared in the descriptor for com.grp:lib ... error above, which is an error, because the confikguration is in the module file.