JVM系のソフトウェアをSDKMANで管理する

はじめに

不勉強なもので、JVM系の言語やツールを管理しやすくするSDKMANというツールの存在をつい最近知りました。 インストールやアップデート、複数のバージョンの切り替えなどをsdkというコマンドで扱えるようです。 この辺は悩みの種になりやすいところなので使ってみることにしました。

SDKMANインストール

$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh" 
$ sdk version
SDKMAN 5.7.4+362

使い方

扱えるソフトウェア一覧

$ sdk list
================================================================================
Available Candidates
================================================================================
q-quit                                  /-search down
j-down                                  ?-search up
k-up                                    h-help

--------------------------------------------------------------------------------
Ant (1.10.1)                                             https://ant.apache.org/

Apache Ant is a Java library and command-line tool whose mission is to drive
processes described in build files as targets and extension points dependent
upon each other. The main known usage of Ant is the build of Java applications.
Ant supplies a number of built-in tasks allowing to compile, assemble, test and
run Java applications. Ant can also be used effectively to build non Java
applications, for instance C or C++ applications. More generally, Ant can be
used to pilot any type of process which can be described in terms of targets and
tasks.

                                                               $ sdk install ant
--------------------------------------------------------------------------------
AsciidoctorJ (2.1.0)                                     http://asciidoctor.org/

AsciidoctorJ is the official library for running Asciidoctor on the JVM. Using
AsciidoctorJ, you can convert AsciiDoc content or analyze the structure of a
parsed AsciiDoc document from Java and other JVM languages.

                                                      $ sdk install asciidoctorj
--------------------------------------------------------------------------------
(略)

特定のソフトウェアのインストール候補

$ sdk list java
================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 AdoptOpenJDK  |     | 13.0.1.j9    | adpt    |            | 13.0.1.j9-adpt      
               |     | 13.0.1.hs    | adpt    |            | 13.0.1.hs-adpt      
               |     | 12.0.2.j9    | adpt    |            | 12.0.2.j9-adpt      
               |     | 12.0.2.hs    | adpt    |            | 12.0.2.hs-adpt      
               |     | 11.0.5.j9    | adpt    |            | 11.0.5.j9-adpt      
               |     | 11.0.5.hs    | adpt    |            | 11.0.5.hs-adpt      
               |     | 8.0.232.j9   | adpt    |            | 8.0.232.j9-adpt     
               |     | 8.0.232.hs   | adpt    |            | 8.0.232.hs-adpt     
 Amazon        |     | 11.0.5       | amzn    |            | 11.0.5-amzn         
               |     | 8.0.232      | amzn    |            | 8.0.232-amzn        
 Azul Zulu     |     | 13.0.1       | zulu    |            | 13.0.1-zulu         
               |     | 12.0.2       | zulu    |            | 12.0.2-zulu         
               |     | 11.0.5       | zulu    |            | 11.0.5-zulu         
               |     | 10.0.2       | zulu    |            | 10.0.2-zulu         
               |     | 9.0.7        | zulu    |            | 9.0.7-zulu          
               |     | 8.0.232      | zulu    |            | 8.0.232-zulu        
               |     | 7.0.242      | zulu    |            | 7.0.242-zulu        
               |     | 6.0.119      | zulu    |            | 6.0.119-zulu        
 Azul ZuluFX   |     | 11.0.2       | zulufx  |            | 11.0.2-zulufx       
               |     | 8.0.202      | zulufx  |            | 8.0.202-zulufx      
 BellSoft      |     | 13.0.1       | librca  |            | 13.0.1-librca       
               |     | 12.0.2       | librca  |            | 12.0.2-librca       
               |     | 11.0.5       | librca  |            | 11.0.5-librca       
               |     | 8.0.232      | librca  |            | 8.0.232-librca      
 GraalVM       |     | 19.3.0.r11   | grl     |            | 19.3.0.r11-grl      
               |     | 19.3.0.r8    | grl     |            | 19.3.0.r8-grl       
               |     | 19.2.1       | grl     |            | 19.2.1-grl          
               |     | 19.1.1       | grl     |            | 19.1.1-grl          
               |     | 19.0.2       | grl     |            | 19.0.2-grl          
               |     | 1.0.0        | grl     |            | 1.0.0-rc-16-grl     
 Java.net      |     | 14.ea.26     | open    |            | 14.ea.26-open       
               | >>> | 13.0.1       | open    | installed  | 13.0.1-open         
               |     | 12.0.2       | open    |            | 12.0.2-open         
               |     | 11.0.5       | open    |            | 11.0.5-open         
               |     | 10.0.2       | open    |            | 10.0.2-open         
               |     | 9.0.4        | open    |            | 9.0.4-open          
               |     | 8.0.232      | open    |            | 8.0.232-open        
 SAP           |     | 12.0.2       | sapmchn |            | 12.0.2-sapmchn      
               |     | 11.0.4       | sapmchn |            | 11.0.4-sapmchn      
