spring - 为依赖于其他项目的 Spring Boot 应用程序创建胖 jar 时出现异常
问题描述
这是我之前的问题 Convert gradle multi project to springboot fat jar application
我有一个 http servlet 应用程序,它是一个多项目 gradle 构建,我的项目是一个包含 gradle HttpServlet 项目,它依赖于其他两个 gradle java 项目。我在 tomcat webapps/Web-INF/lib 目录中部署了所有 3 个 jar 并运行它。
现在有一个要求,我必须将我的应用程序转换为 Spring Boot 应用程序,而不是 3 个单独的 jar,我必须创建一个应该运行它的胖 jar。
我安排我的代码如下
Rootrepository
- settings.gradle
- build.gradle
- Springboot project
- build.gradle
-src….
- OtherGardleProject1
- Src…
- OtherGardleProject2
- Src…
Please see `root project’s build.gradle`
version = '1.0.0'
group = ‘myproj’
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin: 'eclipse'
subprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'java'
sourceCompatibility = 1.8
compileJava
jar {
from sourceSets.main.allSource
}
}
subprojects.each { subproject ->
evaluationDependsOn(subproject.path)
}
task mmJar(type: Jar, dependsOn: subprojects.jar) {
manifest {
attributes 'Main-Class': 'org.springframework.boot.loader.JarLauncher'
attributes 'Start-Class': ‘mypackage.springboot.Application'
}
subprojects.each { subproject ->
from subproject.configurations.archives.artifacts.files.collect {
zipTree(it)
}
}
}
dependencies {
compile 'junit:junit:4.12'
compile 'org.slf4j:slf4j-api:1.7.7'
}
请参见springboot projects’ build.gradle
buildscript {
ext {
spring_plugin_version = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_plugin_version")
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation('org.springframework.boot:spring-boot-starter-quartz')
implementation('org.springframework.boot:spring-boot-starter-web')
compile project(‘:otherproject1’)
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
compile('org.springframework:spring-webmvc')
runtimeOnly('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok:1.18.8')
annotationProcessor('org.projectlombok:lombok:1.18.8')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
然后我跑了gradle clean assemble mmJar
,它按预期创建了一个胖罐子。
但是当我尝试跑步时java -jar build/libs/myproj-1.0.0.jar
Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/JavaEWAH-1.1.6.jar
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:86)
at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/JavaEWAH-1.1.6.jar'
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:256)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:241)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:103)
... 4 more
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/JavaEWAH-1.1.6.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:284)
at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:264)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
... 6 more
有人可以建议我做错了什么吗?
解决方案
正如@M.Deinum 和我在链接帖子中所建议的那样,只需让 spring boot gradle 插件完成这项工作。在您的根文件夹中保留settings.gradle
文件并删除build.gradle
. 跑
gradle clean build
在Rootrepository
文件夹中。Gradle 将构建包含的项目并在文件夹中settings.gradle
创建 fat jarSpringboot project/build/libs
推荐阅读
- php - 在 Bootstrap 5 中更改主按钮颜色
- cmake - “通过魔法”将不正确的依赖项添加到 CMake 目标,variable_watch 报告没有写入
- datetime - 离线模式下的otp当前日期时间问题
- api - VS Code API 调用不适用于基本授权
- c++ - 为什么在 same_as 中检查两次相等性?
- python-3.x - 生成具有特定长度并使用掩码的所有可能的十六进制字符串列表的最有效方法
- javascript - 使用 Firestore 集合创建带有 javascript 函数的 Html 表
- markdown - jupyter实验室可以当markdown编辑器吗?
- php - 在 PHP 中正确显示 HTML 电子邮件
- discord - 如何检查用户消息是否是视频?