Reputation: 93
I have a Springboot Gradle project. The application is up and running perfectly in the IDE. But when I package it as jar and run the jar file, application is not detecting the application.properties
(or application.yml
) file properties. I had to explicitly to add @PropertySource
to read the application.properties
. I feel there is some issue with the application packaging. Here is my Gradle plugins.
buildscript {
repositories {
mavenLocal()
maven {
mavenLocal()
url <Maven Repo>
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE"
classpath "com.github.jengelman.gradle.plugins:shadow:4.0.2"
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id "application"
id "jacoco"
}
apply plugin: 'io.spring.dependency-management'
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'project-report'
Any leads on this issue helpful.
Upvotes: 0
Views: 2434
Reputation: 2092
I don't know how you managed to run jar task when having also org.springframework.boot plugin in the gradle buildscript. As spring boot plugin should disable jar task from java plugin. Stack overflow answer.
If you have overriden this and you would want to generate working jar you would still need to configure that the libraries would be included or pass them to the java command.
So basically if you work with spring application the standard approach is to use bootJar Gradle task which will generate jar file which Spring Boot classloader will be able to process. As @dan1st mentioned in the comment.
See Spring Boot packaged jar has different structure than normal jar:
+--- spring-boot-jar-0.0.1-snapshot.jar
+--- meta-inf
+--- boot-inf
| +--- classes # 1 Project Classes
| | +--- com.rivancic.boot
| | | +--- SpringJarApplication.class
| | |
| | \--- application.properties
| |
| +--- lib # 2 Nested Jar Libraries
| +--- attoparser-2.0.5.RELEASE.jar
| +--- jackson-annotations-2.11.4.jar
| \--- ...
|
+--- org.springframework.boot.loader # 3 Spring Boot Loader Classes
+--- jarlauncher.class
+--- launchedurlclassloader.class
\--- ...
Normal Jar:
+--- jar-0.0.1-snapshot.jar
+--- meta-inf
+--- com.rivancic.boot # 1 Project Classes
| +--- SpringJarApplication.class
|
\--- application.properties
Upvotes: 3