0

I'm totally new to spring boot/gradle, so if i say something wrong, please feel free to correct me.

I have two spring boot projects and i'm using Spring Boot Gradle Plugin to run one by one in different ports and to also generate the respective jars.

I want to know if it's possible to generate one fat jar that can run both projects in different ports.

Here's the structure of my project:

Project

  • setting.gradle

  • Project-1

    • src/.../@SpringBootApplication Main
    • build.gradle
  • Project-2

    • src/.../@SpringBootApplication Main2
    • build.gradle

I included both projects in the setting.gradle, the Project-2 in the dependencies of Project-1 and tried gradle clean build, but the fat jar generated in the Project-1 doesn't include the Project-2's jar. What I expected is that when I run the fat jar it exposes the two projects in their respective ports, as if I did gradle bootRun on each project.

Is what i'm doing correct? I'm assuming that putting the Project-2 in the dependencies of Project-1 would make gradle create the fat jar I want.

Or that's not possible and I just need to use the two jars that are given to me?

Thank you for your time.

DoG
  • 1
  • did you try googling? There are multiple ways to achieve that depending on what is acceptable to the use case. You can either deploy multiple jars behind the same tomcat instance or create a new jar containing both the applications. The first one is recommended though – Nikhil Sahu Oct 23 '20 at 16:02
  • Yes, I did google and that's how I got to the questions I have. Resorted to posting this here bacause I reached my limit on doing this by myself. And thank you for your answer, I'll try to google more in those directions. – DoG Oct 23 '20 at 20:03

1 Answers1

0

You can deploy two separate jars behind the same tomcat instance to get the same outcome. Say if you deploy my-first-jar.jar and my-second-jar.jar behind tomcat, you will get two set of endpoints like:

http://localhost:8080/my-first-jar/apis-from-first-jar

http://localhost:8080/my-second-jar/apis-from-second-jar

However, seems like what you are unable to create a fat jar correctly. By default the dependencies of the project are not included in the jar. You need to explicitly say to the build tool(gradle, in your case) to make a fat jar. Have a look here

Once fat jar of Project 1 is created, it will have files from project 2 as well, but you would be able to run only single spring boot app.

PS: You can run multiple apps my combining in a single container application if u wish. Have a look here but wont recommend you to go down that road coz its messy

Nikhil Sahu
  • 2,463
  • 2
  • 32
  • 48
  • Thanks again for your answer. I'm looking into the things you mensioned and I'll get back to you as soon as I resolve this. – DoG Oct 26 '20 at 13:20
  • So, I ended up using the two jars separately, each in a container and in different ports. Thanks for the help. – DoG Nov 06 '20 at 19:37