Usually, I was working with different projects using different SDK versions that required switching between versions. If you still did not face SDKMan tool welcome to this post.
SDKMan - is a tool that allows to manage of different versions of SDK, Maven, Gradle etc.
The tools was developed inspired by the very useful tool called RVM and rbevn used by Ruby community.
First of all, we should install SDKMan locally.
If you are using Windows, you have to use SDKMan through WSL2. If you don’t have it follow this guide.
Open the Terminal and execute installation command:
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh"
Done! Now, let’s try to execute the following command:
> sdk version SDKMAN 5.15.0
Installing a JDK
Let’s imagine that I want to install Java, for that, I have to take a look at the list of available versions:
> sdk list java ============================================================================== Available Java Versions for Linux 64bit ============================================================================== Vendor | Use | Version | Dist | Status | Identifier ------------------------------------------------------------------------------ Corretto | | 18.0.1 | amzn | | 18.0.1-amzn | | 188.8.131.52.1 | amzn | | 184.108.40.206.1-amzn | | 220.127.116.11.1 | amzn | | 18.104.22.168.1-amzn | | 22.214.171.124.1 | amzn | | 126.96.36.199.1-amzn | | 188.8.131.52.1 | amzn | | 184.108.40.206.1-amzn | | 8.332.08.1 | amzn | | 8.332.08.1-amzn Dragonwell | | 220.127.116.11 | albba | | 18.104.22.168-albba | | 8.8.9 | albba | | 8.8.9-albba GraalVM | | 22.1.0.r17 | grl | | 22.1.0.r17-grl | | 22.1.0.r11 | grl | | 22.1.0.r11-grl
Let’s choose the version which I needed in Identifier column and execute the installation command:
sdk install java 22.214.171.124.1-amzn
By default, installed SKDs will be located in
> ls -al ~/.sdkman/candidates/java/ drwxr-xr-x 10 axbarchuk axbarchuk 4096 Jan 8 15:14 17.0.2-zulu drwxr-xr-x 9 axbarchuk axbarchuk 4096 Apr 16 01:01 126.96.36.199.1-amzn lrwxrwxrwx 1 axbarchuk axbarchuk 15 Jun 15 13:05 current -> 188.8.131.52.1-amzn
Switching between SDK versions
As you can see above we can install any count of versions which we need.
Executing the next command we could easily switch the current version to needed for a particular project, for instance.
sdk use java 11.0.9.j9-adpt
If you want to set a specific version as default, use this command:
sdk default java 17.0.2-zulu
Managing SDKs in projects
Let’s imagine that we are working with a few projects, for instance, we are developing microservices where some of them use java 11 and some of them already moved to Java 17.
How to understand for what service which version of SDK is used.
For that purposes SDKMan provides a file called as
.sdkmanrc were we could describe the versions of SDKs which our project are used.
First of all, you have to execute SDKMan init:
sdk env init
As a result will be created
.sdkmanrc file, next you have to describe the versions of SDKs which your project will use:
If your project already have this file, you just need to use it by executing following command:
> sdk env Using java version 17.0.2-zulu in this shell.
This command automatically sees the file with that name and use it.
Except for SDKs you could install needed tools such as:
- Spring Boot CLI
sdk install maven sdk install gradle sdk install springboot
Also, if you notice above I don’t use the versions of installing tools, in that case, will be using the latest stable versions of that package. The same is applicable to SDKs.
SDKMAN! is a great tool to manage the versions of SDKs or our favorite tools. It allows managing SDK without headache. If you need more information visit the official site.