Guide to SDKMAN! – SDKMAN指南!

最后修改: 2020年 7月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

With the new release cycle of Java, developers might need to manage parallel versions and different builds of Software Development Kits (SDK) in their environment. So setting the PATH variable can become a real pain at times.

随着Java新的发布周期的到来,开发人员可能需要在他们的环境中管理并行的版本和不同的软件开发工具包(SDK)的构建。因此,设置PATH变量有时会成为一种真正的痛苦。

In this tutorial, we’ll see how SDKMAN! can help to manage the installation and selection of SDKs with ease.

在本教程中,我们将看到SDKMAN!如何帮助轻松地管理SDK的安装和选择。

2. What Is SDKMAN!?

2.什么是SDKMAN!?

SDKMAN! is a tool to manage parallel versions of multiple SDKs, which SDKMAN! call “candidates”.

SDKMAN!是一个管理多个SDK并行版本的工具,SDKMAN!称之为 “候选”

It provides a convenient Command-Line Interface (CLI) and API for listing, installing, switching, and removing candidates. Moreover, it takes care of setting environment variables for us.

它提供了一个方便的命令行界面(CLI)和API,用于列出、安装、切换和删除候选人。此外,它还负责为我们设置环境变量。

It also allows the developers to install JVM-based SDKs like Java, Groovy, Scala, Kotlin, and Ceylon. Maven, Gradle, SBT, Spring Boot, Vert.x, and many others are also supported. SDKMAN! is a free, lightweight, open-source utility written in Bash.

它还允许开发者安装基于JVM的SDK,如Java、Groovy、Scala、Kotlin和Ceylon。还支持Maven、Gradle、SBT、Spring Boot、Vert.x等许多其他工具。SDKMAN!是一个用Bash编写的免费、轻量级、开源的工具。

3. Install SDKMAN!

3.安装SDKMAN!

SDKMAN! is supported by all major operating systems, and it can easily be installed on all Unix-based systems. Moreover, it supports Bash and Zsh shells.

SDKMAN!支持所有主要的操作系统,它可以很容易地安装在所有基于Unix的系统上。此外,它还支持Bash和Zsh shells。

So let’s begin by installing it using the terminal:

因此,让我们先用终端安装它。

$ curl -s "https://get.sdkman.io" | bash

Then, follow the on-screen instructions to complete the installation.

然后,按照屏幕上的指示完成安装。

We might need the zip and unzip packages installed to complete the installation process.

我们可能需要安装zip和unzip包来完成安装过程。

Next, open a new terminal or run:

接下来,打开一个新的终端或运行。

$ source "$HOME/.sdkman/bin/sdkman-init.sh"

Lastly, run the following command to ensure the installation worked. If all went well, the version should be displayed:

最后,运行以下命令以确保安装成功。如果一切顺利,应该显示版本。

$ sdk version
SDKMAN 5.8.5+522

For more customization, please refer to the installation guide on the SDKMAN! website.

关于更多的定制,请参考SDKMAN!网站上的安装指南

To see all the available commands, use the help command:

要查看所有可用的命令,请使用help命令。

$ sdk help

4. List All SDK Candidates

4.列出所有SDK候选者

So, let’s start by listing all the available SDK candidates.

因此,让我们首先列出所有可用的SDK候选人。

$ sdk list

The list command shows all the available candidates, identified by a unique name, the description, the official website, and the installation command:

list命令显示所有可用的候选者,由一个独特的名称、描述、官方网站和安装命令来确定:

=====================================================
Available Candidates
=====================================================
q-quit                                  /-search down
j-down                                  ?-search up
k-up                                    h-help
-----------------------------------------------------
Java (11.0.7.hs-adpt)                https://zulu.org
...
                                   $ sdk install java
-----------------------------------------------------
Maven (3.6.3)                https://maven.apache.org
...
                                  $ sdk install maven
-----------------------------------------------------
Spring Boot (2.3.1.RELEASE)          http://spring.io
...
                             $ sdk install springboot
------------------------------------------------------
...

