The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

介紹好用工具:SDKMAN (安裝各種軟體開發套件的利器)

最近開始將 Java 開發環境移往 WSL 環境並改用 SDKMAN 來安裝環境,結果發現非常好用,完全可以無腦安裝。今天這篇文章我就來介紹一下 SDKMAN 這套優秀的 SDK 安裝工具,並示範如何用來安裝 Java 開發環境。

SDKMAN

安裝 SDKMAN 工具

SDKMAN 是一套以 Bash 寫成的工具,只能跑在有 Bash 的環境下。若要在 Windows 環境下使用,建議跑在 WSL, CygwinGit Bash for Windows 等環境下。

  1. 安裝 SDKMAN

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

    安裝過程會提醒你所有必要事先安裝的工具套件。

  2. 首次手動載入 SDKMAN 工具 (預設安裝過程已經設定好 ~/.bashrc 啟動定義檔)

    source ~/.bashrc
    
  3. 檢查 SDKMAN 版本

    sdk version
    

使用 SDKMAN 安裝 OpenJDK、Apache Maven 與 Spring Boot CLI

  1. 安裝 Microsoft OpenJDK 17

    先列出所有 SDKMAN 中支援的 Java 版本

    sdk ls java
    
    ================================================================================
    Available Java Versions for Linux 64bit
    ================================================================================
    Vendor        | Use | Version      | Dist    | Status     | Identifier
    --------------------------------------------------------------------------------
    Corretto      |     | 18.0.2       | amzn    |            | 18.0.2-amzn
                  |     | 17.0.4       | amzn    |            | 17.0.4-amzn
                  |     | 17.0.0.35.1  | amzn    |            | 17.0.0.35.1-amzn
                  |     | 11.0.16      | amzn    |            | 11.0.16-amzn
                  |     | 11.0.12.7.1  | amzn    |            | 11.0.12.7.1-amzn
                  |     | 8.0.342      | amzn    |            | 8.0.342-amzn
    Dragonwell    |     | 17.0.4       | albba   |            | 17.0.4-albba
                  |     | 17.0.3       | albba   |            | 17.0.3-albba
                  |     | 11.0.16      | albba   |            | 11.0.16-albba
                  |     | 11.0.15      | albba   |            | 11.0.15-albba
                  |     | 11.0.12.8    | albba   |            | 11.0.12.8-albba
                  |     | 8.8.9        | albba   |            | 8.8.9-albba
                  |     | 8.0.345      | albba   |            | 8.0.345-albba
                  |     | 8.0.332      | albba   |            | 8.0.332-albba
    Gluon         |     | 22.1.0.1.r17 | gln     |            | 22.1.0.1.r17-gln
                  |     | 22.1.0.1.r11 | gln     |            | 22.1.0.1.r11-gln
    GraalVM       |     | 22.2.r17     | grl     |            | 22.2.r17-grl
                  |     | 22.2.r11     | grl     |            | 22.2.r11-grl
                  |     | 22.1.0.r17   | grl     |            | 22.1.0.r17-grl
                  |     | 22.1.0.r11   | grl     |            | 22.1.0.r11-grl
                  |     | 22.0.0.2.r17 | grl     |            | 22.0.0.2.r17-grl
                  |     | 22.0.0.2.r11 | grl     |            | 22.0.0.2.r11-grl
                  |     | 21.3.3.r17   | grl     |            | 21.3.3.r17-grl
                  |     | 21.3.3.r11   | grl     |            | 21.3.3.r11-grl
                  |     | 21.3.2.r17   | grl     |            | 21.3.2.r17-grl
                  |     | 21.3.2.r11   | grl     |            | 21.3.2.r11-grl
                  |     | 21.3.1.r8    | grl     |            | 21.3.1.r8-grl
                  |     | 21.2.0.r8    | grl     |            | 21.2.0.r8-grl
                  |     | 21.1.0.r8    | grl     |            | 21.1.0.r8-grl
                  |     | 20.3.6.r11   | grl     |            | 20.3.6.r11-grl
                  |     | 20.3.3.r8    | grl     |            | 20.3.3.r8-grl
                  |     | 20.3.2.r8    | grl     |            | 20.3.2.r8-grl
                  |     | 19.3.6.r11   | grl     |            | 19.3.6.r11-grl
                  |     | 19.3.6.r8    | grl     |            | 19.3.6.r8-grl
    Java.net      |     | 20.ea.15     | open    |            | 20.ea.15-open
                  |     | 20.ea.14     | open    |            | 20.ea.14-open
                  |     | 19.ea.36     | open    |            | 19.ea.36-open
                  |     | 19.ea.1.pma  | open    |            | 19.ea.1.pma-open
                  |     | 18.0.2       | open    |            | 18.0.2-open
                  |     | 18.0.2.1     | open    |            | 18.0.2.1-open
                  |     | 11.0.12      | open    |            | 11.0.12-open
                  |     | 11.0.11      | open    |            | 11.0.11-open
                  |     | 11.0.10      | open    |            | 11.0.10-open
                  |     | 11.0.2       | open    |            | 11.0.2-open
                  |     | 8.0.302      | open    |            | 8.0.302-open
                  |     | 8.0.292      | open    |            | 8.0.292-open
                  |     | 8.0.282      | open    |            | 8.0.282-open
                  |     | 8.0.265      | open    |            | 8.0.265-open
    Liberica      |     | 18.0.2.fx    | librca  |            | 18.0.2.fx-librca
                  |     | 18.0.2.1.fx  | librca  |            | 18.0.2.1.fx-librca
                  |     | 18.0.2.1     | librca  |            | 18.0.2.1-librca
                  |     | 18.0.2       | librca  |            | 18.0.2-librca
                  |     | 18.0.1.1.fx  | librca  |            | 18.0.1.1.fx-librca
                  |     | 18.0.1.1.fx  | librca  |            | 18.0.1.1.fx-librca
                  |     | 18.0.1.1     | librca  |            | 18.0.1.1-librca
                  |     | 17.0.4.fx    | librca  |            | 17.0.4.fx-librca
                  |     | 17.0.4.1.fx  | librca  |            | 17.0.4.1.fx-librca
                  |     | 17.0.4.1     | librca  |            | 17.0.4.1-librca
                  |     | 17.0.4       | librca  |            | 17.0.4-librca
                  |     | 17.0.3.1.fx  | librca  |            | 17.0.3.1.fx-librca
                  |     | 17.0.3.1     | librca  |            | 17.0.3.1-librca
                  |     | 11.0.16.fx   | librca  |            | 11.0.16.fx-librca
                  |     | 11.0.16.1.fx | librca  |            | 11.0.16.1.fx-librca
                  |     | 11.0.16.1    | librca  |            | 11.0.16.1-librca
                  |     | 11.0.16      | librca  |            | 11.0.16-librca
                  |     | 11.0.15.fx   | librca  |            | 11.0.15.fx-librca
                  |     | 11.0.15.1.fx | librca  |            | 11.0.15.1.fx-librca
                  |     | 11.0.15.1    | librca  |            | 11.0.15.1-librca
                  |     | 8.0.345.fx   | librca  |            | 8.0.345.fx-librca
                  |     | 8.0.345      | librca  |            | 8.0.345-librca
                  |     | 8.0.342.fx   | librca  |            | 8.0.342.fx-librca
                  |     | 8.0.342      | librca  |            | 8.0.342-librca
                  |     | 8.0.333.fx   | librca  |            | 8.0.333.fx-librca
                  |     | 8.0.333      | librca  |            | 8.0.333-librca
                  |     | 8.0.332.fx   | librca  |            | 8.0.332.fx-librca
    Liberica NIK  |     | 22.2.r17     | nik     |            | 22.2.r17-nik
                  |     | 22.2.r11     | nik     |            | 22.2.r11-nik
                  |     | 22.1.r17     | nik     |            | 22.1.r17-nik
                  |     | 22.1.r11     | nik     |            | 22.1.r11-nik
                  |     | 22.0.0.2.r17 | nik     |            | 22.0.0.2.r17-nik
                  |     | 22.0.0.2.r11 | nik     |            | 22.0.0.2.r11-nik
                  |     | 21.3.3.r17   | nik     |            | 21.3.3.r17-nik
                  |     | 21.3.3.r11   | nik     |            | 21.3.3.r11-nik
                  |     | 21.3.2.r17   | nik     |            | 21.3.2.r17-nik
                  |     | 21.3.2.r11   | nik     |            | 21.3.2.r11-nik
                  |     | 21.2         | nik     |            | 21.2-nik
                  |     | 21.1         | nik     |            | 21.1-nik
                  |     | 21.0.0.2.r11 | nik     |            | 21.0.0.2.r11-nik
                  |     | 21.0.0.2     | nik     |            | 21.0.0.2-nik
    Mandrel       |     | 22.2.r17     | mandrel |            | 22.2.r17-mandrel
                  |     | 22.2.r11     | mandrel |            | 22.2.r11-mandrel
                  |     | 22.1.0.0.r17 | mandrel |            | 22.1.0.0.r17-mandrel
                  |     | 22.1.0.0.r11 | mandrel |            | 22.1.0.0.r11-mandrel
                  |     | 22.0.0.2.r17 | mandrel |            | 22.0.0.2.r17-mandrel
                  |     | 22.0.0.2.r11 | mandrel |            | 22.0.0.2.r11-mandrel
                  |     | 21.3.3.r17   | mandrel |            | 21.3.3.r17-mandrel
                  |     | 21.3.3.r11   | mandrel |            | 21.3.3.r11-mandrel
                  |     | 21.3.2.0.r17 | mandrel |            | 21.3.2.0.r17-mandrel
                  |     | 21.3.2.0.r11 | mandrel |            | 21.3.2.0.r11-mandrel
                  |     | 21.3.1.1.r17 | mandrel |            | 21.3.1.1.r17-mandrel
                  |     | 21.3.1.1.r11 | mandrel |            | 21.3.1.1.r11-mandrel
                  |     | 21.3.1.0.r17 | mandrel |            | 21.3.1.0.r17-mandrel
                  |     | 21.3.1.0.r11 | mandrel |            | 21.3.1.0.r11-mandrel
                  |     | 21.3.0.0     | mandrel |            | 21.3.0.0-mandrel
                  |     | 21.2.0.2     | mandrel |            | 21.2.0.2-mandrel
                  |     | 20.3.3.0     | mandrel |            | 20.3.3.0-mandrel
    Microsoft     |     | 17.0.4.1     | ms      |            | 17.0.4.1-ms
                  |     | 17.0.4       | ms      |            | 17.0.4-ms
                  |     | 17.0.3       | ms      |            | 17.0.3-ms
                  |     | 11.0.16.1    | ms      |            | 11.0.16.1-ms
                  |     | 11.0.16      | ms      |            | 11.0.16-ms
                  |     | 11.0.15      | ms      |            | 11.0.15-ms
    Oracle        |     | 18.0.2       | oracle  |            | 18.0.2-oracle
                  |     | 18.0.1       | oracle  |            | 18.0.1-oracle
                  |     | 17.0.4       | oracle  |            | 17.0.4-oracle
                  |     | 17.0.3       | oracle  |            | 17.0.3-oracle
    SapMachine    |     | 18.0.2       | sapmchn |            | 18.0.2-sapmchn
                  |     | 18.0.2.1     | sapmchn |            | 18.0.2.1-sapmchn
                  |     | 18.0.1.1     | sapmchn |            | 18.0.1.1-sapmchn
                  |     | 17.0.4       | sapmchn |            | 17.0.4-sapmchn
                  |     | 17.0.4.1     | sapmchn |            | 17.0.4.1-sapmchn
                  |     | 17.0.3       | sapmchn |            | 17.0.3-sapmchn
                  |     | 17.0.3.0.1   | sapmchn |            | 17.0.3.0.1-sapmchn
                  |     | 17.0.2       | sapmchn |            | 17.0.2-sapmchn
                  |     | 11.0.16      | sapmchn |            | 11.0.16-sapmchn
                  |     | 11.0.16.1    | sapmchn |            | 11.0.16.1-sapmchn
                  |     | 11.0.15      | sapmchn |            | 11.0.15-sapmchn
                  |     | 11.0.15.0.1  | sapmchn |            | 11.0.15.0.1-sapmchn
                  |     | 11.0.14      | sapmchn |            | 11.0.14-sapmchn
                  |     | 11.0.14.1    | sapmchn |            | 11.0.14.1-sapmchn
    Semeru        |     | 18.0.2       | sem     |            | 18.0.2-sem
                  |     | 18.0.1.1     | sem     |            | 18.0.1.1-sem
                  |     | 17.0.4       | sem     |            | 17.0.4-sem
                  |     | 17.0.4.1     | sem     |            | 17.0.4.1-sem
                  |     | 17.0.3       | sem     |            | 17.0.3-sem
                  |     | 11.0.16      | sem     |            | 11.0.16-sem
                  |     | 11.0.16.1    | sem     |            | 11.0.16.1-sem
                  |     | 11.0.15      | sem     |            | 11.0.15-sem
                  |     | 8.0.345      | sem     |            | 8.0.345-sem
                  |     | 8.0.332      | sem     |            | 8.0.332-sem
    Temurin       |     | 18.0.2       | tem     |            | 18.0.2-tem
                  |     | 18.0.2.1     | tem     |            | 18.0.2.1-tem
                  |     | 18.0.1       | tem     |            | 18.0.1-tem
                  |     | 17.0.4       | tem     |            | 17.0.4-tem
                  |     | 17.0.4.1     | tem     |            | 17.0.4.1-tem
                  |     | 17.0.3       | tem     |            | 17.0.3-tem
                  |     | 11.0.16      | tem     |            | 11.0.16-tem
                  |     | 11.0.16.1    | tem     |            | 11.0.16.1-tem
                  |     | 11.0.15      | tem     |            | 11.0.15-tem
                  |     | 8.0.345      | tem     |            | 8.0.345-tem
                  |     | 8.0.342      | tem     |            | 8.0.342-tem
                  |     | 8.0.332      | tem     |            | 8.0.332-tem
    Trava         |     | 11.0.15      | trava   |            | 11.0.15-trava
                  |     | 11.0.9       | trava   |            | 11.0.9-trava
                  |     | 8.0.282      | trava   |            | 8.0.282-trava
                  |     | 8.0.232      | trava   |            | 8.0.232-trava
    Zulu          |     | 18.0.2       | zulu    |            | 18.0.2-zulu
                  |     | 18.0.2.fx    | zulu    |            | 18.0.2.fx-zulu
                  |     | 18.0.2.1     | zulu    |            | 18.0.2.1-zulu
                  |     | 18.0.2.1.fx  | zulu    |            | 18.0.2.1.fx-zulu
                  |     | 18.0.1       | zulu    |            | 18.0.1-zulu
                  |     | 18.0.1.fx    | zulu    |            | 18.0.1.fx-zulu
                  |     | 17.0.4       | zulu    |            | 17.0.4-zulu
                  |     | 17.0.4.fx    | zulu    |            | 17.0.4.fx-zulu
                  |     | 17.0.4.1     | zulu    |            | 17.0.4.1-zulu
                  |     | 17.0.4.1.fx  | zulu    |            | 17.0.4.1.fx-zulu
                  |     | 17.0.3       | zulu    |            | 17.0.3-zulu
                  |     | 17.0.3.fx    | zulu    |            | 17.0.3.fx-zulu
                  |     | 11.0.16      | zulu    |            | 11.0.16-zulu
                  |     | 11.0.16.fx   | zulu    |            | 11.0.16.fx-zulu
                  |     | 11.0.16.1    | zulu    |            | 11.0.16.1-zulu
                  |     | 11.0.16.1.fx | zulu    |            | 11.0.16.1.fx-zulu
                  |     | 11.0.15      | zulu    |            | 11.0.15-zulu
                  |     | 11.0.15.fx   | zulu    |            | 11.0.15.fx-zulu
                  |     | 8.0.345      | zulu    |            | 8.0.345-zulu
                  |     | 8.0.345.fx   | zulu    |            | 8.0.345.fx-zulu
                  |     | 8.0.342      | zulu    |            | 8.0.342-zulu
                  |     | 8.0.342.fx   | zulu    |            | 8.0.342.fx-zulu
                  |     | 8.0.332      | zulu    |            | 8.0.332-zulu
                  |     | 8.0.332.fx   | zulu    |            | 8.0.332.fx-zulu
                  |     | 7.0.352      | zulu    |            | 7.0.352-zulu
                  |     | 7.0.342      | zulu    |            | 7.0.342-zulu
                  |     | 6.0.119      | zulu    |            | 6.0.119-zulu
    ================================================================================
    Omit Identifier to install default version 17.0.4.1-tem:
        $ sdk install java
    Use TAB completion to discover available versions
        $ sdk install java [TAB]
    Or install a specific version by Identifier:
        $ sdk install java 17.0.4.1-tem
    Hit Q to exit this list view
    ================================================================================
    

    我們安裝 17.0.4.1-ms 這個版本

    sdk install java 17.0.4.1-ms
    

    使用 SDKMAN 查看目前安裝的 Java 版本

    $ sdk current java
    
    Using java version 17.0.4.1-ms
    

    查看目前安裝的 Java 版本

    $ java -version
    openjdk version "17.0.4.1" 2022-08-12 LTS
    OpenJDK Runtime Environment Microsoft-40354 (build 17.0.4.1+1-LTS)
    OpenJDK 64-Bit Server VM Microsoft-40354 (build 17.0.4.1+1-LTS, mixed mode, sharing)
    
  2. 安裝 Apache Maven CLI

    sdk install maven
    
  3. 安裝 Spring Boot CLI

    sdk install springboot
    
  4. 檢查已安裝版本

    $ spring --version
    Spring CLI v2.7.3
    
    $ mvn -v
    Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
    Maven home: /home/will/.sdkman/candidates/maven/current
    Java version: 17.0.4.1, vendor: Microsoft, runtime: /usr/lib/jvm/msopenjdk-17-amd64
    Default locale: en, platform encoding: UTF-8
    OS name: "linux", version: "5.10.102.1-microsoft-standard-wsl2", arch: "amd64", family: "unix"
    

