前言
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'
}
// gradle 脚本自身需要使用的资源
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
# 构建可执行 jar
gradle -q bootJar
# 构建 web 部署包
gradle -q bootWar
2.3 构建 Spring-cloud 脚本
// gradle 脚本自身需要使用的资源
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'
3.2 共享配置
allprojects
allprojects 是根 Project 的一个属性,在 Gradle 中,我们可以在根 Project 的 build.gradle 文件中 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'
}
}
4. 打包 Jar
运行前,添加 jar 描述信息,然后运行命令 gradle jar
即可生成
jar {
archivesBaseName = '<项目名称>'
archiveVersion = '<版本号>'
manifest {
// manifest 文件声明主程序入口
attributes "Main-Class": "<类完整路径>"
attributes "Class-Path": "<第三方库文件路径>"
}
from {
configurations.runtimeClasspath.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
在使用Gradle打包成一个可运行的Jar包的时候,需要把编译时依赖的库也打包进去:
- implementation引入的库
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
- testImplementation引入的库
from {
configurations.testRuntimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
运行 Jar 乱码
- 通过 chcp 命令改变代码页,UTF-8 的代码页为 65001
在中文 windows 系统中,如果一个文本文件是 utf-8 编码的,那么在 cmd.exe 命令行窗口(所谓的 dos 窗口)中不能正确显示文件中的内容。在默认情况下,命令行窗口中使用的代码页是中文或者美国的,即编码是中文字符集或者西文字符集。
- 配置输出编码 -Dfile.encoding=UTF-8
运行脚本如下:
chcp 65001
java -Dfile.encoding=utf-8 -jar xxx.jar
三、常见问题
1. Gradle 版本更新
# update the project to 6.7.
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 仓库地址等。
4. log4j
must be one of [compile, runtime, system] but is ‘test’. in log4j:log4j:1.2.16
这是因为 Log4J 1.2.16 的 pom 中存在一个Bug。1.2.16 已经在 2010 年停止更新了
可以通过声明对 log4j:log4j:1.2.17 的显式依赖或通过依赖关系管理确保使用 1.2.17 来解决
implementation("log4j:log4j:1.2.17")