================================================================================
Use the Identifier for installation:

    $ sdk install java 11.0.3.hs-adpt
================================================================================

インストール

まずはJava。

$ sdk install java 13.0.1-open

Downloading: java 13.0.1-open

In progress...

######################################################################################################################################################### 100.0%

Repackaging Java 13.0.1-open...

Done repackaging...

Installing: java 13.0.1-open
Done installing!

Setting java 13.0.1-open as default.

$ java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

ScalaとKotlinも。

$ sdk install kotlin 1.3.61
$ sdk install scala 2.13.1
$ sdk current

Using:

java: 13.0.1-open
kotlin: 1.3.61
scala: 2.13.1

複数のバージョンの使い分け

JDKをもう1種類インストールしてみる。

$ sdk install java 12.0.2-open

Downloading: java 12.0.2-open

In progress...

######################################################################################################################################################### 100.0%

Repackaging Java 12.0.2-open...

Done repackaging...

Installing: java 12.0.2-open
Done installing!

Do you want java 12.0.2-open to be set as default? (Y/n): Y

Setting java 12.0.2-open as default.

こっちをデフォルトにしますか?という質問にYと答えたので12.0.2-openがデフォルトになりました。

$ sdk list java
================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 AdoptOpenJDK  |     | 13.0.1.j9    | adpt    |            | 13.0.1.j9-adpt      
               |     | 13.0.1.hs    | adpt    |            | 13.0.1.hs-adpt      
               |     | 12.0.2.j9    | adpt    |            | 12.0.2.j9-adpt      
               |     | 12.0.2.hs    | adpt    |            | 12.0.2.hs-adpt      
               |     | 11.0.5.j9    | adpt    |            | 11.0.5.j9-adpt      
               |     | 11.0.5.hs    | adpt    |            | 11.0.5.hs-adpt      
               |     | 8.0.232.j9   | adpt    |            | 8.0.232.j9-adpt     
               |     | 8.0.232.hs   | adpt    |            | 8.0.232.hs-adpt     
 Amazon        |     | 11.0.5       | amzn    |            | 11.0.5-amzn         
               |     | 8.0.232      | amzn    |            | 8.0.232-amzn        
 Azul Zulu     |     | 13.0.1       | zulu    |            | 13.0.1-zulu         
               |     | 12.0.2       | zulu    |            | 12.0.2-zulu         
               |     | 11.0.5       | zulu    |            | 11.0.5-zulu         
               |     | 10.0.2       | zulu    |            | 10.0.2-zulu         
               |     | 9.0.7        | zulu    |            | 9.0.7-zulu          
               |     | 8.0.232      | zulu    |            | 8.0.232-zulu        
               |     | 7.0.242      | zulu    |            | 7.0.242-zulu        
               |     | 6.0.119      | zulu    |            | 6.0.119-zulu        
 Azul ZuluFX   |     | 11.0.2       | zulufx  |            | 11.0.2-zulufx       
               |     | 8.0.202      | zulufx  |            | 8.0.202-zulufx      
 BellSoft      |     | 13.0.1       | librca  |            | 13.0.1-librca       
               |     | 12.0.2       | librca  |            | 12.0.2-librca       
               |     | 11.0.5       | librca  |            | 11.0.5-librca       
               |     | 8.0.232      | librca  |            | 8.0.232-librca      
 GraalVM       |     | 19.3.0.r11   | grl     |            | 19.3.0.r11-grl      
               |     | 19.3.0.r8    | grl     |            | 19.3.0.r8-grl       
               |     | 19.2.1       | grl     |            | 19.2.1-grl          
               |     | 19.1.1       | grl     |            | 19.1.1-grl          
               |     | 19.0.2       | grl     |            | 19.0.2-grl          
               |     | 1.0.0        | grl     |            | 1.0.0-rc-16-grl     
 Java.net      |     | 14.ea.26     | open    |            | 14.ea.26-open       
               |     | 13.0.1       | open    | installed  | 13.0.1-open         
               | >>> | 12.0.2       | open    | installed  | 12.0.2-open         
               |     | 11.0.5       | open    |            | 11.0.5-open         
               |     | 10.0.2       | open    |            | 10.0.2-open         
               |     | 9.0.4        | open    |            | 9.0.4-open          
               |     | 8.0.232      | open    |            | 8.0.232-open        
 SAP           |     | 12.0.2       | sapmchn |            | 12.0.2-sapmchn      
               |     | 11.0.4       | sapmchn |            | 11.0.4-sapmchn      