以下是快速安裝懶人包:

curl -s "https://get.sdkman.io" | bash
source ~/.bashrc

sdk install java 17.0.4.1-ms
sdk install maven
sdk install springboot

透過 SDKMAN 管理多個 JDK 版本

若要用 SDKMAN 管理多個不同的 JDK 版本非常容易,只要用以下步驟體驗一次就知道怎麼做了。

  1. 先安裝多個不同版本

    sdk install java 8.0.345-zulu
    
    Downloading: java 8.0.345-zulu
    
    In progress...
    
    ############################################################################### 100.0%
    
    Repackaging Java 8.0.345-zulu...
    
    Done repackaging...
    
    Installing: java 8.0.345-zulu
    Done installing!
    
    Do you want java 8.0.345-zulu to be set as default? (Y/n): n
    

    這裡我們先選擇 n 不讓新安裝的版本變成預設值。

  2. 列出目前安裝的版本

    sdk list java
    
  3. 在目前 Shell 環境切換版本

    sdk use java 8.0.345-zulu
    

    此時可以透過 sdk current javajava -version 確認版本

  4. 若希望在未來新的 Shell 工作階段預設都使用 8.0.345-zulu 的話

    sdk default java 8.0.345-zulu
    

    此時你可以開一個新的 Shell session 並透過 sdk current javajava -version 確認版本

透過 SDKMAN 管理更新、升級、移除

  1. 查看是否有更新版本

    sdk update
    
  2. 升級版本

    sdk upgrade
    
  3. 查看是否有更新版本

    sdk update
    
  4. 移除特定版本

    # 記得將預設版本切換到現有版本
    sdk default java 17.0.4.1-ms
    
    sdk uninstall java 8.0.345-zulu
    

更新 SDKMAN 到最新版

這部分只要執行以下命令即可:

sdk selfupdate

相關連結