idea 打包时,点击右侧Maven->Lifecycle->package读取不到.env文件报错

来源:4-6 环境和环境变量

慕UI8284076

2024-03-24

application.properties文件内容

spring.application.name=spring-security
server.port=${SERVER_PORT:8080}

spring.profiles.active=${SPRING_PROFILES_ACTIVE}

#日志
logging.pattern.console=%clr(%d{${LOG_DATEFORMAT_PATTERN:HH:mm:ss.SSS}}){blue} %clr(%-5p) %clr(${PID}){faint} %clr(---){faint} %clr([%8.15t]){cyan} %clr(%-40.40logger{0}){blue} %clr(:){red} %clr(%m){faint}%n

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/${MYSQL_DATABASE}?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.username=${MYSQL_USERNAME}
spring.datasource.password=${MYSQL_PASSWORD}

spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
#禁用表结构自动更新
spring.jpa.hibernate.ddl-auto=none

# 国际化
spring.messages.always-use-message-format=false
spring.messages.basename=message
spring.messages.encoding=UTF-8
spring.messages.fallback-to-system-locale=true

# 异常处理,没有找到请求时,是否抛出异常
spring.mvc.throw-exception-if-no-handler-found=true
#用于控制是否将特定路径的静态资源文件添加到URL路径映射中,默认true
#参数的值为false时,不会将静态资源的URL路径添加到映射中。这意味着,即使应用中存在静态资源文件,在访问时也不会通过Spring的URL路径映射来处理,而是直接由Web服务器处理
spring.resources.add-mappings=false

.env 文件内容,该文件和pom.xml在同一层级

SPRING_PROFILES_ACTIVE=prod
SERVER_PORT=8081
MYSQL_DATABASE=security
MYSQL_USERNAME=root
MYSQL_PASSWORD=123456
MYSQL_SSL=true

报错信息:
06:16:50.150 WARN 29636 — [ task-1] JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : Access denied for user ‘${MYSQL_USERNAME}’@‘localhost’ (using password: YES)

写回答

1回答

接灰的电子产品

2024-03-24

这个正常啊,环境变量最终要在操作系统层面设置,命令行不会读取 ide 的插件设置

0
0

Spring Security+OAuth2 精讲,打造企业级认证与授权

一站式掌握主流安全框架与行业解决方案,从容应对各种安全难题。

1042 学习 · 316 问题

查看课程