曹耘豪的博客

Spring之Gradle使用dependency-management和mavenBom管理Spring依赖

主Gradle文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
plugins {
id 'java'
// https://plugins.gradle.org/plugin/org.springframework.boot
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.1.0'
}

subprojects {

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

repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
mavenLocal()
mavenCentral()
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:2.6.2"
}

dependencies { // 其他依赖
dependency 'org.mybatis:mybatis:3.5.11'
dependency 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.5.11'
dependency 'org.apache.kafka:kafka-clients:3.4.0'
dependency 'com.mysql:mysql-connector-j:8.0.33'
}
}

dependencies {
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly "org.projectlombok:lombok:1.18.28"
annotationProcessor "org.projectlombok:lombok:1.18.28"

testCompileOnly "org.projectlombok:lombok:1.18.28"
testAnnotationProcessor "org.projectlombok:lombok:1.18.28"
}

}

子Spring项目Gradle引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
plugins {
id 'java'
id 'org.springframework.boot'
}

version '1.0.0'

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
}

test {
useJUnitPlatform()
}

内置lombok

1
2
3
dependencies {
implementation "org.projectlombok:lombok"
}
   / 
  ,