spring-bootuberjarspring-autoconfiguration

Unable to exclude DataSourceAutoConfiguration class while running fat jar from cmd


Below is my spring boot main class configuration :

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@Configuration
@EnableAutoConfigurationProperties
public class MainClass{

}

When i am running this spring boot using intellij, it doesn't throw any error but after i had exported the fat jar using the following :

build.gradle >>>

jar{
   manifest{
      attributes{
         //main class
      }
   }
   from{
      configurations.runtimeClasspath.collect{it.isDirectory()? it : zipTree(it)}

   }
}

the cmd output says :

The following classes could not be excluded because they are not auto-configurable classes:
    - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Can someone please help me understand what I am missing while running through cmd?

I tried excluding "META-INF/.SF", "META-INF/.DSA" , "META-INF/*.RSA" while creating the fat jar but the issue still persists.


Solution

  • You don't need to build a fatjar yourself. Spring Boot already handles that for you.

    Remove your custom jar task and make sure you have the Spring Boot plugin in yuour list of plugins.

    plugins {
      id 'org.springframework.boot' version '3.1.1'
    }
    

    Or whatever your version is.

    Then when running ./gradlew build or gradle build the resulting artifact will already be a fatjar and runnable jar. You don't need to do any additional work for that.