gpt4 book ai didi

java - Maven在尝试建立与数据库的连接的Spring项目构建中失败

转载 作者:行者123 更新时间:2023-12-02 19:04:39 24 4
gpt4 key购买 nike

我为我的spring项目制作了一个docker-compose和dockerfile,但是在maven构建阶段失败了,因为正在尝试与postgres服务建立连接,我不知道该如何解决。

这是我的docker-compose文件:

version: '3'
services:

postgres:
build:
context: .
dockerfile: docker/postgres/Dockerfile
restart: always
environment:
POSTGRES_DB: ${postgres_database}
POSTGRES_USER: ${postgres_username}
POSTGRES_PASSWORD: ${postgres_password}
ports:
- '5432:5432'
volumes:
- ./pgdata:/var/lib/postgresql/data

api:
build:
context: .
dockerfile: docker/jdk/Dockerfile
restart: always
environment:
postgres_database: ${postgres_database}
postgres_username: ${postgres_username}
postgres_password: ${postgres_password}
ports:
- '8100:8080'
depends_on:
- postgres

我的postgres dockerfile:
FROM postgres:11-alpine

ENV POSTGRES_DB: ${POSTGRES_DB}
ENV POSTGRES_USER: ${POSTGRES_USER}
ENV POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

COPY ./docker/postgres/init.sql /docker-entrypoint-initdb.d

EXPOSE 5432

我的jdk dockerfile,我在其中创建项目(失败)并运行jar:
FROM maven:3.6.0-jdk-11-slim AS build

COPY ./src /usr/src/app/src
COPY ./pom.xml /usr/src/app

RUN mvn -f /usr/src/app/pom.xml clean package

FROM adoptopenjdk/openjdk11:jre11u-alpine-nightly

COPY --from=build /src/usr/src/app/target/logger_service-0.0.1-SNAPSHOT.jar /usr/src/app/app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/usr/app/app.jar"]

错误日志:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 14.844 s <<< FAILURE! - in cl.getcolors.loggerservice.LoggerServiceApplicationTests
[ERROR] contextLoads(cl.getcolors.loggerservice.LoggerServiceApplicationTests) Time elapsed: 0.014 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
Caused by: java.net.UnknownHostException: postgres

我该如何解决?

最佳答案

我找到了解决方案,maven尝试执行应用程序的测试,以便它根据配置执行对数据库的调用,这是通过mvn clean package命令执行的。
-DskipTests必须将指令添加到命令以运行maven no tests,因此可以正确构建。

mvn clean package -DskipTests

我的dockerfile现在看起来像这样,可以正常工作:
FROM maven:3.6.0-jdk-11-slim AS build

COPY ./src /usr/src/app/src
COPY ./pom.xml /usr/src/app

RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests

FROM adoptopenjdk/openjdk11:jre11u-alpine-nightly

COPY --from=build /src/usr/src/app/target/logger_service-0.0.1-SNAPSHOT.jar /usr/src/app/app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/usr/app/app.jar"]

关于java - Maven在尝试建立与数据库的连接的Spring项目构建中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com