================================================================================
Use the Identifier for installation:

    $ sdk install java 11.0.3.hs-adpt
================================================================================

13.0.1-openと12.0.2-openがインストールされていて、デフォルトが12.0.2-openになっていることが分かります。

$ java -version
openjdk version "12.0.2" 2019-07-16
OpenJDK Runtime Environment (build 12.0.2+10)
OpenJDK 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)

確かに12.0.2が使われます。これを13.0.1-openに切り替えてみます。

$ sdk default java 13.0.1-open

Default java version set to 13.0.1-open

$ java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

どのように管理されているのか?

このまま使うのは気持ち悪いので、各ソフトウェアがどういう風にインストールされているのかを確認してみます。

$ which java
/home/myuser/.sdkman/candidates/java/current/bin/java

どうやらホームディレクトリの.sdkman以下にインストールされているようです。
ディレクトリとファイルの構成は以下の通り。

$ cd ~/.sdkman
$ ls
archives  bin  candidates  etc  ext  src  tmp  var
$ ls archives/
java-12.0.2-open.zip  java-13.0.1-open.zip  kotlin-1.3.61.zip  scala-2.13.1.zip
$ ls bin
sdkman-init.sh
$ find etc
etc
etc/config
$ cat etc/config 
sdkman_auto_answer=false
sdkman_auto_selfupdate=false
sdkman_insecure_ssl=false
sdkman_curl_connect_timeout=7
sdkman_curl_max_time=10
sdkman_beta_channel=false
sdkman_debug_mode=false
sdkman_colour_enable=true
$ ls src
sdkman-availability.sh  sdkman-current.sh      sdkman-flush.sh    sdkman-list.sh     sdkman-path-helpers.sh  sdkman-update.sh   sdkman-utils.sh
sdkman-broadcast.sh     sdkman-default.sh      sdkman-help.sh     sdkman-main.sh     sdkman-selfupdate.sh    sdkman-upgrade.sh  sdkman-version.sh
sdkman-cache.sh         sdkman-env-helpers.sh  sdkman-install.sh  sdkman-offline.sh  sdkman-uninstall.sh     sdkman-use.sh
$ find var
var
var/delay_upgrade
var/broadcast_id
var/version
var/broadcast
var/candidates
$ cat var/version 
5.7.4+362
$ cat var/candidates 
ant,asciidoctorj,bpipe,ceylon,crash,cuba,cxf,gaiden,glide,gradle,grails,groovy,groovyserv,infrastructor,java,jbake,kotlin,kscript,lazybones,leiningen,maven,micronaut,sbt,scala,spark,springboot,sshoogr,vertx,visualvm

管理対象のソフトウェアは.sdkman/candidatesの下にありました。

$ ls candidates/
java  kotlin  scala
$ ls -l candidates/java
合計 8
drwxr-xr-x 8 myuser myuser 4096 12月 13 15:25 12.0.2-open
drwxr-xr-x 8 myuser myuser 4096 12月 13 15:12 13.0.1-open
lrwxrwxrwx 1 myuser myuser   48 12月 13 15:29 current -> /home/myuser/.sdkman/candidates/java/13.0.1-open
$ ls -l candidates/kotlin
合計 4
drwxr-xr-x 5 myuser myuser 4096 11月 26 09:15 1.3.61
lrwxrwxrwx 1 myuser myuser   45 12月 13 15:22 current -> /home/myuser/.sdkman/candidates/kotlin/1.3.61
$ ls -l candidates/scala
合計 4
drwxrwxr-x 6 myuser myuser 4096  9月 18 18:23 2.13.1
lrwxrwxrwx 1 myuser myuser   44 12月 13 15:23 current -> /home/myuser/.sdkman/candidates/scala/2.13.1

ソフトウェアの種類ごとにディレクトリを分けてその下に複数のバージョンを展開し、シンボリックリンクでcurrentを示す、という割とシンプルな構成でした。

おわりに

言語処理系以外でも、ant、Maven、sbt等の周辺ツールもSDKMANで管理できます。仕組みもシンプルで使い勝手良さそうです。開発環境で活用していきたいと思います。

Related Post