My spring-boot application is currently running smoothly on a local computer, but I have encountered an issue. When I run mvn package, none of my CSS or JavaScript files located in
/src/main/wepapp/css
are included in the jar file created in the target directory. According to the Spring Boot reference guide,
65.3 Convert an existing application to Spring Boot "Static resources can be moved to /public (or /static or /resources or /META-INF/resources) in the classpath root."
24.1.4 Static Content "Do not use the src/main/webapp folder if your application will be packaged as a jar. Although this folder is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar."
This indicates that I should move all my JS and CSS folders into
/src/main/resources/static
Now my structure looks like this:
/src/main/resources/static/css/
/src/main/resources/static/js/
All of my Thymeleaf templates are still located in
/src/main/resources/templates/
I updated my ResourceHandlers accordingly after moving the CSS files:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pdfs/**").addResourceLocations("/pdfs/").setCachePeriod(0);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(0);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(0);
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0);
}
However, despite trying various configurations, I am still facing a 404 error when attempting to access the CSS and JS files from the browser.
I have temporarily disabled Spring Security in my test project to simplify debugging.
Additionally, I am uncertain about the deployment assembly. Even though I included the necessary folders in the assembly, running "mvn package" does not copy all the contents of my /src/main/static folder into the generated JAR file, while the "templates" folder is copied successfully.
In my Thymeleaf layout declaration (/src/main/resources/templates/layout.html), I reference the CSS file like this:
<!DOCTYPE html>
<html>
<head>
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
<link rel="stylesheet" type="text/css" media="all" th:href="@{/css/syncServer.css}" href="../css/syncServer.css" />
...
</head>
<body>
...
</body>
</html>
My question is: Is my current configuration correct for finding the CSS files in /src/main/static/css? If so, what other settings do I need to consider?
Additional note:
Test project repository link: [email protected]: TheDictator/sArchitecture.git