SDKMAN 支持多版本 java
2023-05-31
安装和使用比较简单, 看官方文档就差不多了, 直接复制了官方文档进行存档.
java本来还有 jenv 可以使用, 但是sdkman支持的语言类型比较多, java, maven, graddle, scala 等都支持配置, 同样功能下大而全的工具还挺有优势. 不过不支持 python, python 还是习惯使用pyenv; node 生态也有nvm可以用; ruby 生态则有rvm; 看起来每种语言都有相同的需求, 估计实现技术方案也差不多, 但都没有去研究清楚.
日常操作
比较常用的命令
sdk env
sdk current java
# 安装 java 1.8版本和maven旧版本
sdk install java 8.0.302-open
sdk install maven 3.6.3
# 设置默认值, 其实就是设置current软连接到当前folder
sdk default java 8.0.302-open
操作记录
skd list java
sdk install java 8.0.302-open
sdk install maven 3.6.3
➜ git:(release-branch) ✗ sdk current java
Using java version 8.0.302-open
➜ git:(release-branch) ✗ sdk current maven
Using maven version 3.8.6
➜ git:(release-branch) ✗ mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /root/.sdkman/candidates/maven/current
Java version: 1.8.0_302, vendor: Oracle Corporation, runtime: /root/.sdkman/candidates/java/8.0.302-open/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.14.105-1-tlinux3-0020", arch: "amd64", family: "unix"
➜ git:(release-branch) ✗ java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)
[root@VM-152-104-centos ~/projects/package]# sdk default java 8.0.302-open
We periodically need to update the local cache. Please run:
$ sdk update
Default java version set to 8.0.302-open
ls -alh ~/.sdkman/candidates/java/
total 24K
drwxr-xr-x 6 root root 4.0K Jul 17 14:57 .
drwxr-xr-x 4 root root 4.0K Jun 15 15:39 ..
drwxrwxr-x 10 root root 4.0K Jul 15 2021 11.0.12-open
drwxr-xr-x 10 root root 4.0K Apr 21 16:42 17.0.7-kona
drwxr-xr-x 8 root root 4.0K Jun 15 15:13 22.ea.1-open
drwxrwxr-x 9 root root 4.0K Jul 16 2021 8.0.302-open
lrwxrwxrwx 1 root root 12 Jul 17 14:57 current -> 8.0.302-open
https://github.com/sdkman/sdkman-cli
Install
Simply open a new terminal and enter:
$ curl -s "https://get.sdkman.io" | bash
Follow the instructions on-screen to complete installation. Next, open a new terminal or enter:
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
Lastly, run the following code snippet to ensure that installation succeeded:
$ sdk version
If all went well, the version should be displayed. Something like:
sdkman 5.18.1
Usage
Installing an SDK
Latest Stable
Install the latest stable version of your SDK of choice (say, Java JDK) by running the following command:
$ sdk install java
You will see something like the following output:
Downloading: java 17.0.7-tem
In progress...
######################################################################## 100.0%
Installing: java 17.0.7-tem
Done installing!
Now you will be prompted if you want this version to be set as default.
Do you want java 17.0.7-tem to be set as default? (Y/n):
Answering yes (or hitting enter) will ensure that all subsequent shells opened will have this version of the SDK in use by default.
Setting java 17.0.7-tem as default.
Specific Version
Need a specific version of an SDK? Simply qualify the version you require:
$ sdk install scala 3.2.2
All subsequent steps same as above.
Install Local Version(s)
Using a snapshot version? Already have a local installation? Setup a local version by specifying the path to the local installation:
$ sdk install groovy 3.0.0-SNAPSHOT /path/to/groovy-3.0.0-SNAPSHOT
$ sdk install java 17-zulu /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
Note that the local version name (3.0.0-SNAPSHOT and 17-zulu in the examples above) must be a unique name which is not already in the list of available version names.
Remove Version
Remove an installed version.
$ sdk uninstall scala 3.2.2
Note that removing a local version will not remove the local installation.
List Candidates
To get a listing of available Candidates:
$ sdk list
This will render a searchable alphabetic list with name, current stable default version, website URL, description and easy install command for each Candidate. The output is piped to less so standard keyboard shortcuts may be used with q to exit.
================================================================================
Available Candidates
================================================================================
q-quit /-search down
j-down ?-search up
k-up h-help
--------------------------------------------------------------------------------
...
--------------------------------------------------------------------------------
Java (17.0.7-tem) https://projects.eclipse.org/projects/adoptium.temurin/
Java Platform, Standard Edition (or Java SE) is a widely used platform for
development and deployment of portable code for desktop and server environments.
Java SE uses the object-oriented Java programming language. It is part of the
Java software-platform family. Java SE defines a wide range of general-purpose
APIs – such as Java APIs for the Java Class Library – and also includes the Java
Language Specification and the Java Virtual Machine Specification.
$ sdk install java
--------------------------------------------------------------------------------
...
List Versions
To get a listing of Candidate Versions:
$ sdk list groovy
This will result in a list view showing the available, local, installed and current versions of the SDK.
================================================================================
Available Groovy Versions
================================================================================
> * 2.4.4 2.3.1 2.0.8 1.8.3
2.4.3 2.3.0 2.0.7 1.8.2
2.4.2 2.2.2 2.0.6 1.8.1
2.4.1 2.2.1 2.0.5 1.8.0
2.4.0 2.2.0 2.0.4 1.7.9
2.3.9 2.1.9 2.0.3 1.7.8
2.3.8 2.1.8 2.0.2 1.7.7
2.3.7 2.1.7 2.0.1 1.7.6
2.3.6 2.1.6 2.0.0 1.7.5
2.3.5 2.1.5 1.8.9 1.7.4
2.3.4 2.1.4 1.8.8 1.7.3
2.3.3 2.1.3 1.8.7 1.7.2
2.3.2 2.1.2 1.8.6 1.7.11
2.3.11 2.1.1 1.8.5 1.7.10
2.3.10 2.1.0 1.8.4 1.7.1
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
Use Version
Choose to use a given version in the current terminal:
$ sdk use scala 3.2.2
It is important to realise that this will switch the candidate version for the current shell only. To make this change permanent, use the default command instead.
Default Version
Chose to make a given version the default:
$ sdk default scala 3.2.2
This will ensure that all subsequent shells will start with version 3.2.2 in use.
Current Version(s)
To see what is currently in use for a Candidate:
$ sdk current java
Using java version 17.0.7-tem
To see what is currently in use for all Candidates:
$ sdk current
Using:
groovy: 4.0.12
java: 17.0.7-tem
scala: 3.2.2
Env Command
Want to switch to a specific JDK or SDK every time you visit a project? This can be achieved through an .sdkmanrc file in the base directory of your project. This file can be generated automatically by issuing the following command:
$ sdk env init
A config file with the following content has now been created in the current directory:
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=17.0.7-tem
The file is pre-populated with the current JDK version in use, but can contain as many key-value pairs of supported SDKs as needed. To switch to the configuration present in your .sdkmanrc file, simply issue the following command:
sdk env
You should see output that looks something like this:
Using java version 17.0.7-tem in this shell.
Your path has now also been updated to use any of these SDKs in your current shell. When leaving a project, you may want to reset the SDKs to their default version. This can be achieved by entering:
$ sdk env clear
Restored java version to 17.0.7-tem (default)
After checking out a new project, you may be missing some SDKs specified in the project's .sdkmanrc file. To install these missing SDKs, just type:
$ sdk env install
Downloading: java 17.0.7-tem
In progress...
######################################################################## 100,0%
Repackaging Java 17.0.7-tem...
Done repackaging...
Installing: java 17.0.7-tem
Done installing!
Do you want to switch SDK versions automatically when you cd into a directory? This can be done by setting the sdkman_auto_env=true in the SDKMAN configuration. Note that this will also reset any project-specific SDKs to their default version when leaving the directory.
Upgrade Version(s)
To see what is currently out of date for a Candidate on your system:
$ sdk upgrade springboot
Upgrade:
springboot (1.2.4.RELEASE, 1.2.3.RELEASE < 3.1.0)
To see what is outdated for all Candidates:
$ sdk upgrade
Upgrade:
gradle (2.3, 1.11, 2.4, 2.5 < 8.1.1)
grails (2.5.1 < 5.3.2)
springboot (1.2.4.RELEASE, 1.2.3.RELEASE < 3.1.0
SDKMAN! Version
Display the current script and native versions of SDKMAN!:
$ sdk version
SDKMAN!
script: 5.7.0
native: 0.1.3
Offline Mode
Initially called Aeroplane Mode, this allows SDKMAN! to function when working offline. It has a parameter that can be passed toenable or disable the offline mode.
$ sdk offline enable
Forced offline mode enabled.
$ sdk offline disable
Online mode re-enabled!
When operating in offline mode, most commands will still work even though they will operate in a scaled down capacity. An example is the list command, which will only display the currently installed and active version(s):
$ sdk list groovy
------------------------------------------------------------
Offline Mode: only showing installed groovy versions
------------------------------------------------------------
> 2.4.4
* 2.4.3
------------------------------------------------------------
* - installed
> - currently in use
------------------------------------------------------------
The offline mode will also be disabled/enabled automatically when the internet becomes available/unavailable. Naturally, commands that require internet connectivity will not function but give a warning.
Self-Update
Installs a new version of SDKMAN! if available.
$ sdk selfupdate
If no new version is available an appropriate message will be displayed. Re-installation may be forced by passing the force parameter to the command:
$ sdk selfupdate force
Automatic daily checks for new versions of SDKMAN! will also be performed on the behalf of the user.
Update
Periodically SDKMAN! requires a refresh to become aware of new (or removed) candidates. When candidate metadata has potentially grown stale, a warning is displayed with instruction on how to update. By simply running the following command, the candidate cache will be refreshed and new candidates will become available for installation:
WARNING: SDKMAN is out-of-date and requires an update.
$ sdk update
Adding new candidates(s): kotlin
Flush
It should rarely be necessary to flush SDKMAN!'s local state. The flush command helps with this, so you don't need to delete any directories. Manually deleting directories like the .sdkman/tmp directory will break SDKMAN! Always use the flush command instead!
$ sdk flush
Home
When using SDKMAN in scripts, it is often useful to get the absolute path of where an SDK resides (similar to how the java_home command works on macOS). For this we have the home command.
$ sdk home java 17.0.7-tem
/home/myuser/.sdkman/candidates/java/17.0.7-tem
Help
You can get basic help by running the following command:
$ sdk help
This should render a useful top-level help page. You can add qualifier to this command to get help about a specific sub-command.
$ sdk help install
Configuration
Configuration can be found in the ~/.sdkman/etc/config file. To edit the configuration, the sdk config command may be issued to edit this file in the system editor. The following configurations are available:
# make sdkman non-interactive, preferred for CI environments
sdkman_auto_answer=true|false
# check for newer versions and prompt for update
sdkman_selfupdate_feature=true|false
# disables SSL certificate verification
# https://github.com/sdkman/sdkman-cli/issues/327
# HERE BE DRAGONS....
sdkman_insecure_ssl=true|false
# configure curl timeouts
sdkman_curl_connect_timeout=5
sdkman_curl_continue=true
sdkman_curl_max_time=10
# subscribe to the beta channel
sdkman_beta_channel=true|false
# enable verbose debugging
sdkman_debug_mode=true|false
# enable colour mode
sdkman_colour_enable=true|false
# enable automatic env
sdkman_auto_env=true|false
# enable rosetta2 compatibility for apple silicon
sdkman_rosetta2_compatible=true|false
# enable bash or zsh auto-completion
sdkman_auto_complete=true|false