1

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?

nantitv
  • 3,539
  • 4
  • 38
  • 61
  • Use Spring Boot to create a fatjar don't do it youyrself. Spring Boot already gnerates an executable fatjar containing everything, you are trying to shoehorn that into another fatjar. – M. Deinum May 22 '19 at 11:56
  • Did you mean my mmJar in root build.gradle is not the correct way? Could you please post it as answer using my two gradle files – nantitv May 22 '19 at 12:00
  • Your Spring Boot (sub) project IS the fatjar... You don't need anything else. – M. Deinum May 22 '19 at 12:01

1 Answers1

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

b0gusb
  • 4,283
  • 2
  • 14
  • 33
  • I removed the build.gradle in root project and ran ./gradlew bootJar Then I got error in my second project Build file ‘Otherproject1/build.gradle' line: 2 * What went wrong: A problem occurred evaluating project ':Otherproject1'. > Could not find method compile() for arguments ```[{group=com.google.guava, name=guava, version=18.0}] ```on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. – nantitv May 22 '19 at 12:28
  • Please see the build.gradle of Otherproject1/build.gradle dependencies { compile group: 'com.google.guava', name: 'guava', version:'18.0' compile 'org.slf4j:slf4j-api:1.7.7' compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.5' compile 'com.fasterxml.jackson.core:jackson-databind:2.9.5' compile 'org.apache.commons:commons-lang3:3.4' testCompile 'junit:junit:4.11' } – nantitv May 22 '19 at 12:28
  • I was being silly. Forgot to add apply plugin 'java' to second project's build.gradle – nantitv May 22 '19 at 12:30
  • yes, it' solved the issue. I checked the content of the fat jar ( jar tf myspringboot.jar) and I can see the my otherproject1.jar in that – nantitv May 22 '19 at 12:43