1.2.1 安装Spring Boot CLI

  正如我们之前讨论过的,Spring Boot CLI为我们提供了一个有趣,又不传统的方式来进行Spring应用的开发。我们会在第五章详细分析CLI到底为我们提供了什么。但是现在,让我们先来看看如何安装Spring Boot CLI,从而可以运行我们在代码清单1-1中所看到的代码。   安装Spring Boot CLI有多种方式:

  • 使用distribution版本安装
  • 使用SDKManager安装
  • 使用HomeBrew进行安装
  • 使用MacPorts安装

  这些安装方式我们一个个来看。此外,我们也会手动的彻底安装Spring Boot CLI,通过Bash或zsh等shell脚本。(这里要对Windows使用者说抱歉了,因为BashShell只有Linux系统支持。)那么,首先让我们来看看如何通过Spring以发布的版本来进行安装。

Spring Boot CLI 安装手册(使用distribution版本安装)
  或许安装Spring Boot CLI最直接的办法就是下载其压缩包,然后解压,将其bin目录添加到PATH中(环境变量中)。你可以从以下链接下载已发布的文件:

    * http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/
    1.3.0.RELEASE /spring-boot-cli- 1.3.0.RELEASE -bin.zip
    * http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/
    1.3.0.RELEASE /spring-boot-cli- 1.3.0.RELEASE -bin.tar.gz  

  只要你下载已经发布的版本,将其解压到你的文件系统中,在当中你可以找到包含spring命令脚本的bin目录。bin目录既包含windows下的脚本,也包含Linux系统下的脚本。然后将bin目录添加到系统的PATH中(环境变量中),即可以准备运行Spring Boot CLI了。

  • 创建Spring Boot的软链接 如果你使用SpringBoot在类Unix系统上,你可以创建一个为解压缩的文件链接,并把链接的目录添加到系统的PATH配置中,而不使用直接目录。通过修改软连接,这样就可以很好的更新SpringBooter的版本,甚至可以灵活的切换版本,你可以在CLI下通过试试如下命令来验证版本。
    $ spring --version
    
      如果一切工作正常,你将会看到显式出已安装的Spring Boot CLI版本提示。
      即使这是一次手工安装,它也是一种很好的选择,它也不需要你额外在安装什么。如果你是Window用户,这也是唯一提供给你的安装方式了。但如果你是Linux机器,就可以做更多的自动化配置了,要想这样,SDKManager可能会很有帮助。

安装 SDKManager(使用SDKManager安装)
The Software Development Kit Manager ( SDKMAN ; formerly known as GVM ) can be used to install and manage multiple versions of Spring Boot CLI installations. In order to use SDKMAN , you’ll need to get and install the SDKMAN tool from http://sdkman .io. The easiest way to install SDKMAN is at the command line:

$ curl -s get.sdkman.io | bash

Follow the instructions given in the output to complete the SDKMAN installation. For my machine, I had to perform the following command at the command line:

$ source "/Users/habuma/.sdkman/bin/sdkman-init.sh"

  Note that this command will be different for different users. In my case, my home directory is at /Users/habuma , so that’s the root of the shell script’s path. You’ll want to adjust accordingly to fit your situation.   Once SDKMAN is installed, you can install Spring Boot’s CLI like this:

  $ sdk install springboot
  $ spring --version

  Assuming all goes well, you’ll be shown the current version of Spring Boot.
  If you want to upgrade to a newer version of Spring Boot CLI , you just need to install it and start using it. To find out which versions of Spring Boot CLI are available, use SDKMAN ’s list command:

$ sdk list springboot

  The list command shows all available versions, including which versions are installed and which is currently in use. From this list you can choose to install a version and then use it. For example, to install Spring Boot CLI version 1.3.0.RELEASE , you’d use the install command, specifying the version:

$ sdk install springboot 1.3.0.RELEASE

  If you’d like that version to be the default for all shells, use the default command:

$ sdk default springboot 1.3.0.RELEASE

  The nice thing about using SDKMAN to manage your Spring Boot CLI installation is that it allows you to easily switch between different versions of Spring Boot. This will enable you to try out snapshot, milestone, and release candidate builds before they’re formally released, but still switch back to a stable release for other work.

