编辑历史
2023-12-08 16:15:使用gradle构建多模块spring boot应用程序新的心得
宜需求评审 生发不易,且码且珍惜
参考资料
plugins.gradle.org/plugin/org.…
以 Spring Boot Gradle Plugin 为例
Using the [plugins DSL]
plugins {
id("org.springframework.boot") version "3.2.0"
}
Using [legacy plugin application]
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:3.2.0")
}
}
apply(plugin = "org.springframework.boot")
我遇到过的问题
在使用gradle构建多模块spring boot应用程序时,我会删除掉src目录(包含主类)
如果在根目录的build.gradle文件中使用了如下gradle插件:
plugins {
id 'java-library' // or id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}
Applied in isolation the spring boot gradle plugin makes few changes to a project. Instead, the plugin detects when certain other plugins are applied and reacts accordingly. For example, when the java plugin is applied a task for building an executable jar is automatically configured.
而我在根目录下会删除src目录,导致出现如下错误:Main class name has not been configured, and it could not be resolved from classpath
解决方法
在根目录下的build.gradle文件中,通过subject{}块避免对根项目应用spring boot gradle plugin
通过 legacy plugin application 传统插件的应用方式 ,结合buildsript{}块 与 apply plugin 方法
The buildscript block is used for:
global dependencies and repositories required for building the project (applied in the subprojects).
declaring which plugins are available for use in the build script (in the build.gradle(.kts) file itself).
Binary plugins published as external jar files can be added to a project by adding the plugin to the build script classpath and then applying the plugin.
External jars can be added to the build script classpath using the buildscript{} block as described in External dependencies for the build script
Applying script plugins using the legacy apply() method
参见:
具体方法如下:
// buildscript{}块:用于指定构建脚本所需的仓库;配置构建脚本自身所需的类路径和依赖项。gradle插件依赖声明通常包括在dependencies{}块中。位于构建脚本的顶层。
buildscript {
ext {
set('springVersion', "1.1.4")
set('springBootVersion', "3.2.0")
set('springCloudVersion', "2023.0.0")
}
// 构建脚本所需的仓库
repositories {
mavenCentral()
}
// Gradle plugin dependencies
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:${springVersion}")
}
}
// 应用和管理项目所需的gradle插件,可以简化插件的引入和应用过程,不需要手动声明依赖项。
plugins {
id 'java-library'
// 2023-12-08 14:19,为什么会将下面的两行代码注释,是因为如果在这里启用gradle插件,会对 lucky-coffee 项目生效
// 出现 application - bootRun Task ,然后 build task 会报错 : Main class name has not been configured and it could not be resolved from classpath
// 原因:Spring Boot Gradle Plugin 与 java` plugin 同时使用: a task for building an executable jar is automatically configured
// id 'org.springframework.boot' version '3.2.0'
// id 'io.spring.dependency-management' version '1.1.4'
}
// `allProjects` block configures the root project and each of the sub-projects.
allprojects {
// Configures the repositories for this project. 项目仓库
repositories {
mavenCentral()
}
group = 'com.example.luckycoffee'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
tasks.named('test') {
useJUnitPlatform()
}
}
// `subProjects` block, unlike allProjects, only configures the sub-projects.
subprojects {
// 应用一个通用的插件到所有子项目中; java plugin内置的可设变量
apply plugin: 'java-library'
// 应用插件
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}