Declare compatible Gradle version(s) in plugin bundle and on the plugin portal

I’m developer of a Gradle plugin and currently upgrading to Gradle 7. The upgrade itself works without problems.

Now I tried the upgraded plugin in a build script using Gradle Wrapper 6.8.3, which fails with some strange errors (something with Gradle’s WorkAction/Worker api, the plugin uses internally). Upgrading the build to Gradle Wrapper 7.0 fixes the issue.

Maybe this is a bug - though I ask myself how I would communicate to the users of the plugin that they might have to upgrade to Gradle 7 when upgrading the plugin. I could even publish different releases of the same plugin, for each supported Gradle version.

What if I could declare the supported Gradle versions in the pluginBundle config when publishing the plugin so that Gradle could the select the “best” matching plugin variant? Would that be possible?

Sorry for the cross-post in GitHub - I discovered too late that the plugin portal has a dedicated GitHub repo. Should i close either the forum post or the GitHhub issue?

What you are after / suggesting is this which was introduced with Gradle 7: Implementing Gradle plugins
So starting with Gradle 7 you can do exactly that, provide different variants for different Gradle versions.
For older Gradle versions you need the classical “if version then use class X otherwise use class Y” or “if class A is available then use class X otherwise use class Y” tactics.

Thanks @Vampire for the quick feedback - seems like I didn’t read the release notes carefully enough :see_no_evil:
I’ll try to go that route.

I’ve tried the approach based on the example from the docs. The published artifacts contain the feature along with the desired attributes. See the snippet from the module.json:

    {
      "name": "gradle7RuntimeElements",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.jvm.version": 8,
        "org.gradle.libraryelements": "jar",
        "org.gradle.plugin.api-version": "7.0",
        "org.gradle.usage": "java-runtime"
      },
      "files": [
        {
          "name": "plugin-2021-04-15T19-59-54-gradle7.jar",
          "url": "plugin-2021-04-15T19-59-54-gradle7.jar",
          "size": 2928,
          "sha512": "417c008a60fbe2898e7a136d8229b4b533188e683e0eb854d72acb848198263da63e1e12008237a720ed96652bf19adb0676fb4845ab7c3e969ca8d1481acbc0",
          "sha256": "2f1feacdcf37d55861b0b39c73759e5adc5a1c6b9a76d0ea4599e9c5365d8b65",
          "sha1": "f82d41b02efbe915a92659c4dd63ac1fee8f173f",
          "md5": "bcc92999f780a625e515538cd73e4ad3"
        }
      ],
      "capabilities": [
        {
          "group": "de.gesellix",
          "name": "plugin",
          "version": "2021-04-15T19-59-54"
        }
      ]
    }

On the consumer side with Gradle 7 it doesn’t select that variant, though. I expected Gradle 7 to implicitly match on the "org.gradle.plugin.api-version": "7.0" attribute, but it selects the non-gradle7 default variant.

Can you tell how I can see which variant Gradle selected, and if I have to configure anything on the consumer side?

On the consumer side it should not be necessary to configure anything, that’s the point.
Unfortunately I’m not sure how to check that with build script dependencies.
With normal dependencies, dependencyInsight task would give you the information about which varinat was selected and which attributes were only chosen a compatible version instead of the requested version.
And I didn’t try this functionality yet, I just have read about it.
Do you maybe have an MCVE?

How do the other variants in the module file look like?

I’ll try to reduce the example to a MCVE.

The complete module.json is this one:

