Skip to content

Conversation

marko-radosavljevic
Copy link
Contributor

Working on multiple projects can require interacting with multiple versions of the Java language. Even within a single project different parts of the codebase may be fixed to a particular language level due to backward compatibility requirements. This means different versions of the same tools (a toolchain) must be installed and managed on each machine that builds the project.

A Java toolchain is a set of tools to build and run Java projects, which is usually provided by the environment via local JRE or JDK installations. Compile tasks may use javac as their compiler, test and exec tasks may use the java command while javadoc will be used to generate documentation.

By default, Gradle uses the same Java toolchain for running Gradle itself and building JVM projects. However, this may only sometimes be desirable. Building projects with different Java versions on different developer machines and CI servers may lead to unexpected issues. Additionally, you may want to build a project using a Java version that is not supported for running Gradle.

In order to improve reproducibility of the builds and make build requirements clearer, Gradle allows configuring toolchains on both project and task levels.

Executing the build (e.g. using gradle check) will now handle several things for you and others running your build:

  1. Gradle configures all compile, test and javadoc tasks to use the defined toolchain.
  2. Gradle detects locally installed toolchains.
  3. Gradle chooses a toolchain matching the requirements (any Java 17 toolchain for the example above).
  4. If no matching toolchain is found, Gradle can automatically download a matching one based on the configured toolchain download repositories.

Almost every newcomer had issues with setting up our project, mainly because of java version mismatches, gradle and java compiler configurations. This is an attempt to eliminate these barariers, and streamline the experience for newcomers. With this PR, you don't even need to have java installed, it will do it for you, and it will download the correct matching version.

Since this is not a new feature anymore, I hope IDEs and other developer tooling started to respect and use these preferences from gradle, because last time I checked, intelliJ didn't support is fully, witch means you would still need to configure your IDE environment if you are launching our bot from there.

Old way of pegging the version with sourceCompatibility = JavaVersion.VERSION_21 is considered deprecated by gradle. Also, it will only download correct jdk if you don't already have it. It's pereference when autoprovisioning is adoptium releases. We can force specific vendor if we want, for the whole project.

* Autodetection of installed toolchains
* Autoprovisioning if toolchain/jdk is not found
@marko-radosavljevic marko-radosavljevic requested review from a team as code owners December 11, 2023 01:13
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@marko-radosavljevic
Copy link
Contributor Author

It would be nice if someone tested this locally, in different environments and setups. Just to make sure it's reliable for everyone, before merging.

If not, we can merge it, it should be pretty safe PR.

Copy link
Member

@Zabuzard Zabuzard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i cant really review this, no idea if it works or not. feel free to try it out

@marko-radosavljevic
Copy link
Contributor Author

Alright, let's try it out. It's already in production on all of our other projects.

You can see all detected JDKs, and see ones provisioned with Gradle with: gradle -q javaToolchains.

@marko-radosavljevic marko-radosavljevic merged commit 4daa26d into develop Dec 11, 2023
@marko-radosavljevic marko-radosavljevic deleted the feature/gradle-toolchain-integration branch December 11, 2023 12:02
@ankitsmt211 ankitsmt211 mentioned this pull request Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants