2.1.2 Spring Boot 项目构建过程解析
Spring Boot 应用程序的大部分内容都与其他 Spring 应用程序没有什么区别,与其他 Java 应用程序也没什么两样,因此构建一个 Spring Boot 应用程序和构建其他 Java 应用程序的过程类似。你可以选择 Gradle 或 Maven 作为构建工具,描述构建说明文件的方法和描述非 Spring Boot 应用程序的方法相似。但是,Spring Boot 在构建过程中耍了些小把戏,在此需要做个小小的说明。
Spring Boot 为 Gradle 和 Maven 提供了构建插件,以便辅助构建 Spring Boot 项目。代码清单 2-3 是 Initializr 创建的 build.gradle 文件,其中应用了 Spring Boot 的 Gradle 插件。
代码清单 2-3 使用 Spring Boot 的 Gradle 插件
buildscript {
ext {
springBootVersion = `1.3.0.RELEASE`
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin: ${springBootVersion}") ←---依赖 Spring Boot 插件
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot' ←---运用 Spring Boot 插件
jar {
baseName = 'readinglist'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") ←---起步依赖
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
runtime("com.h2database:h2")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
另一方面,要是选择用 Maven 来构建应用程序,Initializr 会替你生成一个 pom.xml 文件,其中使用了 Spring Boot 的 Maven 插件,如代码清单 2-4 所示。
代码清单 2-4 使用 Spring Boot 的 Maven 插件及父起步依赖
<?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.manning</groupId>
<artifactId>readinglist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ReadingList</name>
<description>Reading List Demo</description>
<parent> ←---从 spring-boot-starterparent 继承版本号
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{springBootVersion}</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies> ←---起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<start-class>readinglist.Application</start-class>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin> ←---运用 Spring Boot 插件
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
无论你选择 Gradle 还是 Maven,Spring Boot 的构建插件都对构建过程有所帮助。你已经看到过如何用 Gradle 的 bootRun
任务来运行应用程序了。Spring Boot 的 Maven 插件与之类似,提供了一个 spring-boot:run
目标,如果你使用 Maven,它能实现相同的功能。
构建插件的主要功能是把项目打包成一个可执行的超级 JAR(uber-JAR),包括把应用程序的所有依赖打入 JAR 文件内,并为 JAR 添加一个描述文件,其中的内容能让你用 java -jar
来运行应用程序。
除了构建插件,代码清单 2-4 里的 Maven 构建说明中还将 spring-boot-starter-parent 作为上一级,这样一来就能利用 Maven 的依赖管理功能,继承很多常用库的依赖版本,在你声明依赖时就不用再去指定版本号了。请注意,这个 pom.xml 里的 <dependency>
都没有指定版本。
遗憾的是,Gradle 并没有 Maven 这样的依赖管理功能,为此 Spring Boot Gradle 插件提供了第三个特性,它为很多常用的 Spring 及其相关依赖模拟了依赖管理功能。其结果就是,代码清单 2-3 的 build.gradle 里也没有为各项依赖指定版本。
说起依赖,无论哪个构建说明文件,都只有五个依赖,除了你手工添加的 H2 之外,其他的 Artifact ID 都有 spring-boot-starter-
前缀。这些都是 Spring Boot 起步依赖,它们都有助于 Spring Boot 应用程序的构建。让我们来看看它们究竟都有哪些好处。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论