Convert gradle multi project to springboot fat jar application - spring-boot

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?

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.

Related

producing an artifact of a Spring Boot application that can be used as a dependency

My goal is to produce an artifact of my Spring Boot application that I can use as a dependency and [edit] that is a fat jar, that is, it contains all the dependencies of my application [edit].
I use gradle as a build tool with the following plugins:
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.4'
id 'io.spring.dependency-management' version '1.0.14.RELEASE'
id 'org.jetbrains.gradle.plugin.idea-ext' version '1.1.6'
}
Now, the Spring Boot documentation states at
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.build.use-a-spring-boot-application-as-dependency that
'Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency [...] To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.'
This is followed by a description on how to do this with the build tool maven, but there is no mentioning of how to do this in gradle.
Any help of how to set this up with gradle is much appreciated.
When building with Gradle, an artifact with a plain classifier is built by default. This artifact is a standard, plain jar file that is suitable for use as a dependency.

[Forge 1.8.9]: Including dependencies in JAR

I've been making a Forge mod for Minecraft 1.8.9 with the Forge MDK. So far my mod has 1 dependency, which is SnakeYAML. I added this line: compile 'org.yaml:snakeyaml:1.30' to my build.gradle file, so that I have SnakeYAML during development, but it doesn't get included when I build the JAR. Is there any way to include SnakeYAML in my JAR as well?
You can create a fat Jar. In summary, a fat Jar contains all of the dependency classes and resources in a single output Jar. I assume you're using Gradle as this is a Forge project.
Add the Shadow plugin
plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
id 'java'
}
Then configure your dependencies to shadow in SnakeYAML:
dependencies {
implementation 'org.yaml:snakeyaml:1.30'
...
shadow 'org.yaml:snakeyaml:1.30'
}
Finally, use the ShadowJar task added under 'Shadow' to build your fat Jar. You may need to change the version of Shadow for the gradle version you're using. Refer to the documentation for any configuration you may wish to add.

Gradle WAR project setup

I have the following project structure. Gradle 3.1
RootProject
-- Project A <-- RESTful JAX-RS Project
-- Project B <-- Services Project containing EJB's
I want to assemble this project into one single WAR archive such as RootProject.war which would include Project A & B for deployment in an EAR archive (totally separate from this project).
How do I setup the root level build.gradle and the sub projects build.gradle files?
If I set the apply plugin: 'war' to the root build.gradle i get options for creating war archives from each project!

spring boot gradle project with multiple modules not creating fat jar properly

I have a spring boot gradle project with multiple modules in it. The parent project (metadata) is just the root folder for child projects and there are 3 child projects (api, security and ui).
security is a standalone project that uses spring security. here is its pom
dependencies {
compile "org.springframework.boot:spring-boot-starter-security"
compile "com.att.security:csp-cookies:1.0"
compile "javax.servlet:javax.servlet-api"
}
api project uses some of classes from security project so it depends on that
dependencies {
compile ("org.springframework.boot:spring-boot-starter-data-rest") {
exclude module: "spring-boot-starter-tomcat"
}
compile "org.apache.httpcomponents:httpclient:4.4"
compile(project(":metadata-security"))
}
3.finally the UI project is the runnable project that combines everything together.
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile project(":api:metadata-api-invenio")
compile(project(":metadata-security"))
}
When I do gradle build spring boot gradle plugin generates the fat jar for all 3 projects. Upon extracting the jar file it seems like all the transitive deps from all projects are available in the libs folder inside the fat jar. This is good but for some reason the security project jar file is a fat jar and contains its dependencies inside its lib folder which is not needed. In contrast to that, api project jar is not a fat one and only contains its classes because its jars are already available in the ui fat jar.
Any ideas why security project jar is being created different from api project.

Gradle multiple project build ClassNotFound on deploy

My web app is build using two projects one that contains the api and second web part. In Eclipse I am able to use classes from project-api in project-web however in deploy I have exception:
Caused by: java.lang.ClassNotFoundException: project.api.TestApi
This is my main build.gradle file:
sourceCompatibility = 1.7
subprojects {
apply plugin: 'java'
}
This is build.gradle from api:
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
}
And this is part of my build.gradle from web:
dependencies {
compile project(':project-api')
...
}
If I go and see web app libraries I can't see anything like project-api.jar. Wham am I doing wrong?
UPDATE:
It is issue only in eclipse. If I run gradlew war and deploy this manually to tomcat I can deploy it without any issued. project-api.jar is included in war. I tried already to run few times
`gradlew clean cleanEclipse eclipse`
but it doesn't help. I also try to reimport projects in eclipse but still the same.
EDIT:
This project is a spring mvc app and I just discovered that when I build war from gradle I am able to deploy war manually without any errors. Issue is only when trying to deploy via eclipse.
EDIT: Here is settings.gradle
rootProject.name = 'project'
include 'project-test'
include 'project-web'
include 'project-api'
You need to convert the dependencies of the deployable project to a "faceted project".
Project properties -> Project Facets -> Convert to faceted form...
Then mark each dependency as a "Utility Module".

Resources