Gradle 在 Spring 中的使用-ApiHug准备-工具篇-006

  🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓

  1. GitHub - apihug/apihug.com: All abou the Apihug   
  2. apihug.com: 有爱,有温度,有质量,有信任
  3. ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace

ApiHug 整个工具链基于 Gradle, 使用 ApiHug 准备工作最先需要学习的就是 gradle. 工欲善其事,必先利其器

参考: BOM 版本open in new window + 版本字段open in new window 。

pugin文档源码备注
org.springframework.boot2.7.0DOCopen in new window源码open in new window
io.spring.dependency-management1.0.11.REALEASEDOCopen in new window源码open in new window

#init


gradle init

org.springframework.bootopen in new window 主要是各种Job gradle tasks 里面 boot 开头的job 都是:

  1. 打包成可执行jar、war
  2. 引入以来管理 spring-boot-dependencies 也就是: BOM_COORDINATES

public static final String BOM_COORDINATES = "org.springframework.boot:spring-boot-dependencies:"
   + SPRING_BOOT_VERSION;

gradle tasks

Application tasks
-----------------
bootRun - Runs this project as a Spring Boot application.

Build tasks
-----------
bootBuildImage - Builds an OCI image of the application using the output of the bootJar task
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
bootJarMainClassName - Resolves the name of the application's main class for the bootJar task.
bootRunMainClassName - Resolves the name of the application's main class for the bootRun task.

io.spring.dependency-managementopen in new window, 主要是依赖控制: A Gradle plugin that provides Maven-like dependency management and exclusions, 提供和maven 里面 pom 类似操作。

这两个插件一般是组合使用, dependency-management 依赖 boot 的 pom 版本。


plugins {
  id 'org.springframework.boot' version '2.7.0'
  id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  id 'java'
}

BOM 版本open in new window + 版本字段open in new window 。 两个plugin 有什么关系?

当 io.spring.dependency-management 插件被加入的时候, Spring Boot 插件将自动导入 spring-boot-dependencies bom,也就是避免导入starter 时候设置版本号(pom 的本质和好处)。

相当于自动导入了:

dependencyManagement {
  imports {
    mavenBom "org.springframework.boot:spring-boot-dependencies:${SPRING_BOOT_VERSION}"
  }
}
    

spring.boot 插件是避免带入版本号, 如果你只想使用 spring.boot 的版本依赖, 但是不想引用 spring.boot 的定制的一些task 比如 boot jar(比如定制spring starter); 有两种方式。

#第0种

plugins {
  id 'org.springframework.boot' version '2.7.0'
  id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  id 'java'
}


ext {
  set('springCloudVersion', "2021.0.3")
  set('testcontainersVersion', "1.17.2")
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  implementation 'org.springframework.cloud:spring-cloud-starter-config'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'org.testcontainers:junit-jupiter'
}

dependencyManagement {
  imports {
    //不需要显式的导入 boot-dependencies 
    mavenBom "org.testcontainers:testcontainers-bom:${testcontainersVersion}"
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

#第一种

plugins {
	id 'java'
	id("org.springframework.boot") version "2.7.0" apply false
	id("io.spring.dependency-management") version "1.0.11.RELEASE"
}

dependencyManagement {
	imports {
		mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
	}
}

apply false 表示不带task过来, 那么下面 SpringBootPlugin.BOM_COORDINATES 同样带来 导入 boot bom 效果:


public static final String BOM_COORDINATES = "org.springframework.boot:spring-boot-dependencies:"
			+ SPRING_BOOT_VERSION;

默认引入了tasks:

gradle tasks

Application tasks
-----------------
bootRun - Runs this project as a Spring Boot application.

Build tasks
-----------
bootBuildImage - Builds an OCI image of the application using the output of the bootJar task
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
bootJarMainClassName - Resolves the name of the application's main class for the bootJar task.
bootRunMainClassName - Resolves the name of the application's main class for the bootRun task.

一旦 apply false, bootRun 开头的所有任务就没有了!

#第二种

用 gradle 原生的 platform 功能

plugins {
	id 'java'
	id("org.springframework.boot") version "2.7.0" apply false
}

dependencies {
	implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}

#两种方式差别

用 spring 的 dependency 或者 gradle platform 有什么差别?

#Spring Depedencies Management Plugin

Spring depedency management 提供了编辑通道:


ext['slf4j.version'] = '1.7.20'

#Gradle Platform

纯 gradle 方式:


configurations.all {
	resolutionStrategy.eachDependency { DependencyResolveDetails details ->
		if (details.requested.group == 'org.slf4j') {
			details.useVersion '1.7.20'
		}
	}
}

org.springframework.boot 和 boot 一致, io.spring.dependency-management 一般配合使用, dependency-management 会使用 boot 的版本。

#参考项目

  1. 统一版本管理 Sharing dependency versions between projectsopen in new window
  2. The Java Platform Pluginopen in new window
  3. spring boot pluginopen in new window
  4. spring depedency managementopen in new window
  5. spring framework testopen in new window
  6. java gradle template

api-hug-contact

  • 26
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值