前言
最开始,我创建了一个空的项目,并在该项目下依次创建了3个模块。
但是我发现,这几个模块引入的依赖绝大多数都是相同的
于是,我灵光一闪(其实是在最初构建项目时的没想到),开始着手进行改造
但由于也是第一次用Gradle,还相对生疏不少,踩了一些坑,故留此记录
改造开始
- 在项目根目录下,创建settings.gradle及build.gradle
- (根目录)setting.gradle配置内容
pluginManagement {
repositories {
maven { url 'https://repo.spring.io/milestone' }
gradlePluginPortal()
}
}
rootProject.name = 'Server-main'
include ':server-1'
include ':server-2'
include ':server-3'
- (根目录)build.gradle配置内容
buildscript {
ext {
springBootVersion = '3.1.0-M1'
}
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/jcenter/' }
maven { url "https://mvn.getui.com/nexus/content/repositories/releases/" }
maven {
url 'http://mirrors.huaweicloud.com/repository/maven/'
allowInsecureProtocol = true
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 全局项目配置
allprojects {
group = 'com.example'
version = '0.0.1-SNAPSHOT'
apply plugin:'idea'
}
// 子模块配置(可以理解为子模块的全局配置)
subprojects {
apply plugin:'java'
sourceCompatibility = '17'
targetCompatibility = '17'
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/jcenter/' }
maven { url "https://mvn.getui.com/nexus/content/repositories/releases/" }
maven {
url 'http://mirrors.huaweicloud.com/repository/maven/'
allowInsecureProtocol = true
}
mavenCentral()
}
// 统一版本管理
ext {
}
// 统一依赖管理
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.0.4'
testImplementation 'junit:junit:4.12'
}
tasks.named('test') {
useJUnitPlatform()
}
}
- 子模块需要删除setting.gradle,并调整build.gradle的内容,下以server-1举例,其他子模块服务类同
archivesBaseName = "server-1"
dependencies {
// 专属于这个模块的依赖
...
// 引入server-2 | 常见应用于项目统一工具包的引入
compile project(":server-2")
}
// 动态移除对kotlin构建插件的权限,避免子模块构建报错
tasks.register("prepareKotlinBuildScriptModel") {}
写在最后
有关gradle的使用,我正在研究,后期会单独写一个整合gradle配置的记录