SDKMAN!是一个便利的东西,用于办理多个软件开发东西包的并行版别。该东西对Java开发者特别有用,由于它支撑JVM的SDK,如Java、Groovy、ScalaKotlin和Ceylon。还支撑Gradle、Maven、Spring Boot和许多其他软件:

  • 办理Java版别
  • 装置Maven和Gradle
  • 装置Spring Boot CLI
  • 链接

SDKMAN!支撑包括Windows在内的所有首要操作体系,不过它最简单装置在所有基于Unix的体系上。

在macOS上,翻开一个终端并运转:

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

下载结束后,重新发动终端会话或运转:

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

提示:我运用iTerm2终端作为默许。阅览我在macOS上运用的东西:macOS:(Java)开发人员的根本东西

装置结束后,SDKMAN!供给了一个便利的sdk ,用于办理SDK,称为Candidates。要列出所有可用的候选程序,请运用sdk list 指令。

办理Java版别

我最喜欢的是*SDKMAN!*在您的操作体系中办理不同的Java版别是非常简单的。您能够经过根本指令列出、装置、卸载并将选定的Java版别设置为默许版别。

  • 要列出可用的Java版别,请运转:

    sdk list java

$ 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
 GraalVM       |     | 19.3.0.r11   | grl     |            | 19.3.0.r11-grl
               |     | 19.3.0.r8    | grl     |            | 19.3.0.r8-grl
               |     | 19.3.0.2.r11 | grl     |            | 19.3.0.2.r11-grl
 Java.net      |     | 15.ea.2      | open    |            | 15.ea.2-open
               |     | 14.ea.28     | open    |            | 14.ea.28-open
               | >>> | 13.0.1       | open    | installed  | 13.0.1-open
               |     | 12.0.2       | open    | installed  | 12.0.2-open
               |     | 11.0.2       | open    |            | 11.0.2-open
               |     | 10.0.2       | open    |            | 10.0.2-open
               |     | 9.0.4        | open    |            | 9.0.4-open
================================================================================
Use the Identifier for installation:

    $ sdk install java 11.0.3.hs-adpt
================================================================================
  • 要装置指定的版别,运用标识符

    sdk install java 12.0.2-open

已装置的Java版别的二进制文件能够在*SDKMAN!*的主目录中找到,该目录默许为~/.sdkman/candidates/java

$ ls -al ~/.sdkman/candidates/java/
drwxr-xr-x  9 rafal.borowiec  staff  288 Jan  4 00:33 12.0.2-open
drwxr-xr-x  9 rafal.borowiec  staff  288 Oct  6 14:05 13.0.1-open
lrwxr-xr-x  1 rafal.borowiec  staff   57 Jan  4 00:20 current -> /Users/rafal.borowiec/.sdkman/candidates/java/13.0.1-open

假如你想在IntelliJ的项目中运用多个版别,这就很有用了。

  • 要将给定的版别设置为默许版别,请运转:

    sdk default java 12.0.2-open:

我们能够快速验证用sdk 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)
$ ls -al ~/.sdkman/candidates/java/
drwxr-xr-x  9 rafal.borowiec  staff  288 Jan  4 00:33 12.0.2-open
drwxr-xr-x  9 rafal.borowiec  staff  288 Oct  6 14:05 13.0.1-open
lrwxr-xr-x  1 rafal.borowiec  staff   57 Jan  4 00:20 current -> /Users/rafal.borowiec/.sdkman/candidates/java/13.0.1-open
$ sdk default java 12.0.2-open
Default java version set to 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)
$ ls -al ~/.sdkman/candidates/java/
drwxr-xr-x  9 rafal.borowiec  staff  288 Jan  4 00:33 12.0.2-open
drwxr-xr-x  9 rafal.borowiec  staff  288 Oct  6 14:05 13.0.1-open
lrwxr-xr-x  1 rafal.borowiec  staff   57 Jan  5 11:49 current -> /Users/rafal.borowiec/.sdkman/candidates/java/12.0.2-open
  • 要在当时终端会话中运用给定的版别,请运转:

    sdk use java 12.0.2-open

装置Maven和Gradle

Maven和Gradle是开源东西,用于自动构建应用程序并办理其依靠联系。这两个东西都能够用SDKMAN来办理!

  • 用下面的指令装置Maven:
$ sdk install maven
Downloading: maven 3.6.3
In progress... 100.0%
Installing: maven 3.6.3
Done installing!
Setting maven 3.6.3 as default.
  • 用下面的指令装置Gradle:
$ sdk install gradle
Downloading: gradle 6.0.1
In progress... 100.0%
Installing: gradle 6.0.1
Done installing!
Setting gradle 6.0.1 as default.

假如你需要多个版别的Gradle或Maven,你能够用sdk install maven 3.6.1 ,供给所需的版别来装置它们,然后用sdk setsdk default 指令在它们之间切换。

装置Spring Boot CLI

Spring Boot CLI是一个指令行东西,假如你想快速开发一个Spring应用程序,能够运用它。

你能够用sdk list springboot 指令检查可用的Spring Boot版别:

$ sdk list springboot
================================================================================
Available Springboot Versions
================================================================================
     2.2.2.RELEASE       2.0.1.RELEASE       1.5.1.RELEASE       1.2.3.RELEASE
     2.2.1.RELEASE       2.0.0.RELEASE       1.4.7.RELEASE       1.2.2.RELEASE
     2.2.0.RELEASE       1.5.22.RELEASE      1.4.6.RELEASE       1.2.1.RELEASE

经过运转sdk install springboot ,装置默许版别。

一旦装置,spring 指令就可用了:

$ spring
usage: spring [--help] [--version]
       <command> [<args>]

现在你能够经过运转以下指令发动一个新的Spring Boot项目:

$ spring init --build=gradle --java-version=12 --dependencies=web --packaging=jar my-app.zip
Using service at https://start.spring.io
Content saved to 'my-app.zip'

解开压缩包,从应用程序目录中运转:

gradle bootRun
Tomcat started on port(s): 8080 (http) with context path ''
2020-01-04 12:36:59.704  INFO 57156 --- [           main] com.example.myapp.DemoApplication        : Started DemoApplication in 1.251 seconds (JVM running for 1.485)

留意:上述指令运用了你用*SDKMAN!*的大局散布,你也能够经过调用./gradlew 指令在本地装置的Gradle中运转它。

一旦应用程序发动,请检查它是否对你的恳求做出呼应:

$ http localhost:8080
HTTP/1.1 404
Connection: keep-alive
Content-Type: application/json
Date: Sun, 05 Jan 2020 11:38:08 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
{
    "error": "Not Found",
    "message": "No message available",
    "path": "/",
    "status": 404,
    "timestamp": "2020-01-05T11:38:08.041+0000"
}

提示:我运用的是httpie客户端,而不是cURL。在这篇博文中阅览我在macOS上运用的东西:macOS:(Java)开发人员的根本东西