简体   繁体   English

如何在 Maven pom.xml 中重用 application.properties (Spring Boot) 中的属性?

[英]How to reuse properties from application.properties (Spring Boot) in Maven pom.xml?

I would like to define database url, username, password in one place.我想在一个地方定义数据库url, username, password Currently I have目前我有

application.properties with application.properties

spring.datasource.url=....
spring.datasource.username=sa
spring.datasource.password=00

And pom.xml withpom.xml

  <plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.1.2</version>
    <configuration>
      <url>....</url>
      <user>sa</user>
      <password>00</password>
    </configuration>
  </plugin>

So probably I need to reuse property values defined in application.properties .所以可能我需要重用application.properties定义的属性值

This <password>${spring.datasource.password}</password> doesn't work.这个<password>${spring.datasource.password}</password>不起作用。 Also I tried我也试过

 <systemProperties>
    <systemProperty>
      <name>url</name>
      <value>....</value>
    </systemProperty>
    ...
 </systemProperties>

Neither approach is working.这两种方法都不起作用。

You can do the reverse by building a properties file from your pom file.你可以通过从你的 pom 文件构建一个属性文件来做相反的事情。 In your properties file you'd use something like:在您的属性文件中,您将使用以下内容:

password=${pom.password}

And your pom file would have something like:你的 pom 文件会有类似的内容:

<password>your_db_password</password>

Then at:然后在:

mvn clean package

Maven will build your properties file. Maven 将构建您的属性文件。

Here's a simple tutorial: Add Maven Build Information ...这是一个简单的教程: 添加 Maven 构建信息...

As the top two answers from this question (as suggested in rmlan's comment) suggest, use the properties-maven-plugin like this:正如这个问题的前两个答案(如 rmlan 的评论中所建议的)所建议的,像这样使用properties-maven-plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>${basedir}/src/main/resources/application.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>

Then with your application.properties do the following:然后使用您的application.properties执行以下操作:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.1.2</version>
    <configuration>
        <url>${spring.datasource.url}</url>
        <user>${spring.datasource.username}</user>
        <password>${spring.datasource.password}</password>
    </configuration>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 POM.xml 读取 application.properties - How to read application.properties from POM.xml 在application.properties中使用pom.xml的属性值 - Using property values of pom.xml in application.properties Application.properties spring-boot 在 pom.xml 中产生错误。 application.properties 中的 mysql 连接存在问题 - Application.properties spring-boot produce an error in pom.xml. Problem with mysql connection in application.properties Maven如何在Spring Tool Suite中自动从父pom.xml继承属性 - How does Maven automatically inherit properties from parent pom.xml in Spring Tool Suite 在Spring Boot中,如何获取log4j2.properties文件以从pom.xml读取文件名 - How to get log4j2.properties file to read filename from pom.xml in Spring Boot Spring Boot - 无法从application.properties解析xml中的属性 - Spring Boot - property could not be resolved in xml from application.properties 来自application.properties的Spring Boot配置 - Spring Boot configuration from application.properties 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 如何从Spring Boot中的application.properties中分配注释值? - How to assign annotation value from application.properties in Spring Boot? Maven读取.properties以在pom.xml中使用 - Maven read .properties to use in pom.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM