Search code examples
javaspringspring-bootgradlegradlew

Spring boot, gradle, angularJS and a fat jar


My spring-boot project is like this:

project
|__ build.gradle
|__ settings.gradle
   |__ application
   |__ library
   |__ web
|__ application
|__ library
|__ web

application contains the main and a MvcConfig class that implements WebMvcConfigurer:

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers (ResourceHandlerRegistry registry) {
        String workingDirectory = System.getProperty("user.dir");
        
        registry.addResourceHandler("/**")
            .addResourceLocations("file:///" + workingDirectory + "/../web/dist/");
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }
}

When I run: gradlew :application:bootRun => everything works fine.

My web directory is a angularJS application and gulp builds it in a dist repertory. (I'm using moowork node and gulp gradle plugin)

But now I want to a have all my application in one fat jar generated with bootJar.

java -jar application.jar (will run all the app)

application.jar contains web.jar which contains all that is inside the dist repertory.

gradlew :application:bootJar generates a jar with everything (library, spring-boot web) but when I launch it, it doesn't find my index.html

I know that I have to change the addResourceLocations arguments but I need some help to do it. I have tried (without success):

.addResourceLocations("classpath:/web/");

Solution

  • Ok The answer was simple: As web.jar contains all that is inside dist, I just need to do this:

    .addResourceLocations("classpath:/");