java Docker无法在类路径上找到资源

a11xaf1n  于 2023-02-11  发布在  Java
关注(0)|答案(2)|浏览(235)

我有一个简单的Gradle Sping Boot Java应用程序,我尝试使用Spring Boot配置文件从“application.properties“和“application-dev.properties“获取一些属性值。当我尝试在本地计算机上运行应用程序时,该应用程序运行正常,Spring Boot配置文件正在加载,但当我尝试在Docker上运行同一应用程序时,突然弹出一个错误,指出应用程序无法在类路径上找到资源。
项目结构如下:

在App.config类中,我有下面的代码。正如你所看到的,我试图从www.example.com文件中获取属性值application.properties。

@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;
}
}

application.properties 包含以下代码:

map = Main-Map

spring.profiles.active=${profile}

application-dev.properties 包含以下代码:

host = Development-host

正如您所看到的,我正在从外部设置www.example.com中的配置文件值application.properties,这就是我试图通过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映像:

docker build -t demo:latest .

我使用以下命令运行Docker:

docker run -p 8083:8080 demo:latest

当我运行docker run命令时,出现以下异常:

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

我可以尝试什么来解决这个问题?

rks48beu

rks48beu1#

看起来你并没有在建造Fat Jar。你可以利用spring-boot-maven-plugin来建造它。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.0.1.RELEASE</version>
</plugin>

然后将Dockerfile更改为:

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"]
bqf10yzr

bqf10yzr2#

与Docker无关,您是否尝试过构建jar文件并通过java -jar运行它?

ADD build/resources/main/application.properties /app/application.properties

ADD build/resources/main/application-dev.properties /app/application-dev.properties

这些也是不必要的。当你创建你的工件为fat/uber/Shadow jar时,你将把你的属性打包在jar中。

如何构建胖罐/优罐/影子罐(Spring Boot Gradle)

https://plugins.gradle.org/plugin/org.springframework.bootGradle: Build 'fat jar' with Spring Boot Dependencies

备选

https://imperceptiblethoughts.com/shadow/https://github.com/johnrengelman/shadow

相关问题