I'm in the process of switching from war-packaging of my Spring Boot app to using an executable jar.
My resources are under src/main/webapp
and, according to Spring Boot docs (if I understand correctly), should not even be included in the final executable jar:
Do not use the src/main/webapp directory if your application is packaged as a jar. Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools if you generate a jar.
This is confirmed by the fact that in the target jar I cannot find these resources at all. Unfortunately the executable jar structure description seems to say nothing about where the resources are placed.
However, to my surprise, calling servletContext.getResourceAsStream("/myresource.js")
in the application DOES find the resource correctly. And the resource seems to be served from my src/main/webapp
directory (even when running the jar in standalone manner e.g. via java -jar myjar.jar
.
Also:
- calling
servletContext.getResourcePaths
lists the contents of mysrc/main/webapp
directory - calling
servletContext.getRealPath("/")
returns the exact path on the filesystem to my source files e.g.c:\path\to\my\project\src\main\webapp
.
Why is this happening? I would expect only what's inside my jar to be available to the application. Does the embedded tomcat place my source files in the context root for some reason? Any links to relevant documentation would be appreciated.
P.S. In my pom.xml you will see standard things like:
<packaging>jar</packaging>
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
...