目录
前言一、安装1. 下载安装 Gradle2. 在 IDEA 中使用 Gradle
二、Gradle 使用1. build.gradle2. Gradle 构建 Spring-boot 可运行 Jar / war2.1 spring-boot gradle 构建脚本2.2 执行 Task2.3 构建 Spring-cloud 脚本2.4 Euraka client
3. 配置子项目3.1 settings.gradle2. 共享配置allprojectssubprojectsconfigure独享配置
三、常见问题1. Gradle 版本更新2. Gradle 显示所有警告3. build.gradle 文件中 repositories、dependencies 与 buildscript 代码块的区别
前言
Gradle 是一个流行的自动化构建工具,其功能和作用与 Maven 相似。但它拥有比 Maven 更简洁的语法,而且可读性更高。 今日收获的标签是一只小象:
一、安装
1. 下载安装 Gradle
官方网址:https://gradle.org/ 下载地址:https://gradle.org/releases/
解压到指定目录修改系统变量,创建变量 %GRADLE_HOME%, 在 Path 变量中增加 %GRADLE_HOME%\bin;在命令窗口执行 gradle -v,正常显示版本即安装成功
2. 在 IDEA 中使用 Gradle
IDEA 帮助文档:https://www.jetbrains.com/help/idea/gradle.html 在 IDEA 中使用 Gradle 需要插件:Gradle, Gradle Extension
ctrl + shift + s 打开设置窗口在 plugin 中 找到 插件并确保已经开启 双击 ctrl, 在 Run Anything 窗口可以执行 Gradle 相关命令
二、Gradle 使用
1. build.gradle
官方文档:构建脚本基础知识 简单来说, Gradle 脚本近似于 groovy 的程序代码。
2. Gradle 构建 Spring-boot 可运行 Jar / war
官方文档 : Spring Boot’s new Gradle plugin
2.1 spring-boot gradle 构建脚本
group
'org.dt.example'
version
'1.0-SNAPSHOT'
apply plugin
: 'java'
apply plugin
: 'org.springframework.boot'
apply plugin
: 'io.spring.dependency-management'
tasks
.withType(JavaCompile
) {
options
.encoding
= 'UTF-8'
}
repositories
{
maven
{
url
'https://maven.aliyun.com/nexus/content/groups/public/'
}
maven
{ url
'https://repo.spring.io/libs-snapshot' }
mavenLocal()
mavenCentral()
}
dependencies
{
implementation
'org.codehaus.groovy:groovy-all:2.3.11'
implementation
'org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE'
testImplementation
'org.springframework.boot:spring-boot-starter-test:2.3.4.RELEASE'
testImplementation group
: 'junit', name
: 'junit', version
: '4.12'
}
buildscript
{
ext
{
springBootVersion
= '2.0.0.BUILD-SNAPSHOT'
}
repositories
{
maven
{ url
'https://repo.spring.io/libs-snapshot' }
mavenCentral()
}
dependencies
{
classpath
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
2.2 执行 Task
gradle -q bootJar
gradle -q bootWar
2.3 构建 Spring-cloud 脚本
buildscript
{
ext
{
springBootVersion
= '2.0.0.BUILD-SNAPSHOT'
springCloudVersion
= 'Finchley.SR1'
}
repositories
{
maven
{ url
'https://maven.aliyun.com/nexus/content/groups/public/' }
maven
{ url
'https://repo.spring.io/libs-snapshot' }
mavenCentral()
}
dependencies
{
classpath
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
plugins
{
id
'groovy'
}
apply plugin
: 'java'
apply plugin
: 'idea'
apply plugin
: 'org.springframework.boot'
apply plugin
: 'io.spring.dependency-management'
group
'com.payne.study'
version
= '0.0.1-SNAPSHOT'
sourceCompatibility
= 1.8
tasks
.withType(JavaCompile
) {
options
.encoding
= 'UTF-8'
}
repositories
{
maven
{
url
'https://maven.aliyun.com/nexus/content/groups/public/'
}
maven
{ url
'https://repo.spring.io/libs-snapshot' }
mavenLocal()
mavenCentral()
}
dependencies
{
implementation
'org.codehaus.groovy:groovy-all:2.3.11'
implementation
'org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE'
implementation
'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
testImplementation
'org.springframework.boot:spring-boot-starter-test:2.3.4.RELEASE'
testImplementation group
: 'junit', name
: 'junit', version
: '4.12'
}
dependencyManagement
{
imports
{
mavenBom
"org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
2.4 Euraka client
dependencies
{
implementation
'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
3. 配置子项目
3.1 settings.gradle
在根(Root)Project中加入名为 settings.gradle 的配置文件,文件内容如下:
rootProject
.name
= 'spring-cloud'
include
'client'
include
'server'
2. 共享配置
allprojects
allprojects 是根 Project 的一个属性,在 Gradle 中,我们可以通过根 Project 的allprojects() 方法将配置一次性地应用于所有的 Project 中,包括 root-project 本身:
allprojects
{
apply plugin
: 'java'
apply plugin
: 'idea'
apply plugin
: 'org.springframework.boot'
apply plugin
: 'io.spring.dependency-management'
group
'com.dt.study'
version
= '0.0.1-SNAPSHOT'
sourceCompatibility
= 1.8
tasks
.withType(JavaCompile
) {
options
.encoding
= 'UTF-8'
}
repositories
{
maven
{
url
'https://maven.aliyun.com/nexus/content/groups/public/'
}
maven
{ url
'https://repo.spring.io/libs-snapshot' }
mavenLocal()
mavenCentral()
}
dependencies
{
implementation
'org.codehaus.groovy:groovy-all:2.3.11'
implementation
'org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE'
testImplementation
'org.springframework.boot:spring-boot-starter-test:2.3.4.RELEASE'
testImplementation group
: 'junit', name
: 'junit', version
: '4.12'
}
dependencyManagement
{
imports
{
mavenBom
"org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}
subprojects
subprojects 和 allprojects 一样,也是父 Project 的一个属性,在父项目的build.gradle 脚本里,但 subprojects() 方法用于配置所有的子 Project(不包含根Project)
configure
Gradle提供的 configure 属性块可以让我们根据模块名称引入特定插件:
configure(allprojects
.findAll
{ it
.name
.startsWith('sub') }) {
subTask
<< {
println
'this is a sub project'
}
}
独享配置
也可以在 root-project 中对单个Project进行单独配置:
project(':sub-project1') {
task forProject1
<< {
println
'for project 1'
}
}
三、常见问题
1. Gradle 版本更新
gradle wrapper --gradle-version 6.7
执行以上代码后, 项目 gradle-wrapper.properties 文件内 distributionUrl 将更新为:https://services.gradle.org/distributions/gradle-6.7-bin.zip
2. Gradle 显示所有警告
gradle build -Dorg.gradle.warning.mode
=all
通过配置信息 -Dorg.gradle.warning.mode=all 显示运行过程中所有的警告信息
3. build.gradle 文件中 repositories、dependencies 与 buildscript 代码块的区别
build.gradle 文件中直接定义的依赖项、仓库地址等信息是项目自身需要的资源。buildscript 代码块中的声明是 gradle 脚本需要使用的资源。可以声明的资源包括依赖项、第三方插件、maven 仓库地址等。