0

I have a simple Java backend build with Maven. When I create .jar file with mvn clean install, I can run it and everything works fine.

However, when I deploy it to Docker, the server runs, but when I make a GET call, the backend doesn't see static .png file located in src/main/resousces/.

java.io.FileNotFoundException: /src/main/resources/logo.png (No such file or directory)

The image is uploaded to docker with this .gitlab-ci.yml file:

services:
  - docker:dind

before_script:
  - docker info

maven-build:
  stage: build
  tags:
    - maven3-jdk11
  except:
    - tags
  script:
    - mvn deploy -B -U

maven-release:
  stage: deploy
  tags:
    - maven3-jdk11
  only:
    - tags
  script:
    - mvn deploy -B -Prelease-profile -Dmaven.javadoc.failOnError=false

(This is my first time working with Maven and Docker, so if the information I provided is not sufficient, please leave a comment.)

druskacik
  • 2,176
  • 2
  • 13
  • 26

2 Answers2

2

Okay, I managed to solve it using java ClassLoader.

Originally, I loaded the .png file like this:

Image logo = new Image(ImageDataFactory.create("src/main/resources/logo.png"));

I changed that to following and now it works like charm:

ClassLoader classLoader = getClass().getClassLoader();
Image logo = new Image(ImageDataFactory.create(classLoader.getResource("logo.png")));

I accepted Eugene's answer to boost his reputation, he moved me in the right direction.

druskacik
  • 2,176
  • 2
  • 13
  • 26
1

Unpack your jar file and check if .png file in it. If not, you need to package a jar with the resources folder. If yes, so you need to specify the path to file using classpath.