当我在GitLab CI CD中运行命令mvn clean test -Dspring.profiles.active=GITLAB-CI-TEST
时,它没有加载属性文件application-gitlab-ci-test.properties
。它仅加载application.properties
。
文件application-gitlab-ci-test.properties
包含spring.datasource.url
的其他值时,管道在远程运行程序中失败,并显示错误
The last packet sent successfully to the server was 0 milliseconds ago.
The driver has not received any packets from the server.
当然,由于属性文件application.properties
引用了本地主机数据库,因此会出现此错误。
加载application-gitlab-ci-test.properties
的代码:
@Profile("GITLAB-CI-TEST")
@PropertySource("classpath:application-gitlab-ci-test.properties")
@Configuration
public class GitLabCiTestProfile {
}
当我尝试在本地运行同一命令时,它按预期方式工作并在日志中运行,我看到以下记录:
2020-03-30 19:23:00.609调试604 --- [主要] o.s.b.c.c.ConfigFileApplicationListener:加载的配置文件 '文件:/ G:/ **** / **** / **** / **** / target / classes / application.properties' (类路径:/application.properties)
2020-03-30 19:23:00.609调试604 --- [主要] o.s.b.c.c.ConfigFileApplicationListener:加载的配置文件 '文件:/ G:/ **** / **** / **** / **** / target / classes / application-GITLAB-CI-TEST.properties'(类路径:/ application-GITLAB-CI -TEST.properties)用于配置文件 GITLAB-CI-TEST
我注意到远程选手错过了第二行。这个正在加载application-GITLAB-CI-TEST.properties
。
我还尝试了mvn clean test --batch-mode -PGITLAB-CI-TEST
,但是这在远程主机上也失败了,但是在本地运行中却按预期运行。
我通过使用命令
找到了解决此问题的方法mvn clean test --batch-mode -Dspring.datasource.url=jdbc:mysql://mysql-db:3306/*******?useSSL=false&allowPublicKeyRetrieval=true
由于此解决方法不能满足我的要求,您能帮我解决这个问题吗?
答案 0 :(得分:0)
我找到了解决此问题的方法。
我将配置文件的名称从大写(GITLAB-CI-TEST
更改为小写(gitlab-ci-test
),以匹配属性文件-application-gitlab-ci-test.properties
中的配置文件名称的小写字母。
现在在远程运行器中,我正在使用以下命令:
mvn clean test -Dspring.profiles.active=gitlab-ci-test
Spring文档-link