1. 项目结构及说明

1
2
3
4
5
6
7
8
9
10
11
gradle/
wrapper/
gradle-wrapper.jar // 下载gradle包的依赖
gradle-warpper.properties // 下载gradle包的配置文件,里面包含地址等
build.gradle //主配置 = pom.xml
gradlew //执行脚本-shell
gradlew.bat //执行脚本-windows
gradle.properties //全局配置文件/全局变量
setting.properties //


2. 配置说明

2.1. gradle-warpper.properties

1
2
3
4
5
6
7
8
9
10
# 用户目录
distributionBase=GRADLE_USER_HOME
# 下载位置
distributionPath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
# 下载地址
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-6.9.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

2.2. 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
plugins {
id 'java'
}
//apply plugin: 'java'
//apply plugin: org.gradle.api.plugins.JavaPlugin
apply from: 'grammar.gradle' //引入另一个gradle文件
apply from: 'logger.gradle'
// compileJava.destinationDir = file("target/classes")

//添加 Maven 仓库
// 一个项目可以采用多个库,Gradle会按照顺序从各个库里寻找所需的依赖文件,并且一旦找到第一个便停止搜索。
repositories {
maven {
url "https://maven.aliyun.com/nexus/content/groups/public"
}
mavenCentral()
}
//gradle本身需要的依赖
buildscript {
repositories {
maven {
url "https://maven.aliyun.com/nexus/content/groups/public"
}
mavenCentral()
}
dependencies {
classpath("io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE")
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RC1")
}
}
subprojects {
配置结构和父工程相似
}

2.3. gradle.properties

1
2
3
4
# 普通变量 使用project.springBootVersion 获取
springBootVersion=1.2.1
# 系统环境变量 需要使用 System.properties['systemVersion'] 获取
systemProp.systemVersion=1.0.0

2.4. setting.gradle

1
2
3
4
//包含子工程
include 'common'
include 'web'

3. 语法说明

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
//添加属性
ext {
springVersion="2.2.5.RELEASE"
classesDir = new File('build/classes')
}

//创建一个任务
task check2 {
println "------------check2-----------"
println "project.name: "+project.name
println "project.springVersion: "+project.springVersion
println "systemVersion: " + System.properties['systemVersion']
//语法
if(project.hasProperty('springVersion')){
println 'has spring version'
} else {
println 'has not spring version'
}
}
//任务依赖
task compile2(dependsOn: 'resources') {
if (project.classesDir.isDirectory()) {
println 'The class directory exists. I can operate'
}
// do something
}

4. 插件

4.1. java插件

java相关插件

1
2
3
4
5
6
apply plugin: 'java'
//apply plugin: org.gradle.api.plugins.JavaPlugin

//定制java 相关属性(java 的目录)
compileJava.destinationDir = file("target/classes")

4.2. management插件

依赖管理插件

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
plugins {
//id "io.spring.dependency-management" version <<version>>
//插件
id "io.spring.dependency-management" version "1.0.8.RELEASE"
id 'java'
}

/* 配置依赖管理,
dependencies -> 依赖版本控制
imports -> mavenBom 和 maven的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
差不多
*/
dependencyManagement {
dependencies {
dependency 'commons-io:commons-io:2.11.0'
dependency 'commons-collections:commons-collections:3.2'
}
imports {
//spring-boot 依赖管理
mavenBom 'org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE'
//spring-cloud 依赖管理
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7'
}
}

5. 多模块工程

5.1. 父模块

build.gradle 额外配置subprojects方法

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
plugins {
//id "io.spring.dependency-management" version <<version>>
id "io.spring.dependency-management" version "1.0.8.RELEASE"
}

//所有工程配置
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
}
//子工程统一配置
subprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '0.0.1'
//gradle本身需要的依赖信息
buildscript {
repositories {
maven {
url "https://maven.aliyun.com/nexus/content/groups/public"
}
mavenCentral()
}
dependencies {
classpath("io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE")
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RC1")
}
}
//子工程需要的依赖信息
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
dependencies {
dependency 'commons-io:commons-io:2.11.0'
dependency 'commons-collections:commons-collections:3.2'
}
imports {
//spring-boot 依赖管理
mavenBom 'org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE'
//spring-cloud 依赖管理
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7'
}
}
repositories {
maven {
url "https://maven.aliyun.com/nexus/content/groups/public"
}
mavenCentral()
}
//乱码解决
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
}

setting.gradle

1
2
3
include 'common'
include 'web'

5.2. 子模块

build.gradle

1
2
3
4
5
6
7
8
dependencies {
compile group: 'commons-collections', name: 'commons-collections'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-test'
compile 'commons-io:commons-io'
//引用项目模块
implementation(project(':common'))
}