gpt4 book ai didi

spring-boot - 如何在使用命令行 Gradle (6.4) build 构建 Spring Boot 应用程序时传递 spring.config.location ="somepath"

转载 作者:行者123 更新时间:2023-12-03 04:54:48 27 4
gpt4 key购买 nike

我有一个 SpringBoot 应用程序,其中我在项目之外有 application.properties 文件(它不在通常的位置 src/main/resources 中)。
使用 gradle clean build 构建应用程序时,它失败,因为代码无法找到属性文件。
我尝试了许多命令来传递 vm args、gradle opts 但它不起作用。

gradle clean build -Djvmargs="-Dspring.config.location=/users/home/dev/application.properties" //not working
当它创建 Spring 应用程序上下文并且无法替换属性占位符时,它在测试阶段失败。如果我以 gradle clean build -x test 跳过测试有用。
虽然我可以使用 java -jar api.jar --spring.config.location=file:/users/home/dev/application.properties 运行应用程序
请帮助我如何通过 spring.config.location=/users/home/dev/application.properties在 gradle build 中使用命令行,以便 build 与所有 Junit 测试一起运行

最佳答案

如果我是你,我不会涉及到 junit 测试的实际属性。所以我会在 src/test/resources/application-test.properties 下为项目创建一个测试属性。在junit test中我会加载测试属性。
示例:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyProperties.class)
@TestPropertySource("classpath:application-test.properties")
public class MyTestExample{

@Test
public void myTest() throws Exception {
...
}

}

关于spring-boot - 如何在使用命令行 Gradle (6.4) build 构建 Spring Boot 应用程序时传递 spring.config.location ="somepath",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62575906/

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