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

标签 spring-boot gradle gradlew

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

相关文章:

Spring-boot JPA连接到运行时提供数据库和模式的postgres

java - Spring 启动 : error ="invalid_grant", error_description ="Bad credentials"

android - 根据构建参数更改我的源

android - 如何在 jenkins 中为 android 项目创建 local.properties?

java - 使用 RestTemplate 时出错 - 使用 Apache Commons Multimap 反序列化对象时出现问题

android - 在Android Studio中安装Roboguice

android - Android Studio 更新后 Gradle 无法合并 dex

android - 生成 Gradle Wrapper 文件时出错

构建 NativeScript 项目时出现 java.util.zip.zipException

java - 服务发现中的 eureka unknownHostException