在 Spring Boot 中的 application.properties 中使用 Maven 属性 [英] Using Maven properties in application.properties in Spring Boot

查看:52
本文介绍了在 Spring Boot 中的 application.properties 中使用 Maven 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 pom.xml 中的属性加载到 application.properties 中.我想创建两个配置文件:dev 和 prod 以使用不同的数据库 url.我使用 Jenkins 作为 CI,在我的所有应用程序中(主要是 Spring MVC,没有 Boot 项目)都使用 maven 配置文件部署到 Tomcat.有没有可能使用 maven 属性来做到这一点?我试过这样的事情:spring.datasource.url=${jdbc.url}

解决方案

在执行此操作之前,请考虑将属性文件从可部署的包中外部化.通过这种方式,您可以在每个环境中部署相同的编译.它将为您的 Jenkins 节省一些实际上不必要的工作.最佳做法是只构建一次应用程序,但是,如果您不相信,请按以下步骤操作.

  1. 在您的 pom.xml 中定义具有适当属性值的配置文件.

    <id>dev</id><属性><jdbc.url>your_dev_URL</jdbc.url></属性></个人资料>

  2. 设置Maven 资源插件来过滤目录包含您的 application.properties 文件.

    <资源><资源><目录>src/main/resources</directory><filtering>true</filtering></资源></资源>...</build>

  3. 如果你使用 Spring Boot 1.3 或更高版本,你应该意识到为了避免 Spring Boot 占位符和 Maven 资源插件过滤的令牌之间的冲突,框架引入了 需要对过滤值使用不同语法的解决方案.>

    现在,您应该使用 @property.key@ 而不是 ${property.key}.在这种情况下,您的 application.properties 必须包含以下示例才能按预期工作:

    spring.datasource.url=@jdbc.url@

您还可以查看关于为不同的 Maven 配置文件分离 Spring 属性文件.这样你就可以将你的 pom.xml 中的值具体化.

I'm trying to load properties from pom.xml into application.properties. I want to create two profiles: dev and prod to use different database urls. I'm using Jenkins as CI, in all my apps (Spring MVC mainly, without Boot project) are using maven profiles to deploy to Tomcat. There is any posibility to do this using maven properties? I tried something like that: spring.datasource.url=${jdbc.url}

Before you do it, consider externalizing the properties file out of your deployable package. This way you can deploy the same compilation on every environment. It will save your Jenkins some work that is actually unnecessary. The best practice is to build your application only once, however, if you are not convinced, here is how to do it.

  1. In your pom.xml define the profiles with appropriate values for the property.

    <profile>
        <id>dev</id>
       <properties>
           <jdbc.url>your_dev_URL</jdbc.url>
       </properties>
    </profile>
    

  2. Setup the Maven Resources Plugin to filter the directory which contains your application.properties file.

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

  3. If you use Spring Boot 1.3 or more, you should be aware of the fact that to avoid conflicts between Spring Boot placeholders and tokens filtered by the Maven Resources Plugin, the framework introduced a solution that requires using a different syntax for filtered values.

    Now, instead ${property.key} you should use @property.key@. In this case, your application.properties must contain the following sample to work as you expect:

    spring.datasource.url=@jdbc.url@
    

You can also check out a post about separating Spring properties files for different Maven profiles. That way you will externalize the values from your pom.xml.

这篇关于在 Spring Boot 中的 application.properties 中使用 Maven 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