Exception while creating a fat jar for spring boot application which depends on other project

依然范特西╮ 提交于 2019-12-11 07:36:44

问题


This is follow up of my previous question Convert gradle multi project to springboot fat jar application

I had a http servlet application which was a multi project gradle build, where my project was a contain gradle HttpServlet project and it depends on the other two gradle java projects. I deployed all the 3 jars in the tomcat webapps/Web-INF/lib directory and run it.

Now there is a requirement that I have to convert my application as a spring boot application and instead of 3 separate jars I have to create a fat jar which I should run it.

I arranged my code as follows

    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'
}

Please see 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')
}

Then I ran gradle clean assemble mmJar and it created a fat jar as expected.

But when I try to run 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

Can someone please suggest what am I doing wrong?


回答1:


As suggested by @M.Deinum and me in the linked post, just let spring boot gradle plugin do the the job. In your root folder keep the settings.gradle file and remove the build.gradle. Run

gradle clean build

in the Rootrepository folder. Gradle will build the projects included in settings.gradle and create the fat jar in the Springboot project/build/libs folder



来源:https://stackoverflow.com/questions/56256266/exception-while-creating-a-fat-jar-for-spring-boot-application-which-depends-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!