Therefore, we can use this identifier to install the default version of a candidate like Spring Boot (2.3.1.RELEASE) or Maven (3.6.3). The specified versions in this list represent the stable or LTS versions of each SDK.

因此,我们可以使用这个标识符来安装Spring Boot(2.3.1.RELEASE)或Maven(3.6.3)等候选版本的默认版本。该列表中的指定版本代表每个SDK的稳定版或LTS版。

5. Install and Manage Java Versions

安装和管理Java版本

5.1. Listing Versions

5.1.上市版本

To list the available versions of Java, use the list command. The result is a table of entries grouped by the vendor and sorted by version:

要列出Java的可用版本,请使用list命令。其结果是一个按供应商分组并按版本排序的条目表。

$ sdk list java
===================================================================
Available Java Versions
===================================================================
Vendor       | Use | Version | Dist    | Status | Identifier
-------------------------------------------------------------------
AdoptOpenJDK |     | 14.0.1  | adpt    |        | 14.0.1.j9-adpt
...
Amazon       |     | 11.0.8  | amzn    |        | 11.0.8-amzn
...
Azul Zulu    |     | 14.0.2  | zulu    |        | 14.0.2-zulu
...
BellSoft     |     | 14.0.2  | librca  |        | 14.0.2.fx-librca
...
GraalVM      |     | 20.1.0  | grl     |        | 20.1.0.r11-grl
...
Java.net     |     | 16.ea   | open    |        | 16.ea.6-open
...
SAP          |     | 14.0.2  | sapmchn |        | 14.0.2-sapmchn
...

We’ll need this command each time we want to check, switch, or manage the storage of candidates.

每次我们想检查、切换或管理候选人的存储时都需要这个命令。

5.2. Install a Java Version

5.2.安装一个Java版本

Let’s say we want to install the newest build of Java 14 from Azul Zulu. Therefore, we copy its identifier, which is the version from the table, and we add it as an argument in the install command:

比方说,我们想安装Azul Zulu公司最新的Java 14版本。因此,我们复制它的标识符,也就是表中的版本,并将其作为参数添加到install命令中。

$ sdk install java 14.0.2-zulu
Downloading: java 14.0.2-zulu
In progress...
########### 100.0%
Repackaging Java 14.0.2-zulu...
Done repackaging...
Installing: java 14.0.2-zulu
Done installing!
Setting java 14.0.2-zulu as default.

SDKMAN! will download and unzip this version into a directory on our computer.

SDKMAN!将下载并解压该版本到我们电脑的一个目录中。

Moreover, it will update environment variables so that we can use Java in the terminal immediately.

此外,它还会更新环境变量,这样我们就可以立即在终端使用Java。

We can verify the status and the usage of any version by using the list command. Consequently, the version 14.0.1 is now installed and in use:

我们可以通过使用list命令验证任何版本的状态和使用情况。因此,版本14.0.1 i现在已经安装并正在使用。

$ sdk list java
=================================================================
Available Java Versions
=================================================================
 Vendor    | Use | Version | Dist    | Status    | Identifier
-----------------------------------------------------------------
 ...
 Azul Zulu | >>> | 14.0.1  | adpt    | installed | 14.0.1.j9-adpt
 ...

In addition, it’s possible to install Java or any custom version from a computer with the same command but by specifying the path of binaries as an additional argument:

此外,还可以用同样的命令从计算机上安装Java或任何自定义版本,但要指定二进制文件的路径作为附加参数。

$ sdk install java custom-8 ~/Downloads/my-company-jdk-custom-8

5.3. Switching Between Versions

5.3.版本之间的切换

We can control the switching between versions in two forms, temporarily:

我们可以暂时以两种形式控制版本之间的切换。

$ sdk use java 14.0.1.j9-adpt

or permanently:

或永久性的。

$ sdk default java 14.0.1.j9-adpt

5.4. Remove a Version

5.4.删除一个版本

To remove an installed version, run the uninstall command with the targeted version:

要删除已安装的版本,请用目标版本运行uninstall命令。

$ sdk uninstall java 14.0.1.j9-adpt

5.5. Display the Versions in Use

5.5.显示使用中的版本

To check the current version of Java, we run the current command:

要检查Java的当前版本,我们运行current命令。

$ sdk current java
Using java version 14.0.2-zulu

Similarly, the last command has the same effect as:

同样,最后一条命令的效果也是如此。

$ java -version

To show the version by SDK on our machine, we can run the current command with no arguments:

为了显示我们机器上SDK的版本,我们可以运行current命令,不需要任何参数。

$ sdk current
Using:
java: 14.0.2-zulu
gradle: 6.2.2

6. Using SDKMAN! with an IDE

6.在IDE中使用SDKMAN!

The installed SDKs are stored in the SDKMAN! directory which defaults to ~/.sdkman/candidates.

已安装的SDK存储在SDKMAN!目录中,默认为~/.sdkman/candidates

For example, the different versions of Java will also be available under the ~/.sdkman/candidates/java/ directory and the subdirectories are named after the versions:

例如,不同版本的Java也会在~/.sdkman/candidates/java/目录下提供,子目录以版本命名。

$ ls -al ~/.sdkman/candidates/java/
total 0
drwxrwxrwx 1 user user 12 Jul 25 20:00 .
drwxrwxrwx 1 user user 12 Jul 25 20:00 ..
drwxrwxr-x 1 user user 12 Jul 25 20:00 14.0.2-zulu
lrwxrwxrwx 1 user user 14 Jul 25 20:00 current -> 14.0.2-zulu

Therefore, the currently selected version of Java will also be available as current in that directory.

因此,当前选择的Java版本也将在该目录中作为当前版本提供。

In the same vein, Gradle or any other SDK will be installed under the candidates directory.

同样,Gradle或任何其他SDK将被安装在candidates目录下。

In this way, we can use any particular version of Java, for example in our favorite IDE. All we have to do is to copy the path of a specific version and set it in the configuration of our IDE.

通过这种方式,我们可以使用任何特定版本的Java,例如在我们最喜欢的IDE中。我们所要做的就是复制一个特定版本的路径,并在我们的IDE的配置中设置它。

6.1. IntelliJ IDEA

6.1.IntelliJ IDEA

In IntelliJ IDEA, open “Project Structure”, then open “Project Settings”. In the project configuration, we can add a new Java version by selecting “New…” from the “Project SDK” section:

在IntelliJ IDEA中,打开 “项目结构”,然后打开 “项目设置”。在项目配置中,我们可以从 “项目SDK “部分选择 “新… “来添加一个新的Java版本。

Project Structure dialog in IntelliJ

We can also define the version of Java, Gradle, or Maven to use in the “Build Tools” section:

我们还可以在 “构建工具 “部分定义要使用的Java、Gradle或Maven的版本。

Maven Configuration in IntelliJ

Gradle Configuration in IntelliJ

Gradle Configuration in IntelliJ

Tip: The version of Java must be the same as used in “Project SDK” for Gradle or Maven.

提示:Java的版本必须与Gradle或Maven的 “项目SDK “中使用的相同。

6.2. Eclipse

日蚀

In Eclipse open “Project Properties”, select “Java Build Path”, and then switch to the “Libraries” tab. Here, we can manage the new SDK of Java via “Add Library…” and by following the instructions:

在Eclipse中打开 “项目属性”,选择 “Java构建路径”,然后切换到 “库 “标签。在这里,我们可以通过 “添加库…… “并按照说明来管理Java的新SDK。

Library managment in Eclipse

We can also control the installed SDKs for all the projects. Open “Preferences” under the “Window” menu, and then go to “Installed JREs”. Here we can manage the SDK of Java via “Add…” and by following the instructions:

我们还可以控制所有项目所安装的SDK。打开 “窗口 “菜单下的 “偏好”,然后进入 “安装的JREs”。在这里我们可以通过 “添加…… “并按照说明来管理Java的SDK。

Installed JREs in Eclipse

7. Conclusion

7.结语

In this tutorial, we’ve shown how SDKMAN! can help us in managing different versions of Java SDKs among other Java Environment tools such as Maven.

在本教程中,我们展示了SDKMAN!如何帮助我们在其他Java环境工具(如Maven)中管理不同版本的Java SDKs。