当我在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
您可以帮我解决此问题,因为此解决方法使我不满意吗?
我找到了解决此问题的方法。我将配置文件的名称从大写(GITLAB-CI-TEST
)更改为小写(gitlab-ci-test
),以匹配属性文件-application-gitlab-ci-test.properties
中的配置文件名称的小写字母。
现在在远程运行程序中,我正在使用以下命令:
mvn clean test -Dspring.profiles.active=gitlab-ci-test
春季文档-link