1

I have a basic springboot application with the following folder structure. inside webapps i have some images and css.

enter image description here

If i run it using my Intellij IDE, I am able to access images and css

at localhost:8080/images/test.png

But if i create a fat jar and run it using java -jar demo.jar then i am not able to access these images or css folders

This is how i am building my jar using maven plugin

   <build>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>css/default.css</include>
                    <include>images/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

How can i make sure that these images and css files will be available when running as a jar file

Andromeda
  • 12,659
  • 20
  • 77
  • 103

2 Answers2

2

The problem is that in the case of a JAR-file, src/main/webapp has no special meaning. It's not recommended to use it according to the documentation:

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.

The way you can make this work is by treating src/main/webapp as a normal classpath directory. You did that by adding the <resources> tag.

However, Spring boot only serves static content from certain folders (/public, /static or /META-INF/resources). Since you didn't put your files in such a folder, they aren't served by Spring boot.

To solve this, you need to put your files within either src/main/resources/public (recommended) or src/main/webapp/public. If moving the files isn't an option, you can also change Maven to put it in that directory for you:

<resources>
    <resource>
        <directory>src/main/webapp</directory>
        <targetPath>public/</targetPath> <!-- Add this -->
        <includes>
            <include>css/default.css</include>
            <include>images/*</include>
        </includes>
    </resource>
</resources>
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • Its working now. targetPath was the one that i was missing. After adding that it works fine – Andromeda Mar 30 '22 at 09:43
  • It was working from my windows machine with oracle jdk-15. But when i run the same jar in wsl with openjdk15, it wont work. Is there anything specific to os or jdk – Andromeda Mar 30 '22 at 13:50
  • Well i got the same error with wsl and oraclejdk15. So it must be the OS compatibility issue – Andromeda Mar 30 '22 at 14:07
0

Put custom files in resources/static.

I host a Spring Boot application in Docker on my VPS. This code will work on a local system whilst developing in an IDE as well as if the .JAR-file is wrapped in a Docker image:

Example case:

I like to get a PDF. The PDf document is located at static/docs/not-a-porn.PDF.

1.) Wire the class ResourceLoader

private final ResourceLoader resourceLoader;

public MyService(@Autowired ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
}

2.) Create a resource by pathOfAttachment (String)

String pathOfAttachment = "classpath:static/docs/not-a-porn.PDF"
Resource resource = this.resourceLoader.getResource(pathOfAttachment);

Resource is an interface that extends InputStreamSource as you can see in the documentation:

public interface Resource extends InputStreamSource

Source: org.springframework.core.io Link to documentation (click here)

3.) Get file

String fileName = resource.getFilename();
File file = resource.getFile();
Kevin O.
  • 355
  • 3
  • 11