Skip to content

Latest commit

 

History

History
303 lines (202 loc) · 15.5 KB

10.InstallingSpringBoot.md

File metadata and controls

303 lines (202 loc) · 15.5 KB

10. Installing Spring Boot

10. 安装Spring Boot

Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Either way, you need Java SDK v1.8 or higher. Before you begin, you should check your current Java installation by using the following command:

Spring Boot可以和“经典”的Java开发工具一起使用,或作为命令行工具来进行安装。不管那种方式,你都需要Java SDK 1.8或更高版本。在你开始之前,你需要使用下面的命令来检查你当前已经安装的Java版本:

$ java -version

If you are new to Java development or if you want to experiment with Spring Boot, you might want to try the Spring Boot CLI(Command Line Interface) first. Otherwise, read on for "classic" installation instructions.

如果你是一名Java开发初学者或者你想尝试Spring Boot,你也许可以尝试先从Spring Boot CLI(命令行接口)开始。另外,读一些“经典”安装说明。

Installation Instructions for the Java Developer

Java开发者的安装说明

You can use Spring Boot in the same way as any standard Java library.To do so, include the appropriate spring-boot-*.jar files on you classpath.Spring Boot does not require any special tools integration, so you can use any IDE or text editor. Also, there is nothing special about a Spring Boot application, so you can run and debug a Spring Boot application as you would any other Java program.

你可以像使用任何Java标准类库一样使用Spring Boot。只需要将恰当的spring-boot-*.jar包含在你的classpath里即可。Spring Boot不需要任何特殊的工具集成,所以你可以使用任何IDE或者文本编辑器。当然,一个Spring Boot应用程序也没有什么特殊的要求,所以你可以像任何其他Java程序一样运行、调试Spring Boot应用程序。

Although you could copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management(such as Maven or Gradle).

虽然你可以只拷贝Spring Boot的jars,我们通常建议你使用一个支持依赖管理的构建工具(如,Maven或者Gradle)。

Maven Installation

使用Maven安装说明

Spring Boot is compatible with Apache Maven 3.2 or above.If you do not already have Mavent installed, you can follow the instructions at maven.apache.org.

Spring Boot兼容Apache Maven 3.2或更高版本。如果你还没有安装Mavent,你可以参考maven.apache.org上的安装说明。

Tip
建议

On many operating systems, Maven can be installed with a package manager. If you use OSX Homebrew, try brew install maven.Ubuntu users can run sudo apt-get install maven.Windows users with Chocolatey can run choco install maven from an elevated (administrator) prompt.

在大多数操作系统上,Maven可以使用安装包来安装。如果你使用的是OSX Homebrew 可以使用 brew install maven安装。Ubuntu用户可以运行sudo apt-get install maven安装。Windows Chocolatey用户可以在命令行提示符中运行choco install maven安装。

Spring Boot dependencies use the org.springframework.boot groupId.Typically, you Maven POM file inherits from the spring-boot-starter-parent project and declares dependencies to one or more "Starters".Spring Boot also provides an optional Maven plugin to create executable jars.

Spring Boot依赖使用org.springframework.boot groupId.通常,你的Maven POM文件继承spring-boot-starter-parent项目,并且声明了多个“Starters”依赖.Spring Boot提供了一个创建可执行的jars可选的Maven插件。

The following listing show a typical pom.xml file:

下面展示了一个典型的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"         
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Tip
建议

The spring-boot-starter-parent is a great way to use Spring Boot, but it might not be suitable all of the time. Sometimes you may need to inherit from a dfferent parent POM, or you might not like our default setting. In those cases, see the section called "Using Spring Boot without the Parent POM" for an alternative solution that uses an import scope.

spring-boot-starter-parent是一种使用Spring Boot比较好的方式,但是它并不是所有情况都适用。有时,你可能不喜欢我们的默认设置,你可以继承一个不同的父POM。请看“在不使用父POM情况下适用Spring Boot”章节提供的可选择的替代方案,它适用了import作用域。

Gradle Installation

使用Gradle安装说明

Spring Boot is compatible with Gradle 4. If you do not already have Gradle installed, you can follow the instructions at gradle.org.

