Spring Boot - 构建系统

在 Spring Boot 中,选择构建系统是一项重要任务。 我们推荐 Maven 或 Gradle,因为它们为依赖管理提供了良好的支持。 Spring 不能很好地支持其他构建系统。


依赖管理

Spring Boot 团队提供了一个依赖项列表,以支持其每个版本的 Spring Boot 版本。 您无需在构建配置文件中提供依赖项的版本。 Spring Boot 会根据 release 自动配置依赖版本。 请记住,当您升级 Spring Boot 版本时,依赖项也会自动升级。

注意 − 如果要指定依赖的版本,可以在配置文件中指定。 但是,Spring Boot 团队强烈建议不需要指定依赖的版本。


Maven 依赖

对于 Maven 配置,我们应该继承 Spring Boot Starter 父项目来管理 Spring Boot Starters 依赖项。 为此,我们可以简单地继承 pom.xml 文件中的起始父级,如下所示。

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.8.RELEASE</version>
</parent>

我们应该为 Spring Boot Parent Starter 依赖项指定版本号。 那么对于其他的starter依赖,我们就不需要指定Spring Boot的版本号了。 观察下面给出的代码 −

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

Gradle 依赖

我们可以将 Spring Boot Starters 依赖项直接导入到 build.gradle 文件中。 我们不需要像 Maven for Gradle 这样的 Spring Boot 启动父依赖项。 观察下面给出的代码 −

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

同样,在 Gradle 中,我们不需要为依赖项指定 Spring Boot 版本号。 Spring Boot 会根据版本自动配置依赖。

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
}