Spring Boot classpath
Asked Answered
C

3

41

In the Spring Boot's docs here, about serving static content, it says:

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.

I found that all the content in the directory:

src/main/resources

will be copied inside the classpath, so I can put my static content in:

src/main/resources/static

and all will work fine and I'm happy since I can have my static content under the src directory.

But, I have some questions about this:

  1. Why the documentation doesn't say to put static content in src/main/resources/static instead of speaking about the classpath (I think this is a bit confusing)?
  2. Is it good to assume that the content in src/main/resources/ will be always copied in the classpath?
  3. Is there some Spring Boot official documentation explaining what I'm supposed to find in the classpath other than Java classes and packages (up to now I only know I can found all the content from src/main/resources/)?
Charentemaritime answered 8/12, 2015 at 16:1 Comment(0)
I
50

/src/main/resources is a Maven project structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar, some physical file system location used in the classpath (with java's -cp option), etc.

I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.

The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.

Irs answered 8/12, 2015 at 16:42 Comment(2)
It is a bit sad to know that the directory /src/main/resources it is not strictly related with Spring Boot, since now I have to remember that if I start a Spring Boot project with another build tool instead of Maven (maybe Gradle?) the project could not works if I put static content in /src/main/resources.. :/Charentemaritime
@Charentemaritime It's a pretty widely adopted convention. Gradle supports it, too.Irs
L
7

The classpath also contains additional libraries (JARs), which also can have a static folder, which would then be included for serving static resources. So if the documentation would only state the folder src/main/resources/static, it would be incomplete.

Ad 2: As long as you don't mess with the default Maven configuration, then it's safe to assume this.

Ad 3: Maybe start with the official Oracle documentation: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html. Hint: Of course, it's not only the contents of the resources folder, which are in the classpath, but also of course all compiled classes, hence its name.

Linearity answered 8/12, 2015 at 16:22 Comment(3)
Thanks for the answer. In my 3th point I meant "official docs by Spring Boot": some doc where it is explained that in a Spring Boot application the content from src/main/resources is copied in the classpath or something like that. ThxCharentemaritime
That has nothing to do with Spring Boot, so you won't find anything about that in the Spring Boot docs. It is default Maven (or Gradle, if you use that) functionality, and it's available in every Maven/Gradle project, not only Spring Boot applications.Linearity
Thanks, I didn't knew that /src/main/resources is a build tool's convention, I thought was more Spring Boot related.Charentemaritime
H
0

To add to previous answers, it's the Spring Boot Maven Plugin (spring-boot-maven-plugin in the pom.xml) that that enables you to run the application using Maven. One of its functions is to ensure contents in the app including dependency libraries are available on the runtime classpath, such as within the the executable JAR file.

Heel answered 31/8, 2022 at 3:47 Comment(1)
Hello Andrew and welcome to SO! This is some great content but might be better suited as a comment. See stackoverflow.com/help/how-to-answerProvocative

© 2022 - 2024 — McMap. All rights reserved.