【问题标题】:Spring Boot Profile-specific Properties build using Maven Configuration使用 Maven 配置构建 Spring Boot Profile-specific Properties
【发布时间】:2019-01-24 12:39:39
【问题描述】:

如何使用 maven 为 Spring Boot 应用程序构建环境特定的 War 文件。我创建了 3 个配置文件配置文件,放置在 src/main/resource/ 文件夹中。

  1. application.prod.properties
  2. application.dev.properties
  3. application.test.properties

在将项目作为 Spring Boot 应用程序执行时,我可以通过在 VM 参数选项卡中使用值“-Dspring.profiles.active=dev”指定所需的配置文件类型来运行应用程序。

在这里,作为 Spring Boot 应用程序运行时,我可以指定所需的配置文件。以同样的方式,当我需要使用不同的配置文件进行 MAVEN 安装时。有没有办法在 Maven 安装目标的运行配置中将配置文件指定为 VM 参数列表的一部分。

我有限制,不能接触现有的 java 代码。

我正在使用 STS IDE、Spring boot 1.5.2.RELEASE 版本、Java 1.8 和 oracle db。

以同样的方式也帮助我如何在 Jenkins 中配置配置文件。

我的个人资料配置有两个要求。

  1. 在 STS IDE 中将应用程序作为带有 VM args 的 spring boot 应用程序运行。 使用了以下 VM ARGS -Dspring.profiles.active=dev

块引用

(这里我在开发环境中本地启动 SpringBootApp 时遇到异常)。


应用程序启动失败


说明:

无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

行动:

如果您想要一个嵌入式数据库,请在类路径中放置一个受支持的数据库。如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(配置文件“dev”当前处于活动状态)。

块引用

  1. 如何使用 maven install 通过动态指定配置文件来生成战争文件来执行相同的操作。我找不到解决方案。

【问题讨论】:

  • 我不太明白...您是否要求 hot 使用 cmd 参数激活特定的 maven 配置文件(就像您使用 -Dspring.profiles.active=prod 为 spring 配置文件所做的那样)?
  • Spring 配置文件的全部目的是为所有环境提供单个 war / jar 文件。我认为如果您为每个环境创建不同的 war / jar 文件,它将无法达到目的。你能详细说明为什么每个环境需要不同的战争/罐子吗?
  • 实际上我需要动态拾取执行 maven 安装的环境特定文件。我不想要差异战争文件。不应在 pom.xml 中配置所需的配置文件。我需要避免在 pom.xml 中为 diff env 进行更改。
  • 我不需要多个战争文件。这里的要求是一旦代码准备好部署,我需要使用命令行参数加载配置。或任何其他方式来避免提醒属性文件中的配置。

标签: java maven spring-boot war spring-profiles


【解决方案1】:

在您的主要application.properties 文件中,将spring.profiles.active 设置为@myActiveProfile@(或您希望的任何名称)

spring.profiles.active=@myActiveProfile@

将您的 application.properties 文件添加为过滤资源,以便在构建阶段替换占位符 myActiveProfile

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    …
</build>

将配置文件部分添加到您的 pom.xml

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <myActiveProfile>dev</myActiveProfile>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <myActiveProfile>prod</myActiveProfile>
        </properties>
    </profile>
</profiles>

基本上,您可以在执行特定目标时指定 Maven 配置文件。例如mvn install -P profileName。在配置文件中,您可以创建一个属性,该属性可以在运行诸如 mvn install -DmyActiveProfile=foo 之类的目标时从命令行传递

希望这会有所帮助。

有用的链接

How to set spring active profiles with maven profiles https://maven.apache.org/guides/introduction/introduction-to-profiles.html

【讨论】:

  • 是的,此配置有效。我的要求是差异。我不应该再次触摸 pom 文件来设置差异配置文件。我想在使用 maven 构建战争文件时动态传递配置文件。就像在运行 Spring Boot 应用程序时一样,我将配置文件类型动态指定为 VM args 的一部分。
  • 哦,是的。您将不再需要触摸 pom.xml :-)。我更新了我的答案,以展示如何在 pom.xml 中预先定义所有配置文件。然后你可以像mvn install -DmyActiveProfile=devmvn install -DmyActiveProfile=prod一样执行你的maven目标
  • 您不需要添加任何听众 AFAIK
  • 我的应用程序已经在开发中。需要做最少的 Java 编码。
【解决方案2】:

在这里,首先我建议将您的属性文件重命名为 application-prod.properties、application-dev.properties 和 application-test.properties。

其次,maven install 的目标是编译和构建你的项目。

如果你还想运行你的应用程序,我建议在安装时使用 spring-boot-maven-plugin。

你可以使用类似下面的 maven 命令

mvn clean install spring-boot:run

下面的一些链接供您参考

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html

https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 2018-06-12
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2015-09-30
    • 2014-04-15
    相关资源
    最近更新 更多