Java toolchain not working with buildSrc module

I am trying to use java toolchain to build my project with a specific version of java. I am using toolchain to download and use java 14 for my project. My project build fails when it has buildSrc module in it. I am getting following error

* What went wrong:
Could not determine the dependencies of null.
> Could not resolve all task dependencies for configuration ':buildSrc:buildScriptClasspath'.
   > Could not resolve project :buildSrc.
     Required by:
         project :buildSrc
      > No matching variant of project :buildSrc was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value
 '8.0' but:
          - Variant 'apiElements' capability :buildSrc:unspecified declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 14 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
          - Variant 'mainSourceElements' capability :buildSrc:unspecified declares a component, and its dependencies declared externally:
              - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
                  - Doesn't say anything about its usage (required runtime)
          - Variant 'runtimeElements' capability :buildSrc:unspecified declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 14 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
          - Variant 'testResultsElementsForTest' capability :buildSrc:unspecified:
              - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
                  - Doesn't say anything about its usage (required runtime)

Can someone please look into the issue and help me here. Thank you!

You are building buildSrc with and for Java 14, but run Gradle with Java 8.
buildSrc builds the build logic used by Gradle in the main build.
So unless you want to force anyone executing that build to use at least Java 14 to run Gradle, you cannot build buildSrc with Java 14.

1 Like

Thank you so much for the response :raised_hands:.
My team is currently working on multiple projects few need Java 8 whereas a few need Java 14. Every time we switch working on the project we have to manually switch the environment variables to the corresponding project-specific java version.
My idea was to use Gradle 8 to automatically detect the project Java version, and download and use that version of Java for future builds of the project. I was trying to use the Java toolchain with the understanding that it will download the project-specific Java version on the fly and use it for project build but with the above response it looks like this approach will not work if the project has the buildSrc module.
Is there any other recommended solution that can save from switching the JAVA environment variables every time we switch working on different projects?

You have to mentally separate running Gradle and building your production code. And in the same vein building your production code and building your build logic.

With a toolchain you define what version some code is built with and for. So if you configure the toolchain for your production code to use 14, it is done with 14. And if you configure your build logic to build with 14, it is built with 14, but then can also only be used with 14, that is Gradle has to run with 14.

Configure the toolchain for your production code to 14, that’s fine. But for the build logic, either configure no toolchain as then the version you run Gradle with is used, or configure a Java 8 toolchain so that not someone using Java 14 to run the build accidentally introduces a change Java 8 people then cannot use.

1 Like

I’m trying to decipher this response and I think my question boils down to this: Can the version of java that Gradle is running on be less than the version defined in the toolchain?
In my case, my build server is setup with java8, but I want to start using java17. I was hoping that adopting toolchain would allow me to do that, but I am finding in practice that it does not work.
At the same time, I am also finding that configuring Gradle to use java17 and then using a toolchain value of 8 also does not work.

Can the version of java that Gradle is running on be less than the version defined in the toolchain?

Of course, that’s one of the major intentions.
To decouple the Java version running Gradle with from the Java version used to build and run your code.
So when using toolchains you can use any Java version that is compatible to run your Gradle version and use any toolchain that is compatible for usage with your Gradle version freely combinable.
See Compatibility Matrix for the compatibility details.

In my case, my build server is setup with java8, but I want to start using java17. I was hoping that adopting toolchain would allow me to do that,

Sure, as long as you use at least Gradle 7.3 which is the first version to officially support Java 17 for both, running Gradle and usage as toolchain.

but I am finding in practice that it does not work.
At the same time, I am also finding that configuring Gradle to use java17 and then using a toolchain value of 8 also does not work.

Then you seem to do something wrong.
But it most probably has nothing to do with this thread here, so probably better open a new one. :wink: