1、概述
gradle官网: https://gradle.org/
gradle学习视频:https://www.bilibili.com/video/BV1yT41137Y7/?p=24&spm_id_from=333.788.top_right_bar_window_history.content.click&vd_source=372365c1ea72bb6075a0ec1b05671b3a
Gradle是一款Google推出的 基于JVM、 通用灵活的 项目构建工具, 支持Maven,JCenter多种第三方仓库;支持传递性依赖管理、废弃了繁杂的xml文件,转而使用 简洁的 、 支持多种语言 (例如:java、groovy等)的 build脚本文件 。
- 常见构建工具
- 学习目的
适用大型项目,构建性能高
1.1 安装
- 本地安装
- 下载压缩包
- 解压压缩包
- 配置环境变量
- 验证版本 gradle -v
- 使用gradle wrapper
1.2 目录结构
gradle默认露目录结构和maven默认目录结构基本一致,都是基于约定大于配置
1.3 常用指令
- 创建项目
- 常用指令
2、groovy
2.1 groovy简介
可以视为java的一种脚本化改良工具
2.1.1 特点
- 功能强大,例如提供了动态类型转换、闭包和元编程支持
- 支持函数式编程,不需要main函数
- 默认导入常用的包
- 类不支持default作用域,且默认作用域为public
- 基本类型也是对象,可以直接调用对象的方法
- 支持DSL和其他简洁的语法,让代码变得易于阅读和维护
- 基于java语言,完全兼容java语言
2.1.2 安装
brew install groovy
groovy --version
2.2 语法
2.3 应用
2.3.1 数据类型
def a = 1
def str = 'say'
def str1 = "i ${str}"
def str1 = "i $str"
2.3.1 闭包
一般作为方法的参数使用
// 定义闭包
def test1= { ->
println("闭包+")
}
test()
test1.call()
//方法中定义闭包
def test1(Closure closure){
closure()
}
// 闭包如果是最后一个参数,可以放在方法的最外边
def method1(num1,num2,Closure closure){
closure(num1,num2)
}
method1(1,2,{a,b -> println(a+b)})
method1(1,3){a,b->println(a+b)}
def method2(Closure closure){
closure(5,5)
}
method2 {k,v -> println(k+v)}
3、 gradle
3.1 基础知识
3.1.1 生命周期
gradle的生命周期主要分为三个阶段
- 初始化阶段
- 配置阶段
- 执行阶段
3.1.2 三种依赖方式
- 本地依赖:依赖本地某个jar包,具体可通过文件集合,文件树的方式执行
plugins {
id 'java'
}
group 'org.xing'
version '1.0-SNAPSHOT'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation files('/lib/mysql.jar')
implementation fileTree('dir':'lib',includes: ['*.jar'], excludes: ['a.jar'])
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
}
// junit5的支持,4不需要
test {
useJUnitPlatform()
}
- 项目依赖:依赖某个项目
implementation project 'demo01'
- 直接依赖:依赖的类型 组名 名称 版本号
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testImplementation group:'org.junit.jupiter', name: 'junit-jupiter-api',version: '5.7.2'
}
3.1.3 依赖的类型
- 1
- api与implementation的区别
3.1.4 依赖冲突
- 排除依赖
第一种
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testImplementation (group:'org.junit.jupiter', name: 'junit-jupiter-api',version: '5.7.2') {
exclude(group:'',name:'', version:'')
}
}
第二种
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testImplementation (group:'org.junit.jupiter', name: 'junit-jupiter-api',version: '5.7.2') {
//exclude(group:'',name:'', version:'')
exclude("module":"")
}
}
3.2 gradle插件
3.2.1 使用插件的原因
- 促进代码重用、减少功能类似代码编写,提升工作效率
- 促进项目更高程度的模块化、自动化、便捷化
- 可插拔式的拓展项目的功能
3.2.2 插件的作用
- 可以添加task[任务]到项目中,从而帮助完成测试、编译
打包- 可以添加依赖配置到项目中
- 可以向项目中拓展新的扩展属性和方法
可以对项目进行一些约定,如应用java插件后,约定src/main/java目录为我们的源代码存在位置,编译时编译这个目录下的java源代码
3.2.3 插件的分类和使用
Ⅰ脚本插件
- verson.gradle
ext {
company = 'xing'
springBootVersion = '2.7.5'
spring = ['version':'5.0.5']
}
build.gradle
apply from: 'version.gradle'
Ⅱ二进制插件
a. 内部插件 [核心插件]
1. apply map具名参数
/**
* map具名参数
* key固定为plugin
* value 插件id、插件全类名、插件简类名
*/
apply plugin: 'java'
apply plugin: 'org.gradle.api.plugins.JavaPlugin'
2.apply 闭包
apply {
plugin 'java'
}
3. DSL方式
plugins {
id 'java'
}
b. 第三方插件
如果使用第三方发布的二进制插件,一般需要配置对应的仓库和类路径
1. 传统方式
buildscript {
ext {
springBootVersion = "2.7.4"
}
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
jcenter()
}
// 此处先引入插件
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 再应用插件
apply plugin: 'org.springframework.boot'
2. plugins DSL方式
plugins {
id 'org.springframework.boot' version '2.7.4'
}
c. 自定义插件
- 第一种方式
// 定义插件
class GreetingPlugin implements Plugin<Project>{
@Override
void apply(Project project) {
project.task('hello'){
doLast {
println 'hello from the GreetingPlugin'
}
}
}
}
// 使用插件
apply plugin: GreetingPlugin
- 第二种方式
abstract class GreetingPluginExtension {
abstract Property<String> getMessage()
GreetingPluginExtension() {
message.convention("hello from GreetingPlugin")
}
}
class GreetingPlugin implements Plugin<Project>{
@Override
void apply(Project project) {
def extension = project.extensions.create("greeting", GreetingPluginExtension)
project.task('hello'){
doLast {
println extension.message.get()
}
}
}
}
apply plugin: GreetingPlugin
greeting.message = 'hi from gradle'
Ⅲ 自定义插件之buildSrc目录
buildSrc是gradle默认的插件目录,编译的时候gradle会自动识别这个目录,将其中的代码编译为插件,
sourceSets {
main{
java {
srcDir 'src/main/java'
// 改动默认的编译文件目录
output.resourcesDir = file 'out/bin'
java.destinationDirectory.set file('out/bin')
}
resources {
srcDir 'src/main/resources'
}
}
test {
java {
srcDir 'src/test/java'
}
resources {
srcDir 'src/test/resources'
}
}
}
3.3 build.gradle文件
3.3 项目发布
将写好的项目发布到公司的私服
- 引入maven发布的插件
plugins {
id 'java'
id 'java-library' // 如果为war包,需要war插件,java-library支持带源码、文档发布
id 'maven-publish'
}
- 设置发布的代码
publishing {
// 配置坐标信息
publications {
myLibrary(MavenPublication) {
groupId = 'com.xing'
artifactId = 'springcloud-02'
version = '1.1'
from components.java //表明发布的是一个jar包
// from components.web //表明发布的是一个war包
}
}
// 配置仓库信息
repositories {
mavenLocal() // 本地仓库
// 私服仓库
maven {
name = '' // 属性可选,表示仓库名称,url必填
def snapshotsRepoUrl = layout.buildDirectory.dir("repos/snapshots")
def releaseRepoUrl = layout.buildDirectory.dir("repos/releases")
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releaseRepoUrl
// 配置认证信息
credentials {
username 'root'
password 'root'
}
}
}
}
3.5 微服务搭建
3.5.1 项目结构
microservice-parent:统一管理所有模块的jar包版本信息
microservice-bean:统一管理所有模块用到的pojo
microservice-common:统一管理所有模块用到的工具类、枚举类、异常处理、日志文件、统一返回结构等
microservice-gateway:封装网关信息
microservice-service:封装所有的微服务
3.5.2 settings.gradle
rootProject.name = 'microservice-parent'
include 'microservice-bean'
include 'microservice-common'
include 'microservice-gateway'
include 'microservice-service'
include 'microservice-service:microservice-order'
include 'microservice-service:microservice-user'
3.5.3 build.gradle
description 'springcloud-02 微服务父工程'
// 第三方插件对应的仓库和类路径
buildscript {
ext {
springBootVersion = '2.7.4'
springCloudVersion = '2021.0.3'
springCloudAlibabaVersion = '2021.0.1.0'
}
// 配制插件仓库
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
google()
mavenCentral()
}
// 此处先引入springboot插件
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
// 引入脚本插件
apply from: 'version.gradle'
allprojects {
group 'com.xing'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'java-library' // 提供api依赖方式支持
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
tasks.withType(JavaCompile){
options.encoding = 'UTF-8'
}
/*sourceSets {
main{
java {
srcDir 'src/main/java'
// 改动默认的编译文件目录
*//* output.resourcesDir = file 'out/bin'
java.destinationDirectory.set file('out/bin')*//*
}
resources {
srcDir 'src/main/resources'
}
}
test {
java {
srcDir 'src/test/java'
}
resources {
srcDir 'src/test/resources'
}
}
}*/
// 设置仓库地址
repositories {
mavenLocal()
// aliyun镜像
maven { url 'https://maven.aliyun.com/repository/public' }
maven {url 'https://repo.spring.io/milestone'}
mavenCentral()
}
// 设置公用的依赖
dependencies {
// 提供注解处理器
annotationProcessor 'org.projectlombok:lombok:1.18.24'
implementation 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
testImplementation 'org.projectlombok:lombok:1.18.24'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
// junit5的支持,4不需要
test {
useJUnitPlatform()
}
}
// 配置所有子工程
subprojects {
// 使用springboot支持插件
apply plugin:'org.springframework.boot'
// 提供dependencyManagement支持
apply plugin: "io.spring.dependency-management"
// 相当于maven的dependencyManagement标签
dependencyManagement{
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"
}
}
}
project(":microservice-bean"){
description '统一管理所有模块用到的pojo'
}
project(":microservice-common"){
description '统一管理所有模块用到的工具类、枚举类、异常处理、日志文件、统一返回结构等'
dependencies {
api "org.springframework.boot:spring-boot-starter-test"
//api 'mysql:mysql-connector-java:8.0.30'
//api 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2'
//api 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
//api 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
}
}
project(":microservice-service"){
description '封装所有的微服务'
subprojects {
dependencies {
api 'org.springframework.boot:spring-boot-starter-web'
//api 'org.springframework.boot:spring-boot-starter-amqp'
api 'io.jsonwebtoken:jjwt:0.9.0'
api 'javax.xml.bind:jaxb-api:2.3.1' // 解决javax.xml.bind.DatatypeConverter
api project(':microservice-bean')
api project(':microservice-common')
}
}
}
project(":microservice-service:microservice-user"){
dependencies {
}
}