使用HomeBrew进行安装
If you’ll be developing on an OS X machine, you have the option of using Homebrew to install the Spring Boot CLI . Homebrew is a package manager for OS X that is used to install many different applications and tools. The easiest way to install Homebrew is by running the installation Ruby script:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/
master/install)"

You can read more about Homebrew (and find other installation options) at http:// brew.sh. In order to install the Spring Boot CLI using Homebrew, you’ll need to “tap” Piv- otal’s tap: 1

$ brew tap pivotal/tap

Now that Homebrew is tapping Pivotal’s tap, you can install the Spring Boot CLI like this:

$ brew install springboot

Homebrew will install the Spring Boot CLI to /usr/local/bin, and it’s ready to go. You can verify the installation by checking the version that was installed:

$ spring --version

It should respond by showing you the version of Spring Boot that was installed. You can also try running the code in listing 1.1.

1 Tapping is a way to add additional repositories to those that Homebrew works from. Pivotal, the company behind Spring and Spring Boot, has made the Spring Boot CLI available through its tap.

使用MacPorts安装
  Another Spring Boot CLI installation option for OS X users is to use MacPorts, another popular installer for Mac OS X . In order to use MacPorts to install the Spring Boot CLI , you must first install MacPorts, which itself requires that you have Xcode installed. Furthermore, the steps for installing MacPorts vary depending on which ver- sion of OS X you’re using. Therefore, I refer you to https://www.macports.org/ install.php for instructions on installing MacPorts.
  Once you have MacPorts installed, you can install the Spring Boot CLI at the com- mand line like this:

$ sudo port install spring-boot-cli

MacPorts will install the Spring Boot CLI to /opt/local/share/java/spring-boot-cli and put a symbolic link to the binary in /opt/local/bin, which should already be in your system path from installing MacPorts. You can verify the installation by checking the version that was installed:

$ spring --version

It should respond by showing you the version of Spring Boot that was installed. You can also try running the code in listing 1.1.

ENABLING COMMAND-LINE COMPLETION   Spring Boot’s CLI offers a handful of commands for running, packaging, and testing your CLI -based application. Moreover, each of those commands has several options. It can be difficult to remember all that the CLI offers. Command-line completion can help you recall how to use the Spring Boot CLI .
  If you’ve installed the Spring Boot CLI with Homebrew, you already have command- line completion installed. But if you installed Spring Boot manually or with SDKMAN , you’ll need to source the scripts or install the completion scripts manually. (Command- line completion isn’t an option if you’ve installed the Spring Boot CLI via MacPorts.)
  The completion scripts are found in the Spring Boot CLI installation directory under the shell-completion subdirectory. There are two different scripts, one for BASH and one for zsh. To source the completion script for BASH , you can enter the following at the command line (assuming a SDKMAN installation):

$ . ~/.sdkman/springboot/current/shell-completion/bash/spring

  This will give you Spring Boot CLI completion for the current shell, but you’ll have to source this script again each time you start a new shell to keep that feature. Option- ally, you can copy the script to your personal or system script directory. The location of the script directory varies for different Unix installations, so consult your system docu- mentation (or Google) for details.
  With command completion enabled, you should be able to type spring at the command line and then hit the Tab key to be offered options for what to type next. Once you’ve chosen a command, type -- (double-hyphen) and then hit Tab again to be shown a list of options for that command.
  If you’re developing on Windows or aren’t using BASH or zsh, you can’t use these command-line completion scripts. Even so, you can get command completion if you run the Spring Boot CLI shell:

$ spring shell

  Unlike the command-completion scripts for BASH and zsh (which operate within the BASH /zsh shell), the Spring Boot CLI shell opens a new Spring Boot–specific shell. From this shell, you can execute any of the CLI ’s commands and get command com- pletion with the Tab key.
  The Spring Boot CLI offers an easy way to get started with Spring Boot and to prototype simple applications. As we’ll discuss later in chapter 8, it can also be used for production-ready applications, given the right production runtime environment.
  Even so, Spring Boot CLI ’s process is rather unconventional in contrast to how most Java projects are developed. Typically, Java projects use tools like Gradle or Maven to build WAR files that are deployed to an application server. If the CLI model feels a little uncomfortable, you can still take advantage of most of the features of Spring Boot in the context of a traditionally built Java project. 2 And the Spring Initializr can help you get started.

results matching ""

    No results matching ""