繁体   English   中英

在application.conf中使用Maven配置文件属性

[英]Use maven profile properties in application.conf

在我的pom.xml文件中,我设置了多个配置文件。 我想在我的application.conf文件中使用当前配置文件的值。 Ninja Framework文档仅提及模式配置 ,但是我找不到与配置文件配置有关的任何内容。

例如:文档中提到

database.name=database_production   # will be used when no mode is set (or prod)
%prod.database.name=database_prod   # will be used when running in prod mode
%dev.database.name=database_dev     # will be used when running in dev mode
%test.database.name=database_test   # will be used when running in test mode

如何根据当前使用的配置文件设置不同的数据库名称?

您可以将要替换的内容添加为配置文件定义的属性,然后启用如下所示的Maven资源过滤:

<build>
  ..
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  ..
</build>
<profiles>
  <profile>
    <id>developmentProfile</id>
    <properties>
      <dbName>database.name=database_dev</dbName>
    </properties>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
  </profile>
  <profile>
    <id>productionProfile</id>
    <properties>
      <dbName>database.name=database_prod</dbName>
    </properties>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
</profiles>

请注意,如果您使用spring-boot ,则应像这样引用application.properties文件中的属性,以使其正常工作:

someOtherProp = something
@dbName@

之后,构建应用程序应正确过滤属性:

mvn clean install -PdevelopmentProfile

结果:

someOtherProp=something
database.name=database_dev

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM