【发布时间】:2019-10-08 21:49:24
【问题描述】:
这是我上一个问题的后续 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
有人可以建议我做错了什么吗?
【问题讨论】:
-
使用 Spring Boot 创建一个 fatjar 不要自己动手。 Spring Boot 已经生成了一个包含所有内容的可执行 fatjar,您正试图将其硬塞到另一个 fatjar 中。
-
你的意思是我在根 build.gradle 中的 mmJar 不是正确的方法吗?您能否使用我的两个 gradle 文件将其发布为答案
-
您的 Spring Boot(子)项目是 fatjar...您不需要其他任何东西。
标签: spring spring-boot gradle spring-boot-gradle-plugin