我有一个 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/