Spring Boot兼容Gradle4。如果你还没有安装Gradle,你可以按照gradle.org的介绍来安装。

Spring Boot dependencies can be declared by using the org.springframework.boot group. Tyeically, your project declares dependencies to one or more "Starters". Spring Boot provides a useful Gradle plugin that can be used to simplify dependency declarations and to create executable jars.

Spring Boot依赖于org.framework.boot group。通常你的项目声明一个或多个“Starters”依赖。Spring boot提供了一个非常有用的Gradle插件来帮助你简化依赖声明并创建一个可执行的jars。

Gradle Wrapper

The Gradle Wrapper provides a nice way of "obtaining" Gradle when you need to build a project.It is a small script an library that you commit alongside your code to bootstrap the build process.See docs.gradle.org/4.2.1/userguide/gradle_wrapper.html for details.

当你需要构建一个项目时,Gradle Wrapper提供了一个非常方便的方式来获取Gradle。它是一个小脚本和类库,并随着你的代码启动时一起提交参与构建。更多信息请看docs.gradle.org/4.2.1/userguide/gradle_wrapper.html

The following example shows a typical build.gradle file:

下面的例子展示了一个典型的build.gradle文件:

plugins {
    id 'org.springframework.boot' version '2.0.1.RELEASE' id 'java'
}
jar {
    baseName = 'myproject'
    version = '0.0.1-SNAPSHOT'
}
repositories {
    jcenter ()
}
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Installing the Spring Boot CLI

安装Spring Boot CLI

The Spring Boot CLI(Command Line Interface) is a command line tool that you can use to quickly prototype with Spring. It lets you run Groovy scripts,which means that you have a familiar Java-like syntax without so much boilerplate code.

Spring Boot CLI(命令行接口)是一个帮助你快速使用标准Spring的命令行工具。它可以让你运行Groovy脚本,这意味着你可以有一个熟悉的类Java语法的样本代码。

You do not need to use the CLI to work with Spring Boot, but it is definitely the quickest way to get a Spring application off the ground.

你在适用Spring Boot工作时可以不需要使用CLI,但是它是显而易见快速使用Spring Boot应用程序的方式。

Manual Installation

手动安装

You can download the Spring CLI distribution from the Spring software repository:

你可以从Spring软件镜像来下载Spring CLI版本:

Cutting edge snapshot distributions are also available.

最新的分发快照也可以获取到。

Once downloaded, follow the INSTALL.txt instructions from the unpack archive.In summary, there is a Spring script(spring.bat for Windows) in a bin/ directory in the .zip file.Alternatively, you can use java -jar with the .jar file(the script helps you to be sure that the classpath is set correctly).

当你下载后,你可以参考从解压的压缩包里的INSTALL.txt介绍内容。总结,Spring脚本(Windows下spring.bat)在.zip文件里的bin目录下。另外一种选择, 你可以使用java -jar来运行.jar文件(这个脚本可以帮你确保你已经正确设置了classpath)。

Installation with SDKMAN!

使用SDKMAN!安装

SDKMAN!(The Software Development Kit Manager)can be used for managing multiple versions of various binary SDKs,including Groovy and the Spring Boot CLI.Get SDKMAN! from sdkman.io and install Spring Boot by using the following commands:

SDKMAN!(软件开发工具包管理器)可以帮你管理可执行SDKs的多个版本,它包含Groovy和Spring Boot CLI。可以使用下面的命令从sdkman.io获取SDKMAN!和安装Spring Boot CLI:

$ sdk install springboot
$ spring --version
Spring Boot v2.0.1.RELEASE

If you develop feature for the CLI and want easy access to the version you build, use the following commands:

如果你使用CLI开发功能,并很容易的访问构你构建的版本,你可以使用下面的命令:

$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-2.0.1.RELEASE-bin/spring-2.0.1.RELEASE/
$ sdk default springboot dev
$ spring --version
Spring CLI v2.0.1.RELEASE

The preceding instructions install a local instance of spring called the dev instance. It points at your target build location, so every time you rebuild Spring Boot, spring is up-to-date.

