梦想还是要有的,万一忘了咋办?

0%

Gradle多模块

目录

  • 目录结构
  • 完整示例

目录结构

1
2
3
4
5
6
7
8
9
10
11
12
├── build.gradle
├── settings.gradle
├── spring-config
├── spring-config-client
│   ├── build.gradle
│   └── src
├── spring-config-server
│   ├── build.gradle
│   └── src
└── spring-event
├── build.gradle
└── src

完整示例

./build.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
49
50
51
52
53
54
buildscript {
ext {
springBootVersion = '2.4.4'
springCloudVersion = '2020.0.2'
}
repositories {
// 本地maven仓库
mavenLocal()
maven { url = 'http://maven.aliyun.com/nexus/content/groups/public/' }
//和maven中央仓库一样的另一个依赖管理仓库,主要是java
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
allprojects {
apply plugin: 'idea'
apply plugin: 'java-library'
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot' //使用springboot插件
apply plugin: 'io.spring.dependency-management' //版本管理插件
apply plugin: 'application' // 识别mainClassName 插件
//如果是多模块项目,需要指定一个程序入口,必须指定,否则无法build,单模块可以不用指定
mainClassName = 'cm.hou.blogweb.BlogWebApplication'
// java编译的时候缺省状态下会因为中文字符而失败
[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
// 配置所有子模块的依赖仓库地址
repositories {
// 本地maven仓库
mavenLocal()
maven { url = 'http://maven.aliyun.com/nexus/content/groups/public/' }
//和maven中央仓库一样的另一个依赖管理仓库,主要是java
jcenter()
}
//所有子模块共有依赖
dependencies {
// gradle5.0版本以上引入需要这种形式
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
implementation 'org.codehaus.groovy:groovy'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}

./settings.gradle

1
2
3
4
rootProject.name = 'learn-spring-cloud'
include "spring-event"
include "spring-config-server"
include "spring-config-client"

./spring-event/build.gradle

1
2
3
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
}

./spring-config-server/build.gradle

1
2
3
4
5
6
7
dependencies {
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile project(":spring-event")
}