Article directory
-
- I. Introduction
- 2. Download Gradle
- 3. Gradle mirror source - global level configuration
- Four. Configure Gradle wrapper-project-level configuration
- 5. Gradle's support for testing
- 5. Life cycle
- 6. Getting started with Gradle tasks
- Seven. Dependencies dependency introduction
- Eight. Gradle integrates multi-module SpringBoot
- Nine. Gradle integrates microservice SpringCloud
I. Introduction
Gradle has prominent version incompatibility issues. So pay attention SpringBoot对Gradle的要求
, and IDEA对Gradle的要求
.
The maximum version number of gradle is stipulated in IDEA's plugins/lib/gradle. It is 6.7 in 2022.2.1 and 7.4 in 2022.3.2. Check the rest by
yourself Version control of this location. However, the version number specified by the idea can be changed.
Check the compatibility between SpringBoot and Gradle
2. Download Gradle
Gradle official website address
After downloading the full version and decompressing it,
configure the Gradle repository with system variables
键必须为GRADLE_USER_HOME
System variable configuration Gradle global system variable gradle bin directory
verify gradle -v
3. Gradle mirror source - global level configuration
Gradle uses maven to locate the mirror address reference: Alibaba Cloud Cloud Effect Maven
The global configuration creates a new file with gradle as the suffix in the init.d directory of Gradle.
allprojects {
repositories {
maven {
name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
maven {
name "google" ; url 'https://maven.aliyun.com/repository/google' }
mavenCentral()
}
buildscript {
repositories {
maven {
name "Alibaba" ; url 'https://maven.aliyun.com/repository/public' }
maven {
name "gradle-plugin" ; url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven {
name "spring-plugin" ; url 'https://maven.aliyun.com/repository/spring-plugin' }
maven {
name "M2" ; url 'https://plugins.gradle.org/m2/' }
}
}
}
Four. Configure Gradle wrapper-project-level configuration
Solve problems such as inconsistent Gradle environments and incompatible ideas when projects are developed on multiple computers.
The Gradle command calls the local gradle script. In the project, gradlew should be run to call the wrapper script in the project
gradlew wrapper --gradle-version=6.7
when executed
gradlew.bat classes
gradlew.bat test
gradlew.bat build
Wait, will download gradle related content
5. Gradle's support for testing
test{
useJUnitPlatform() // 支持对junit5测试
}
5. Life cycle
The life cycle of a Gradle project is divided into three phases: Initialization -> Configuration -> Execution. Each phase has its own responsibilities, as shown in the following figure: Initialization executes the initialization
script only once.
Configuration executes loading the parent script first, and then child script, then grandchild script
5.1 settings file
6. Getting started with Gradle tasks
6.1 Task Behavior
def map = new HashMap<String,Object>();
//action属性可以设置为闭包,设置task自身的行为
map.put("action",{
println "task one.."})
task (map,"task1"){
// 任务的配置段:在配置阶段执行
println "最先执行"
// 任务的行为:在执行阶段执行,doFirst会在doLast执行之前执行
doFirst {
println "task1 doFirst"
}
doLast {
println "task1 doLast"
}
}
task.doFirst {
println "task1 doFirst outer"
}
task.doLast {
println "task1 doLast outer"
}
6.2 Task Dependency
Seven. Dependencies dependency introduction
Unless it involves multi-module dependencies, in order to avoid repeated dependencies, we will use api. In other cases, we prefer implementation. Having a large number of api dependencies will significantly increase the build time.
7.1 Dependency conflicts and solutions
Dependency conflict refers to "during the compilation process, if there are multiple versions of a certain dependency, which one should the build system choose to build", as shown below: A, B, and C are all
local sub-project modules, and log4j is a remote rely.
When compiling: B uses the 1.4.2 version of log4j, C uses the 2.2.4 version of log4j, there is no conflict between B and C
When packaging: only one version of the code is finally packaged into the final jar |war package corresponding to A , for Gradle there is a conflict here
By default, Gradle will use the latest version of the jar package [considering that the new version of the jar package is generally backward compatible]. In actual development, it is recommended to use the official solution. Of course, in addition to this, Gradle also provides us with a series of methods to resolve dependency conflicts: exclude removes a dependency, does not allow dependency transfer, and forces a certain version to be used.
● Exclude excludes a dependency
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' implementation('org.hibernate:hibernate-core:3.6.3.Final'){
//排除某一个库(slf4j)依赖:如下三种写法都行
exclude group: 'org.slf4j' exclude module: 'slf4j-api'
exclude group: 'org.slf4j',module: 'slf4j-api'
}
//排除之后,使用手动的引入即可。implementation 'org.slf4j:slf4j-api:1.4.0'
}
● Do not allow dependency transfer When adding dependencies, if you set transitive to false, it means that dependency transfer is turned off. i.e. all internal dependencies will not be added to the compile and runtime classpath.
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' implementation('org.hibernate:hibernate-core:3.6.3.Final'){
//不允许依赖传递,一般不用
transitive(false)
}
//排除之后,使用手动的引入即可implementation 'org.slf4j:slf4j-api:1.4.0'
}
● Force a version
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' implementation('org.hibernate:hibernate-core:3.6.3.Final')
//强制使用某个版本!!【官方建议使用这种方式】
implementation('org.slf4j:slf4j-api:1.4.0!!')
//这种效果和上面那种一样,强制指定某个版本implementation('org.slf4j:slf4j-api:1.4.0'){
version{
strictly("1.4.0")
}
}
}
Eight. Gradle integrates multi-module SpringBoot
SpringBoot official website integration instructions
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
module aggregation ./settings.gradle
新建.gradle后缀的文件做版本管理
config.gradle
ext {
lombokVerison = '1.18.24'
mybatisPlusVersion = '3.5.2'
druidVersion = '1.2.8'
mysqlVersion = '8.0.32'
commonsVersion = '3.12.0'
}
./build.gradle抽取公共配置, 编码,环境,镜像源
// 加载构建期需要的插件
buildscript {
repositories {
maven {
name "Alibaba"; url 'https://maven.aliyun.com/repository/public' }
maven {
name "google"; url 'https://maven.aliyun.com/repository/google' }
maven {
name "gradle-plugin"; url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven {
name "spring-plugin"; url 'https://maven.aliyun.com/repository/spring-plugin' }
maven {
name "M2"; url 'https://plugins.gradle.org/m2/' }
mavenCentral()
}
// 维护插件版本
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:2.6.3')
}
}
// 导入插件
plugins {
id 'java-library'
}
group 'com.vector'
version '1.0-SNAPSHOT'
// 读取gradle版本配置
apply from: 'config.gradle'
// 对所有子模块做统一管理
subprojects {
//添加插件 目前Gradle版本不支持在allprojects下声明plugins,使用的是旧的写法
apply plugin: 'java-library'
apply plugin: 'org.springframework.boot' //维护springboot版本号,不单独使用,和下面两个插件一起用
apply plugin: 'io.spring.dependency-management'// 相当于<dependencyManagement>版本管理
//基本JDK配置sourceCompatibility = 1.8
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding "UTF-8"
compileTestJava.options.encoding "UTF-8"
tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
}
// SpringBoot Plugin生效的非常关键的设置
// 主启动类位置
bootJar {
mainClass.set('org.vector.Main')
}
//依赖的配置:设置通用的依赖
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVerison
}
test {
useJUnitPlatform()
}
}
project("module01") {
apply plugin: 'java-library'//支持api
dependencies {
compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVerison
}
}
project("module02") {
apply plugin: 'java-library'//支持api
dependencies {
// implementation不会进行依赖传递. api可以进行依赖传递
api project(':module01')
// mp持久化框架
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: mybatisPlusVersion
// druid连接池
implementation group: 'com.alibaba', name: 'druid-spring-boot-starter', version: druidVersion
// mysql数据库
implementation group: 'mysql', name: 'mysql-connector-java', version: mysqlVersion
}
}
project("module03") {
dependencies {
// 不需要在继续依赖传递
implementation project(':module02')
// spring-boot-starter-web
implementation 'org.springframework.boot:spring-boot-starter-web'
//spring-boot-starter-test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// spring-boot-starter-aop
implementation 'org.springframework.boot:spring-boot-starter-aop'
//spring-boot-starter-data-redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// commons工具类
implementation group: 'org.apache.commons', name: 'commons-lang3', version: commonsVersion
}
}
那么可以自行探寻更优雅的写法.
Nine. Gradle integrates microservice SpringCloud
项目结构
创建version.gradle
ext {
version = [
"lombokVerison" : "1.18.24",
"mybatisPlusVersion": "3.5.2",
"druidVersion" : "1.2.8",
"mysqlVersion" : "8.0.32",
"commonsVersion" : "3.12.0"
]
// 公共依赖
dependencies = [
"lombok" : "org.projectlombok:lombok:${
version.lombokVerison}",
"druid" : "com.alibaba:druid-spring-boot-starter:${
version.druidVersion}",
"mysql" : "mysql:mysql-connector-java:${
version.mysqlVersion}",
"common-lang3": "org.apache.commons:commons-lang3:${
version.commonsVersion}",
"mybatisPlus" : "com.baomidou:mybatis-plus-boot-starter:${
version.mybatisPlusVersion}"
]
}
./build.gradle
description '微服务父工程'
//构建Gradle脚本自身需要的资源,可以声明的资源包括依赖项、第三方插件、maven仓库地址等。
buildscript {
ext {
springBootVersion = '2.6.3'
springCloudversion = '2021.0.1'
springCloudAlibabaVersion = '2021.1'
springBootGradlePlugin = '2.6.3'
}
repositories {
maven {
name "Alibaba"; url 'https://maven.aliyun.com/repository/public' }
maven {
name "google"; url 'https://maven.aliyun.com/repository/google' }
maven {
name "gradle-plugin"; url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven {
name "spring-plugin"; url 'https://maven.aliyun.com/repository/spring-plugin' }
maven {
name "M2"; url 'https://plugins.gradle.org/m2/' }
mavenCentral()
}
// 维护插件版本
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${
springBootGradlePlugin}")
}
}
group 'com.vector'
version '1.0-SNAPSHOT'
// 读取gradle版本配置
apply from: 'version.gradle'
// 对所有子模块做统一管理
subprojects {
//添加插件 目前Gradle版本不支持在allprojects下声明plugins,使用的是旧的写法
apply plugin: 'java-library'
apply plugin: 'org.springframework.boot' //维护springboot版本号,不单独使用,和下面两个插件一起用
apply plugin: 'io.spring.dependency-management'// 相当于<dependencyManagement>版本管理
// 将配置信息加载进声明中.版本控制
dependencyManagement{
dependencies {
for(depJar in rootProject.ext.dependencies){
dependency depJar.value
}
}
imports {
// spring-cloud-dependencies
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${
springCloudversion}"
// spring-cloud-alibaba-dependencies
mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${
springCloudAlibabaVersion}"
}
}
//依赖的配置:设置通用的依赖
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
// spring-boot-starter
implementation 'org.springframework.boot:spring-boot-starter'
// spring-cloud-starter-alibaba-nacos-discovery
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
// spring-cloud-starter-alibaba-nacos-config
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
// spring-cloud-alibaba-sentinel-gateway
implementation 'com.alibaba.cloud:spring-cloud-alibaba-sentinel-gateway'
}
//基本JDK配置sourceCompatibility = 1.8
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding "UTF-8"
compileTestJava.options.encoding "UTF-8"
tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
}
test {
useJUnitPlatform()
}
}
project("module01") {
description ("微服务模块1")
apply plugin: 'java-library'//支持api
dependencies {
//lombok
api "org.projectlombok:lombok"
api "mysql:mysql-connector-java"
api "org.apache.commons:commons-lang3"
}
}
project("module02") {
description ("微服务模块2")
apply plugin: 'java-library'//支持api
dependencies {
// mp持久化框架
implementation 'com.baomidou:mybatis-plus-boot-starter'
// druid-spring-boot-starter
implementation 'com.alibaba:druid-spring-boot-starter'
// mysql数据库
implementation 'mysql:mysql-connector-java'
}
}
project("module03") {
description ("微服务模块3")
dependencies {
// spring-boot-starter-web
implementation 'org.springframework.boot:spring-boot-starter-web'
//spring-boot-starter-test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// spring-boot-starter-aop
implementation 'org.springframework.boot:spring-boot-starter-aop'
//spring-boot-starter-data-redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// commons工具类
implementation 'org.apache.commons:commons-lang3'
}
}