我有一个弹簧引导应用程序。

我的应用程序中有三个概要文件——>开发、登台和生产。我有3个文件

application-development.yml application-staging.yml application-production.yml

我的应用程序。Yml位于src/main/resources中。我已经在应用程序中设置了活动配置文件。Yml为:

spring:
  profiles.active: development

其他3个特定于配置文件的配置文件在C:\config文件夹中。

我正在为eclipse使用gradle插件。当我尝试做一个“bootRun”,我设置命令行参数在我的gradle配置在eclipse为

 -Dspring.profiles.active=staging -Dspring.config.location=C:\Config

然而,命令行属性没有得到反映,我的活动概要文件总是被设置为开发(这是我在应用程序中提到的一个)。yml文件)。此外,C:\Config文件夹不会搜索配置文件特定的配置文件。

我想我遗漏了一些东西。过去两天我一直在想这件事。但运气不好。我非常感激你的帮助。


当前回答

当通过Maven插件设置配置文件时,你必须通过run.jvmArguments来完成

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"

带有调试选项:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=jpa"

我看过很多人的旅行,希望能有所帮助

其他回答

就像一个插件,如果你有一个属性在你的应用程序中提到。属性文件,你需要覆盖另一个配置文件的属性,你可以使用下面的属性spring.config。附加位置(使用-D,因为从命令行传递) 我们以前用这个是因为我们有一个应用。jar中的属性和每个服务器配置文件夹中的一个外部属性,用于覆盖任何服务器特定的属性。

我必须补充一点:

bootRun {
    String activeProfile =  System.properties['spring.profiles.active']
    String confLoc = System.properties['spring.config.location']
    systemProperty "spring.profiles.active", activeProfile
    systemProperty "spring.config.location", "file:$confLoc"
}

现在bootRun将获取概要文件和配置位置。

非常感谢@jst的指点。

这将解决你在windows上的问题:

mvn spring-boot:run -D"spring-boot.run.profiles"=env

当通过Maven插件设置配置文件时,你必须通过run.jvmArguments来完成

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"

带有调试选项:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=jpa"

我看过很多人的旅行,希望能有所帮助

我认为您的问题可能与spring.config.location没有以“/”结束路径有关。

引用文档

如果spring.config.location包含目录(而不是文件),它们应该以/结尾(并且在加载之前会附加spring.config.name生成的名称)。

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files