Spring Boot Gradle Plugin – Spring Boot Gradle Plugin

最后修改: 2018年 4月 10日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

The Spring Boot Gradle plugin helps us manage Spring Boot dependencies, as well as package and run our application when using Gradle as a build tool.

Spring Boot Gradle插件帮助我们管理Spring Boot的依赖关系,以及在使用Gradle作为构建工具时打包和运行我们的应用程序。

In this tutorial, we’ll discuss how we can add and configure the plugin, and then we’ll see how to build and run a Spring Boot project.

在本教程中,我们将讨论如何添加和配置该插件,然后我们将看到如何构建和运行一个Spring Boot项目。

2. Build File Configuration

2.构建文件配置

First, we need to add the Spring Boot plugin to our build.gradle file by including it in our plugins section:

首先,我们需要在build.gradle文件中加入Spring Boot插件,将其纳入plugins部分。

plugins {
    id "org.springframework.boot" version "2.0.1.RELEASE"
}

If we’re using a Gradle version earlier than 2.1 or we need dynamic configuration, we can add it like this instead:

如果我们使用的Gradle版本早于2.1,或者我们需要动态配置,我们可以像这样添加它。

buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath(
          "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'org.springframework.boot'

3. Packaging Our Application

3.包装我们的应用程序

We can package our application to an executable archive (jar or war file) by building it using the build command:

我们可以通过使用build命令将我们的应用程序打包成可执行档案(jar或war文件):

./gradlew build

As a result, the generated executable archive will be placed in the build/libs directory.

因此,生成的可执行档案将被放置在build/libs目录下。

If we want to generate an executable jar file, then we also need to apply the java plugin:

如果我们想生成一个可执行的jar文件,那么我们还需要应用java插件。

apply plugin: 'java'

On the other hand, if we need a war file, we’ll apply the war plugin:

另一方面,如果我们需要一个war文件,我们将应用war插件。

apply plugin: 'war'

Building the application will generate executable archives for both Spring Boot 1.x and 2.x. However, for each version, Gradle triggers different tasks.

构建应用程序将为Spring Boot 1.x和2.x生成可执行文件。然而,对于每个版本,Gradle会触发不同的任务。

Next, let’s have a closer look at the build process for each Boot version.

接下来,让我们仔细看看每个Boot版本的构建过程。

3.1. Spring Boot 2.x

3.1.Spring Boot 2.x

In Boot 2.x, the bootJar and bootWar tasks are responsible for packaging the application.

在 Boot 2.x 中,bootJarbootWar 任务负责打包应用程序。

The bootJar task is responsible for creating the executable jar file. This is created automatically once the java plugin is applied.

bootJar任务负责创建可执行的jar文件。一旦应用了java插件,该文件就会自动创建。

Let’s see how we can execute the bootJar task directly:

让我们看看我们如何直接执行bootJar任务。

./gradlew bootJar

Similarly, bootWar generates an executable war file and gets created once the war plugin is applied.

同样,bootWar会生成一个可执行的战争文件,一旦应用war插件就会被创建。

We can execute the bootWar task using:

我们可以使用以下方法执行bootWar任务。

./gradlew bootWar

Note that for Spring Boot 2.x, we need to use Gradle 4.0 or later.

请注意,对于Spring Boot 2.x,我们需要使用Gradle 4.0或更高版本。

We can also configure both tasks. For example, let’s set the main class by using the mainClassName property:

我们也可以对这两项任务进行配置。例如,让我们通过使用mainClassName属性来设置主类。

bootJar {
    mainClassName = 'com.baeldung.Application'
}

Alternatively, we can use use the same property from the Spring Boot DSL:

另外,我们也可以使用Spring Boot DSL的相同属性。

springBoot {
    mainClassName = 'com.baeldung.Application'
}

3.2. Spring Boot 1.x

3.2.Spring Boot 1.x

With Spring Boot 1.x, bootRepackage is responsible for creating the executable archive (jar or war file depending on the configuration.

在Spring Boot 1.x中,bootRepackage负责创建可执行归档文件 (jarwar文件,取决于配置。

We can execute the bootRepackage task directly using:

我们可以直接使用bootRepackage任务执行。

./gradlew bootRepackage

Similar to the Boot 2.x version, we can add configurations to the bootRepackage task in our build.gradle:

与Boot 2.x版本类似,我们可以在build.gradle的bootRepackage任务中添加配置:

bootRepackage {
    mainClass = 'com.example.demo.Application'
}

We can also disable the bootRepackage task by setting the enabled option to false:

我们也可以通过将enabled选项设置为false来禁用bootRepackage任务。

bootRepackage {
    enabled = false
}

4. Running Our Application

4.运行我们的应用程序

After building the application, we can just run it by using the java -jar command on the generated executable jar file:

在构建应用程序后,我们可以通过在生成的可执行jar文件上使用java -jar命令来运行它。

java -jar build/libs/demo.jar

Spring Boot Gradle plugin also provides us with the bootRun task which enables us to run the application without the need to build it first:

Spring Boot Gradle插件还为我们提供了bootRun任务,使我们能够运行应用程序,而无需先构建它。

./gradlew bootRun

The bootRun task can be simply configured in build.gradle.

bootRun任务可以在build.gradle.中简单配置。

For example, we can define the main class:

例如,我们可以定义主类。

bootRun {
    main = 'com.example.demo.Application'
}

5. Relation With Other Plugins

5.与其他插件的关系

5.1. Dependency Management Plugin

<5.1.依赖性管理插件[/strong]

For Spring Boot 1.x, it used to apply the dependency management plugin automatically. This would import the Spring Boot dependencies BOM and act similar to dependency management for Maven.

对于Spring Boot 1.x,它曾经自动应用依赖性管理插件。这将导入Spring Boot的依赖性BOM,其作用类似于Maven的依赖性管理。

But since Spring Boot 2.x, we need to apply it explicitly in our build.gradle if we need this functionality:

但从Spring Boot 2.x开始,如果我们需要这个功能,我们需要在build.gradle中明确地应用它。

apply plugin: 'io.spring.dependency-management'

5.2. Java Plugin

5.2. Java插件

When we apply the java plugin, the Spring Boot Gradle plugin takes multiple actions like:

当我们应用java插件时,Spring Boot Gradle插件会采取多种行动,比如。

  • creating a bootJar task, which we can use to generate an executable jar file
  • creating a bootRun task, which we can use to run our application directly
  • disabling jar task

5.3. War Plugin

5.3.战争插件

Similarly, when we apply the war plugin, that results in:

同样地,当我们应用war插件时,这导致了。

  • creating the bootWar task, which we can use to generate an executable war file
  • disabling the war task

6. Conclusion

6.结论

In this quick tutorial, we learned about the Spring Boot Gradle Plugin and its different tasks.

在这个快速教程中,我们了解了Spring Boot Gradle插件和它的不同任务。

Also, we discussed how it interacts with other plugins.

此外,我们还讨论了它与其他插件的互动方式。