2

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 don’t have much experience with grade and spring boot. I was wondering how can I create a fat jar from the multi gradle project.?

I converted my http servlet project to a spring boot project but I am confused that how I will refer the other gradle projects in the springboot project and create a single fat jar? Please see the directory structure of the projects

Rootrepository
  - Springboot project
     -src….
- OtherGardleProject1
    - Src…
- OtherGardleProject2
    - Src…

Can someone please share some pointer?

nantitv
  • 3,539
  • 4
  • 38
  • 61
  • I think following example follow fall into my requirement https://github.com/jwilsoncredera/Profiles-Blog. Still I welcome comments.Thanks – nantitv May 21 '19 at 20:52

1 Answers1

1

You could use a top-level settings.gradle file that includes the main app and the libraries

Rootrepository
  - settings.gradle 
  - Springboot project
     - build.gradle
     -src….
- OtherGardleProject1
    - Src…
- OtherGardleProject2
    - Src…

The settings.gradle looks like this:

include: ':Springboot project', ':OtherGardleProject1', ':OtherGardleProject2'

Then in the build.gradle of the Springboot project module you add the dependencies to the libraries

plugins {
   id 'org.springframework.boot' version '2.0.3.RELEASE'
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

...
dependencies {
  compile project(':OtherGardleProject1')
  compile project(':OtherGardleProject2')
  ...
}

After you build the project, the jar in Sprinboot project/build/libs folder should contain the classes of the app and the other two modules as jar files.

Hope it helps.

b0gusb
  • 4,283
  • 2
  • 14
  • 33
  • That was I doing previously. I was looking to create a single fat jar – nantitv May 22 '19 at 09:29
  • What exactly do you mean with a fat jar? Add the classes of the two modules in the `BOOT-INF\classes` inside the jar? – b0gusb May 22 '19 at 10:35
  • I am not sure whether it should add in BOOT-INF but classes of other project should add to the springboot jars – nantitv May 22 '19 at 10:38
  • I followed the directory structure as you mentioned and able to create a fat jar using ```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) } } } ```. But while running it fails with following exception – nantitv May 22 '19 at 10:50
  • ```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)``` – nantitv May 22 '19 at 10:51
  • 2
    I suspect it has to do with the way the jar is built. Instead of creating the jar yourself you should let the `spring-boot` gradle [plugin](https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/) do it. – b0gusb May 22 '19 at 11:04
  • Don't I need to add build.gradle in the root repository? – nantitv May 22 '19 at 11:41
  • I posted a followup question. Please see https://stackoverflow.com/questions/56256266/exception-while-creating-a-fat-jar-for-spring-boot-application-which-depends-on – nantitv May 22 '19 at 11:56
  • As you said, I removed the build.gradle from the root and jut relied on springboot plugin to create the jar and it worked – nantitv May 22 '19 at 12:45