这里将会参考前面安装说明安装一个名为dev的本地Spring实例。他指向到你构建目录,可以在任何时间构建Spring Boot,来保持sping的最新版本。

You can see it by running the following command:

你可以参考下面的命令来运行:

$ sdk ls springboot

================================================================================
Available Springboot Versions
================================================================================
> + dev
* 2.0.1.RELEASE

================================================================================
+ - local version
* - installed
> - currently in use
================================================================================

OSX Homebrew Installation

使用OSX Homebrew安装

If you are on a Mac and use Homebrew, you can install the Spring Boot CLI by using the following commands:

如果你在Mac上使用Homebrew,你可以使用下面的命令来安装Spring Boot CLI:

$ brew tap pivotal/tap
$ brew install springboot
Homebrew installs spring to /usr/local/bin.
Note

If you do not see the formula, your installation of brew might be out-of-date.In that case, run brew update and try again.

如果你很少使用它,你的brew安装可能已经过期。这时,你可以运行brew update并重试一次试试看。

MacPorts Installation

使用MacPorts安装说明

If you are on a Mac and use MacPorts, you can install the Spring Boot CLI by using the following command:

如果你在Mac上使用MacPorts,你可以使用下面的命令来安装Spring Boot CLI:

$ sudo port install spring-boot-cli

Command-line Completion

命令行实现

The Spring Boot CLI includes scripts that provide command completion for the BASH and ZSH shells.You can source the script(also named spring) in any shell or put it in your personal or system-wide bash completion initialization.On a Debian system, the system-wide scripts are in /shell-completion/bash and all scripts in that directory are executed when a new shell starts.For example, to run the script manually if you have installed by using SDKMAN!, use the following command:

Spring Boot CLI包含了提供了在BASH和ZSH shells中使用的命令脚本。你可以在任何shell中使用的脚本(通常它的名字是spring)或者你可以把它放在个人或系统的bash里完成初始化工作。在Debian系统里,系统脚本通常在/shell-completion/bash里,当shells启动时,所有脚本都可以被执行。比如,当你想使用已经安装的SDKMAN!去手动运行脚本时,可使用如下的命令:

$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
grab help jar run test version
Note

If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line completion scripts are automatically registered wit your shell.

如果你使用Homebrew或者MacPorts安装Spring Boot CLI,命令行脚本将会自动记录你使用的shells。

Quick-start Spring CLI Example

Spring CLI快速入门示例

You can use the following web application to test your installation.To start, create a file called app.groovy, as follows:

你可以使用下面的web应用程序来测试你的安装情况。首先,创建一个叫app.groovy文件,如下:

@RestController
class ThisWileActuallyRun {
    @RequestMapping("/")
    String home() {
        "Hello World!"
    }
}

Then run it from a shell, as follows:

然后,再shell里执行下面的命令:

$ spring run app.groovy
Note

The first run of your application is slow, as dependencies are downloaded. Subsequent runs are much quicker.

第一次运行你的应用程序会比较慢,因为要下载依赖包。后续在运行就非常快了。

Open http://localhost:8080/ in your favorite web browser. You should see the following output:

在你的浏览器里打开http://localhost:8080/。你将会看到如下的输出:

Hello World!

Upgrading from an Earlier Version of Spring Boot

更新老版本的Spring Boot

If you are upgrading from an earlier release of Spring Boot, check the "migration guide" on the project wiki that provides detailed upgrade instructions. Check also the "release notes" for a list of "new and noteworthy" features for each release.

如果你从早期版本升级Spring Boot时,可以参考“migration guide”项目wiki提供的详细的升级说明。也可以参考“release notes”列举的每次发布的特性列表。

To upgrade an existing CLI installation, use the appropriate package manager command (for example, brew upgrade) or, if you manually installed the CLI, follow the standard instructions, remembering to update your PATH environment variable to remove any older references.

升级已经安装的CLI,可以使用对应的包管理命令(比如,brew upgrade),如果你是手动安装的CLI,可以参考标准参考说明去做,移除任何旧版本参考文献记住更新你的PATH环境变量。