Issue: application.yml Not Loaded Correctly in Fat JAR Built with Shadow Plugin
Issue: application.yml Not Loaded Correctly in Fat JAR Built with Shadow Plugin
Description
I encountered an issue where the application.yml file is not being loaded correctly when packaging a Spring Boot application into a fat JAR using the com.github.johnrengelman.shadow plugin.
Steps to Reproduce
-
Project Setup:
-
Spring Boot version:
2.7.7 -
Gradle version:
8.10(Replace with your version) -
Shadow plugin version:
8.1.1 -
Java version:
17
-
Spring Boot version:
-
Build the Fat JAR:
-
I used the following Gradle configuration to build the fat JAR:
plugins { id 'application' id 'org.openjfx.javafxplugin' version '0.0.13' id 'com.github.johnrengelman.shadow' version '8.1.1' } shadowJar { archiveClassifier.set('all') mergeServiceFiles() from('src/main/resources') { into 'BOOT-INF/classes/' include '**/*.yml' } } application { mainClass = 'com.jawwad.posrestaurant.PosRestaurantApplication' }
-
-
Run the Application:
-
After building the fat JAR, I executed it using:
java -jar build/libs/posrestaurant-0.0.1-SNAPSHOT-all.jar
-
Observed Behavior
- The
application.ymlfile is present in both the root andBOOT-INF/classes/directories within the JAR. - Despite this, Spring Boot fails to resolve placeholders defined in
application.yml(e.g.,${backend.baseurl}) during the application startup. - The application throws an
IllegalArgumentExceptionindicating that the placeholder could not be resolved.
Expected Behavior
- Spring Boot should correctly load
application.ymlfromBOOT-INF/classes/and resolve all placeholders defined within it.
Additional Information
- The application runs correctly when built using the
bootJartask or when using the non-fat JAR. - The issue appears to be specific to the configuration when using the Shadow plugin.
Logs and Output
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'backend.baseurl' in value "${backend.baseurl}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180)
...
Same Issue to me, hoping any updates.
@kaziasifjawwad What should maven-shade-plugin do for this?
I believe you could move src/main/resources/application.yml into src/main/resources/BOOT-INF/classes/application.yml and remove
from('src/main/resources') {
into 'BOOT-INF/classes/'
include '**/*.yml'
}
This case looks like the correct behavior like Jar task did, see #1296.