{
  "formatVersion": "1.1",
  "component": {
    "group": "de.gesellix",
    "module": "plugin",
    "version": "2021-04-15T20-09-49",
    "attributes": {
      "org.gradle.status": "release"
    }
  },
  "createdBy": {
    "gradle": {
      "version": "7.0"
    }
  },
  "variants": [
    {
      "name": "apiElements",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.jvm.version": 8,
        "org.gradle.libraryelements": "jar",
        "org.gradle.usage": "java-api"
      },
      "dependencies": [
        {
          "group": "de.gesellix",
          "module": "docker-client",
          "version": {
            "requires": "2021-04-10T14-34-47"
          }
        },
        {
          "group": "de.gesellix",
          "module": "gradle-docker-plugin-tasks",
          "version": {
            "requires": "2021-04-15T20-09-49"
          }
        }
      ],
      "files": [
        {
          "name": "plugin-2021-04-15T20-09-49.jar",
          "url": "plugin-2021-04-15T20-09-49.jar",
          "size": 171609,
          "sha512": "c38cf905fbcec5dd889d41111f443953be117acf9ee09c9a68d3ade8f52670ff0f6cd59612cbc62b266e744fe1da1ef19abf0613d9609fef35e417d070821e95",
          "sha256": "45e11e4312afaba40636d2589d8756b768e5bb036f3f6060d8d8c4b62830d179",
          "sha1": "fc998ade7e0f922b440c867fe68abc7f68fdb6bc",
          "md5": "9945585d862b06ccf6a78a77a2ada279"
        }
      ]
    },
    {
      "name": "runtimeElements",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.jvm.version": 8,
        "org.gradle.libraryelements": "jar",
        "org.gradle.usage": "java-runtime"
      },
      "dependencies": [
        {
          "group": "de.gesellix",
          "module": "docker-client",
          "version": {
            "requires": "2021-04-10T14-34-47"
          }
        },
        {
          "group": "de.gesellix",
          "module": "gradle-docker-plugin-tasks",
          "version": {
            "requires": "2021-04-15T20-09-49"
          }
        }
      ],
      "files": [
        {
          "name": "plugin-2021-04-15T20-09-49.jar",
          "url": "plugin-2021-04-15T20-09-49.jar",
          "size": 171609,
          "sha512": "c38cf905fbcec5dd889d41111f443953be117acf9ee09c9a68d3ade8f52670ff0f6cd59612cbc62b266e744fe1da1ef19abf0613d9609fef35e417d070821e95",
          "sha256": "45e11e4312afaba40636d2589d8756b768e5bb036f3f6060d8d8c4b62830d179",
          "sha1": "fc998ade7e0f922b440c867fe68abc7f68fdb6bc",
          "md5": "9945585d862b06ccf6a78a77a2ada279"
        }
      ]
    },
    {
      "name": "gradle7ApiElements",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.jvm.version": 8,
        "org.gradle.libraryelements": "jar",
        "org.gradle.plugin.api-version": "7.0",
        "org.gradle.usage": "java-api"
      },
      "files": [
        {
          "name": "plugin-2021-04-15T20-09-49-gradle7.jar",
          "url": "plugin-2021-04-15T20-09-49-gradle7.jar",
          "size": 2928,
          "sha512": "d1179d5dca9893f81b3d020d2c9fea15c0f509077d1a5faf0efd91c78ba2d47062099bc0fb20e523ddcd64184b64fad7ca05f9fffe4dca602dc3bbba563d59fd",
          "sha256": "9e56e7854ed886ac6ff2f88e946f99418dbf5e3bc7bb754f1d3c3ad0d744adbd",
          "sha1": "3b164afcc7e0a15c8159ea2f2361ddd7cf5affb8",
          "md5": "6e87d2c6bb50087eea09174fea839f49"
        }
      ],
      "capabilities": [
        {
          "group": "de.gesellix",
          "name": "plugin",
          "version": "2021-04-15T20-09-49"
        }
      ]
    },
    {
      "name": "gradle7RuntimeElements",
      "attributes": {
        "org.gradle.category": "library",
        "org.gradle.dependency.bundling": "external",
        "org.gradle.jvm.version": 8,
        "org.gradle.libraryelements": "jar",
        "org.gradle.plugin.api-version": "7.0",
        "org.gradle.usage": "java-runtime"
      },
      "files": [
        {
          "name": "plugin-2021-04-15T20-09-49-gradle7.jar",
          "url": "plugin-2021-04-15T20-09-49-gradle7.jar",
          "size": 2928,
          "sha512": "d1179d5dca9893f81b3d020d2c9fea15c0f509077d1a5faf0efd91c78ba2d47062099bc0fb20e523ddcd64184b64fad7ca05f9fffe4dca602dc3bbba563d59fd",
          "sha256": "9e56e7854ed886ac6ff2f88e946f99418dbf5e3bc7bb754f1d3c3ad0d744adbd",
          "sha1": "3b164afcc7e0a15c8159ea2f2361ddd7cf5affb8",
          "md5": "6e87d2c6bb50087eea09174fea839f49"
        }
      ],
      "capabilities": [
        {
          "group": "de.gesellix",
          "name": "plugin",
          "version": "2021-04-15T20-09-49"
        }
      ]
    }
  ]
}

A small update: a MCVE works fine, so I’ll try to find the bug in my code.

1 Like