gpt4 book ai didi

java - Gradle:使用 spring-boot 进行多项目构建

转载 作者:行者123 更新时间:2023-12-02 06:02:58 24 4
gpt4 key购买 nike

我是 Gradle 新手,我刚刚开始使用多项目构建。目前,我正在使用 spring-boot 进行服务,并且我有以下目录结构

|- api-web (module)
|-- build.gradle
|- api-admin (module)
|-- build.gradle
|- build.gradle (root)
|- settings.gradle

以下是root的配置

build.gradle(根)

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

description = "Company Multi-Project"

ext {
springBootVersion = '2.2.2.RELEASE'
}

bootJar {
enabled = false
}

allprojects {

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

repositories {
mavenCentral()
}
}

subprojects {

group = 'com.company'
version = '0.0.1-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

jar {
enabled = true
}

bootJar {
enabled = false
}

dependencies {

// Lombok annotation processor
annotationProcessor 'org.projectlombok:lombok'

compileOnly 'org.projectlombok:lombok'
}
}

settings.gradle(根)

rootProject.name = 'core'

include 'common'
include 'api-web'
include 'api-admin'

模块的 Gradle 配置

api-admin(模块)

jar {
manifest {
attributes 'Main-Class': 'com.company.apiadmin.ApiAdminApplication'
}
}

dependencies {
implementation project(':common')
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}

我添加了 jar 关闭以防止运行时出错。这避免了无法运行并显示“No Manifest”的问题

但是,即使添加了 jar 关闭之后,现在还是出现了一个新问题

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

现在我知道这表示找不到类,但我的问题是它是如何发生的以及为什么以及如何克服这个问题。我已经尝试了 Medium、StackOverflow 和博客文章中的大部分建议。没有任何帮助。我们将非常感谢您的帮助。谢谢

最佳答案

我自己找到了解决方案,找到起来有点棘手,但最终我找到了

要点是你一定要添加 spring-boot-gradle-plugin这将解决您所有的 NoClassDef错误和 No Manifest Found错误。要添加此内容,您应该在根 build.gradle 文件中定义构建脚本

buildscript {
ext {
springBootVersion = "2.2.2.RELEASE"
}

repositories {
mavenCentral()
}

dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}

然后你应该为你的项目定义插件

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

然后你就可以定义所有的 subprojectsallprojects像平常一样。

然后在模块中,在每个子模块中只需添加这些行

apply plugin: 'org.springframework.boot'

bootJar {
launchScript() // optional
}

dependencies {

// your module-specific dependencies
}

和平常一样的依赖关系。

就是这样,现在您可以使用 gradle 进行构建并执行 .jar像往常一样保存文件。 :-)

关于java - Gradle:使用 spring-boot 进行多项目构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59430631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com