shadow icon indicating copy to clipboard operation
shadow copied to clipboard

Issue: application.yml Not Loaded Correctly in Fat JAR Built with Shadow Plugin

Open kaziasifjawwad opened this issue 1 year ago • 1 comments

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

  1. 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
  2. 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'
      }
      
  3. 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.yml file is present in both the root and BOOT-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 IllegalArgumentException indicating that the placeholder could not be resolved.

Expected Behavior

  • Spring Boot should correctly load application.yml from BOOT-INF/classes/ and resolve all placeholders defined within it.

Additional Information

  • The application runs correctly when built using the bootJar task 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)
    ...

kaziasifjawwad avatar Aug 27 '24 04:08 kaziasifjawwad

Same Issue to me, hoping any updates.

kyuri-code avatar Sep 10 '24 01:09 kyuri-code

@kaziasifjawwad What should maven-shade-plugin do for this?

Goooler avatar Jan 27 '25 02:01 Goooler

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'
  }

Goooler avatar Feb 28 '25 04:02 Goooler

This case looks like the correct behavior like Jar task did, see #1296.

Goooler avatar Feb 28 '25 09:02 Goooler