13

The documentation of Spring Boot states:

Do not use the src/main/webapp directory if your application will be packaged as a jar.

But surprisingly the Spring Boot Sample for static web files is using the /src/main/webapp directory. And also JHipster is using the webapp folder.

So I'm confused. Is the warning in the documentation of Spring Boot outdated? Is it now considered good practice to use src/main/webapp to serve static files with Spring Boot jar applications? And if not, what is the recommended practice now when using Spring Boot in a Maven setup?

asmaier
  • 11,132
  • 11
  • 76
  • 103
  • 2
    the sample app you reference is packaged as a war, not a jar. See the pom: war – gyoder May 06 '15 at 13:50
  • 2
    Ok, that explains why the example works. But I thought one of the main selling points of Spring Boot is that it creates a standalone jar file. So the example is a bit misleading. – asmaier May 07 '15 at 07:55

3 Answers3

1

From Spring Boot Reference Documentation (emphasis mine):

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.

In some of my (Maven) projects I am currently using src/main/resources/static because it's considered part of the classpath by default and IDEs (like Eclipse) tend to like this.

Marco Ferrari
  • 4,914
  • 5
  • 33
  • 53
1

Be wary because Spring Boot 2.4.x disables the default servlet (which impacts on loading of src\main\webapp and anything else at the root of your WAR file. JAR file packaging is disabled by default but there is a configuration option to all that too - see Spring boot: configure it to find the webapp folder.

https://github.com/spring-projects/spring-boot/issues/22915

The full release notes are here: https://spring.io/blog/2020/11/12/spring-boot-2-4-0-available-now

0

hi if i understand your question when your are using jar packaging in spring boot yo will put your resource in META-INF/resources and you can use this jar package in other project and call resource , you can move webapp to meta-inf directory using maven and gradle build

ali akbar azizkhani
  • 2,213
  • 5
  • 31
  • 48