配置镜像
修改 gradle>wrapper>gradle-wrapper.properties
distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.6-all.zip
配置父项目
使用 subprojects
编码问题处理
[compileJava, compileTestJava, javadoc].options.encoding = 'UTF-8'
依赖版本控制
增加使用插件
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
子项目排除依赖
使用configuration
configurations {
all*.exclude group: 'javax.servlet'
}
项目之间依赖
使用compile关键字,不过目前Gradle版本对于compile不再支持了,可以使用api关键字或implementation关键字
dependencies {
implementation project(':sb-common')
}
完整示例
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://repo.spring.io/libs-milestone' }
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:3.2.2'
}
}
group project_group
version project_version
def env = System.getProperty("env") ?: 'dev'
subprojects {
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = project_jdk
targetCompatibility = project_jdk
repositories {
mavenLocal()
maven {
allowInsecureProtocol = true
url = 'https://maven.aliyun.com/repository/public'
}
maven {
allowInsecureProtocol = true
url = 'https://repo.spring.io/libs-milestone'
}
mavenCentral()
}
//配置公共依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-devtools'
}
sourceSets {
main {
java { srcDirs = ['src/main/java'] }
resources { srcDirs = ['src/main/resources', "src/main/profiles/$env"] }
}
test {
java { srcDirs = ['src/test/java'] }
resources { srcDirs = ['src/test/resources'] }
}
}
test {
useJUnitPlatform()
}
task sourceJar(type: Jar, dependsOn: classes) {
archiveClassifier = 'sources'
from sourceSets.main.allSource
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
artifacts {
archives sourceJar
}
gradle.taskGraph.whenReady {
tasks.each { task ->
if (task.name.contains('test')) {
task.enabled = true
}
}
}
[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'
}
project('sb-web') {
dependencies {
implementation project(':sb-common')
}
}
project('sb-common') {
dependencies {
api 'org.springframework.boot:spring-boot-starter-web'
}
}