从 Gradle 多模块项目构建的 Jar 与 Spring 引导不起作用
Jar built from Gradle multi-module project with Spring Boot doesn't work
我创建了 spring-boot gradle 多模块项目,它由 3 个模块组成:控制器、服务、存储库。主文件位于控制器模块中,名为 MySpringBootApplication。
我可以构建这个项目(使用 gradle 构建)并且可以获得 jar 文件。但是在命令行中启动这个 jar 之后,我遇到了下一个错误:
Error: Could not find or load main class com.epam.esm.config.MySpringBootApplication Caused by: java.lang.ClassNotFoundException: com.epam.esm.config.MySpringBootApplication.
为了修复这个错误,我在主 build.gradle
中的 MANIFEST.MF 文件中添加了 Main-Class
属性,但此操作没有帮助。那么有人可以帮忙吗?
主要BUILD.GRADLE文件
plugins {
id 'java'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.4.3'
id 'application'
}
group = 'com.myproject'
version = 'snapshot-1.0.0'
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
manifest {
attributes(
"Main-Class": "com.myproject.config.MySpringBootApplication")
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.4.3', ext: 'pom'
}
test {
useJUnitPlatform()
}
subprojects {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
group = 'com.epam.esm'
version = '1.0.0'
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'org.hibernate', name: 'hibernate-envers', version: '5.4.27.Final'
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
implementation group: 'org.openidentityplatform.commons', name: 'json-web-token', version: '2.0.11'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.18'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.4.3', ext: 'pom'
}
test {
useJUnitPlatform()
}
}
SETTINGS.GRADLE
rootProject.name = 'module'
include('repository', 'service', 'controller')
SpringBoot应用程序可执行jar文件是由bootJar
任务构建的,所以通过[=添加main-class信息15=] 也不行。
The bootJar
task tries to create an executable jar, and that
requires a main() method. As a result, you need to disable the
bootJar
task and enable the jar task (which creates an ordinary jar
rather than an executable jar) only for your no executable jar
modules.
由于您是在 subprojects
部分下完成的,因此 controller
模块也会生成标准的 jar。您可以为所有模块生成标准 jars,但不包括 controller
模块,如下所示:
subprojects {
if (it.name != 'controller') {
bootJar {
enabled = false
}
jar {
enabled = true
}
}
}
此外,您必须删除下面的 jar
部分
jar {
enabled = true
manifest {
attributes(
"Main-Class": "com.myproject.config.MySpringBootApplication")
}
}
并替换
bootJar {
enabled = false
}
和
bootJar {
mainClassName = 'com.myproject.config.MySpringBootApplication'
}
参考
我创建了 spring-boot gradle 多模块项目,它由 3 个模块组成:控制器、服务、存储库。主文件位于控制器模块中,名为 MySpringBootApplication。
我可以构建这个项目(使用 gradle 构建)并且可以获得 jar 文件。但是在命令行中启动这个 jar 之后,我遇到了下一个错误:
Error: Could not find or load main class com.epam.esm.config.MySpringBootApplication Caused by: java.lang.ClassNotFoundException: com.epam.esm.config.MySpringBootApplication.
为了修复这个错误,我在主 build.gradle
中的 MANIFEST.MF 文件中添加了 Main-Class
属性,但此操作没有帮助。那么有人可以帮忙吗?
主要BUILD.GRADLE文件
plugins {
id 'java'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.4.3'
id 'application'
}
group = 'com.myproject'
version = 'snapshot-1.0.0'
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
manifest {
attributes(
"Main-Class": "com.myproject.config.MySpringBootApplication")
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.4.3', ext: 'pom'
}
test {
useJUnitPlatform()
}
subprojects {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
group = 'com.epam.esm'
version = '1.0.0'
repositories {
mavenCentral()
}
bootJar {
enabled = false
}
jar {
enabled = true
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'org.hibernate', name: 'hibernate-envers', version: '5.4.27.Final'
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
implementation group: 'org.openidentityplatform.commons', name: 'json-web-token', version: '2.0.11'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.18'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.4.3', ext: 'pom'
}
test {
useJUnitPlatform()
}
}
SETTINGS.GRADLE
rootProject.name = 'module'
include('repository', 'service', 'controller')
SpringBoot应用程序可执行jar文件是由bootJar
任务构建的,所以通过[=添加main-class信息15=] 也不行。
The
bootJar
task tries to create an executable jar, and that requires a main() method. As a result, you need to disable thebootJar
task and enable the jar task (which creates an ordinary jar rather than an executable jar) only for your no executable jar modules.
由于您是在 subprojects
部分下完成的,因此 controller
模块也会生成标准的 jar。您可以为所有模块生成标准 jars,但不包括 controller
模块,如下所示:
subprojects {
if (it.name != 'controller') {
bootJar {
enabled = false
}
jar {
enabled = true
}
}
}
此外,您必须删除下面的 jar
部分
jar {
enabled = true
manifest {
attributes(
"Main-Class": "com.myproject.config.MySpringBootApplication")
}
}
并替换
bootJar {
enabled = false
}
和
bootJar {
mainClassName = 'com.myproject.config.MySpringBootApplication'
}
参考