在 Spring Boot 中的 application.properties 中使用 Maven 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36703499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:03:26  来源:igfitidea点击:

Using Maven properties in application.properties in Spring Boot

springmavenspring-bootspring-boot-maven-plugin

提问by Jakub Pomyka?a

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}

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

回答by Daniel Olszewski

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.

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

  1. In your pom.xmldefine 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 Pluginto filter the directory which contains your application.propertiesfile.

    <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 syntaxfor filtered values.

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

    [email protected]@
    
  1. 在您的pom.xml 中定义具有适当属性值的配置文件。

    <profile>
        <id>dev</id>
       <properties>
           <jdbc.url>your_dev_URL</jdbc.url>
       </properties>
    </profile>
    
  2. 设置Maven 资源插件以过滤包含application.properties文件的目录。

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        ...
    </build>
    
  3. 如果您使用 Spring Boot 1.3 或更高版本,您应该意识到为了避免 Spring Boot 占位符和由 Maven 资源插件过滤的令牌之间的冲突,该框架引入了一个解决方案,该解决方案需要对过滤值使用不同的语法

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

    [email protected]@
    

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.

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

回答by Xtreme Biker

Of course there is. Just use Maven Filteringover your application.propertiesfile and Maven will write your profile specific values in the file.

当然有。只需在application.properties文件上使用Maven 过滤,Maven 就会在文件中写入您的配置文件特定值。

However, you must understand that while Maven profiles work at application package/build time, Spring Boot ones do at runtime. In other words, with Maven profiles you'll get profile specific immutable builds, while when using the ones from Spring Boot you'll be able to change your application configuration every time before launching it or even while it's running.

但是,您必须了解,虽然 Maven 配置文件在应用程序打包/构建时工作,但 Spring Boot 配置文件在运行时工作。换句话说,使用 Maven 配置文件,您将获得配置文件特定的不可变构建,而使用 Spring Boot 中的配置文件时,您将能够在每次启动之前甚至在运行时更改应用程序配置。

See also:

也可以看看:

回答by Teodor Dimitrov

In addition to Daniel Olszewski, in my yml file I got an error: (Do not use @ for indentation)

除了 Daniel Olszewski 之外,在我的 yml 文件中我得到了一个错误:(不要使用 @ 进行缩进)

So i fixed it by adding single quotes. Someone might find it helpful.

所以我通过添加单引号来修复它。有人可能会发现它有帮助。

spring:
 datasource:
  url: '@jdbc.url@'