Last week a fried of mine faced a very special problem with a build in CI/CD pipeline.
In its Dockerfile, he wasn’t setting a specific JDK version.
This reponsibility was so, automatically, transfered to the image eclipse-temurin:21.
He was using version 21.0.9 then image updated to 21.0.10 and bang… build started to fail.
A some time I wanted to write about similar thing:
How could we keep same JDK version when working in a team?
Is very easy to us, while working in a team, have different versions, e.g.:
- In our local
JAVA_HOME - In our IDE
- In CI/CD
- And so on
And indepently which JDK version manager you’re using:
- manually (i was this guy a long time a go)
- sdkman (today is my favorite)
- jenv
- etc
It’s very simple:
- We create a file in root project that containing a version, e.g.
.java-version:1
21.0.9 - In Gradle(build.gradle.kts - yes I prefer kotlin over groove), set the toolchain to use that
majorversion:1
2
3
4
5
6
7
8val projectJdkFullVersion = file(".java-version").readText().trim() val projectJdkMajorVersion = projectJdkFullVersion.substringBefore('.').toInt() java { toolchain { languageVersion.set(JavaLanguageVersion.of(projectJdkMajorVersion)) } } - We create a
taskthat will check the version:1
2
3
4
5
6
7
8val checkJavaVersion by tasks.registering(Task::class) { doLast { val jdkRuntimeVersion = System.getProperty("java.version") if (!jdkRuntimeVersion.startsWith(projectJdkFullVersion)) { throw GradleException("Requires JDK $projectJdkFullVersion, but found $jdkRuntimeVersion") } } } - Bind the task validation compile task in Gradle Lifecycle:
1
2
3tasks.compileJava { dependsOn(checkJavaVersion) }
In case the environment isn’t using the expected version, it’ll fail:
1 | |
That’s it.
Everywhere your project is on, it’ll always be running on same version and be consistent.
That care with your versions.
When updating, even in case its a “low importance”.
Like the example, it was just “21.0.9 to 21.10”, it can result in fail on your end.
Take a moment to read the release notes.
Remember: if you don’t assume the responsibility about your version, your code… someone will do it.
xoff.