Spring Boot Gradle 插件在Gradle 提供Spring Boot 支持。它允许您打包可执行jar 或war 归档文件,运行SpringBoot 应用程序,并使用Spring-Boot-dependencies 提供的依赖管理。相关文档请参考:
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application
① 创建工程
如下所示,首先使用Spring Initializr创建项目,然后通过Idea的Settings修改gradle。
这里Server URL :start.spring.io还可以换成阿里云的 https://start.aliyun.com/。
② 引入插件
plugins {
id 'org.springframework.boot' version '2.3.7.RELEASE'
//维护springboot版本号,不单独使用,和下面两个插件一起用
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
//进行依赖管理,在引入其它boot依赖时省略版本号、解决jar包冲突问题
id 'java'
}
③ 引入所需要的依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web' //省略版本,原生bom支持,插件management提供
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
④ 运行项目
要想运行当前Springboot 项目,直接执行gradle bootRun
指令或者idea 右侧按钮即可。
当然如果想让当前项目打成可执行jar 包,只需执行: gradle bootJar
指令即可。Cloud 项目创建也可以借助于脚手架创建,与Boot 项目类似。
⑤ 拓展spring-boot-gradle-plugin 插件
如下所示,这里我们可以在build.gradle中指定仓库与依赖。
//构建Gradle脚本自身需要的资源,可以声明的资源包括依赖项、第三方插件、maven仓库地址等。
buildscript {
ext {
springBootVersion = '2.2.1.RELEASE'
springCloudVersion = 'Hoxton.RELEASE'
springCloudAlibabaVersion = '0.2.2.RELEASE'
}
//设置仓库
repositories {
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://repo.spring.io/milestone'}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
⑥ 多module工程
如下图所示,工程包括多个module。其实与传统多module工程并无区别,同样是创建工程后创建module。这里说明一下Gradle的配置。
如下是settings.gradle配置文件示例:
rootProject.name = 'meinian-parent'
include 'meinian-bean'
include 'meinian-dao'
include 'meinian-service'
include 'meinian-web'
include 'meinian-mobile-web'
如下是build.gradle配置文件示例:
//父工程的坐标
group 'com.jane'
version '1.0-SNAPSHOT'
//配置全局, 包括root项目和子项目---如果配置了allprojects ,
//那么subprojects 中就无需配置编码和仓库
allprojects {
group 'com.jane'
version '1.0-SNAPSHOT'
//配置编码格式
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
//设置仓库
repositories {
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://repo.spring.io/milestone'}
}
}
//对所有子工程设置统一配置
subprojects {
//添加插件
apply plugin: 'java'
//基本JDK配置
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding "UTF-8"
compileTestJava.options.encoding "UTF-8"
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
group 'com.jane'
version '1.0-SNAPSHOT'
repositories {
mavenLocal()
maven {url "https://maven.aliyun.com/repository/public"}
maven {url "https://maven.aliyun.com/repository/central"}
maven {url "https://maven.aliyun.com/repository/google"}
maven {url "https://maven.aliyun.com/repository/spring"}
mavenCentral()
}
//依赖的配置:设置通用的依赖
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
implementation 'log4j:log4j:1.2.17'
}
test {
useJUnitPlatform()
}
}
//为每个子工程设置依赖
project("meinian-bean"){
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.24'
}
}
project("meinian-dao"){
apply plugin: 'java-library'//支持api
dependencies {
api project(':meinian-bean')
implementation 'org.mybatis:mybatis-spring:1.2.3'
implementation 'com.alibaba:druid:1.0.15'
implementation 'org.mybatis:mybatis:3.3.0'
implementation 'mysql:mysql-connector-java:5.1.36'
}
}
project("meinian-service"){
apply plugin: 'java-library'//支持api
dependencies {
api project(':meinian-dao')
implementation 'org.springframework:spring-web:4.1.7.RELEASE'
implementation 'org.springframework:spring-test:4.0.5.RELEASE'
implementation 'org.springframework:spring-jdbc:4.1.7.RELEASE'
implementation 'org.aspectj:aspectjweaver:1.8.6'
}
}
project("meinian-web"){
apply plugin: 'war'
dependencies {
implementation project(':meinian-service')
implementation 'org.springframework:spring-webmvc:4.1.7.RELEASE'
implementation "com.fasterxml.jackson.core:jackson-databind:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.2.3"
compileOnly 'javax.servlet:servlet-api:2.5'
implementation 'jstl:jstl:1.2'
}
}
project("meinian-mobile-web"){
apply plugin: 'war'
dependencies {
//implementation project(':meinian-bean')
implementation project(':meinian-service')
implementation 'org.springframework:spring-webmvc:4.1.7.RELEASE'
implementation "com.fasterxml.jackson.core:jackson-databind:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.2.3"
compileOnly 'javax.servlet:servlet-api:2.5'
implementation 'jstl:jstl:1.2'
}
}