[英]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
with和pom.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.