I have a simple gradle spring boot java application, where I am trying to get some properties value from "application.properties" and "application-dev.properties" using spring boot profiling. It is working fine and spring boot profile is loading when I try to run application on the local machine, but when when I try to run same application on the docker, so suddenly error popsup which says that application is not able to find resources on the classpath.
Below is the project structure
In the App.Config class I have the following code. As you can see I am trying to get property value from application.properties file.
@Component
@PropertySource("classpath:application.properties")
public class AppConfig {
@Value("${host}")
private String host;
@Value("${map}")
private String map;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getMap() {
return map;
}
public void setMap(String map) {
this.map = map;
}
}
map = Main-Map
spring.profiles.active=${profile}
host = Development-host
As you can see I am setting profile value in application.properties from outside. Thats what I am trying to inject through docker
FROM java:8
VOLUME /tmp
ENV tom=dev
ADD build/libs/demo-0.0.1-SNAPSHOT.jar /app/app.jar
ADD build/resources/main/application.properties /app/application.properties
ADD build/resources/main/application-dev.properties /app/application-dev.properties
WORKDIR /app
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-
Dprofile=${tom}","-jar","app.jar", "-
-spring.config.location=/app/application.properties, /app/application-
dev.properties"]
docker build -t demo:latest .
docker run -p 8083:8080 demo:latest
2019-12-12 07:09:50.405 INFO 1 --- [ main] com.example.demo.DemoApplication :
Starting DemoApplication on ed7cb11b8a34 with PID 1 (/app/app.jar started by root in /app)
2019-12-12 07:09:50.407 INFO 1 --- [ main] com.example.demo.DemoApplication : The
following profiles are active: dev
2019-12-12 07:09:50.603 WARN 1 --- [ main] ConfigServletWebServerApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class
[com.example.demo.DemoApplication]; nested exception is java.io.FileNotFoundException: class path
resource [application.properties] cannot be opened because it does not exist
2019-12-12 07:09:50.712 ERROR 1 --- [ main] o.s.boot.SpringApplication :
Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [ com.example.demo.DemoApplication]; nested exception is java.io.FileNotFoundException: class path
resource [ application.properties] cannot be opened because it does not exist
at
Any help would be appreciated.
Thanks in advance.
Seems like you aren't building Fat Jar. You can utilize spring-boot-maven-plugin
for that.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
</plugin>
Then change your Dockerfile
like:
FROM java:8
VOLUME /tmp
ENV tom=dev
COPY build/libs/demo-0.0.1-SNAPSHOT.jar /app/app.jar
WORKDIR /app
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dprofile=${tom}","-jar","app.jar"]
It has nothing to do with the docker. Have you tried building the jar file and running it by java -jar
.
ADD build/resources/main/application.properties /app/application.properties
ADD build/resources/main/application-dev.properties /app/application-dev.properties
These are unnecessary too. When you create your artifact as fat/uber/Shadow jar, you will have your properties packaged in jar.
https://plugins.gradle.org/plugin/org.springframework.boot (Gradle: Build 'fat jar' with Spring Boot Dependencies)
Alternative
https://imperceptiblethoughts.com/shadow/ (https://github.com/johnrengelman/shadow )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With