我正在使用properties-maven-plugin从自定义属性文件加载属性。 属性文件名取决于所选的构建配置文件。 每个vpn - $ {environment} .properties文件都包含两个属性,例如对于dev环境: vpn.username = dev_us ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在使用 Maven 过滤来创建一个属性文件,该文件将具有给定堆栈的专用属性。
我有一个带有类似属性的 application.properties 文件
elasticsearch.members=${elasticsearch.members}
我有两个配置文件
config.ppd.properties 其中有
elasticsearch.members=ppd-es-01:9700;ppd-es-02:9700;ppd-es-03:9700
config.prod.properties 其中有
elasticsearch.members=prod-es-01:9700;prod-es-02:9700;prod-es-03:9700
我有 2 组服务器,别名为 ppd-es-nn 和 prod-es-nn
这一切都很好。 当我使用 ppd 配置文件生成工件时,我在 application.properties 文件中获得了正确的属性。
但我真正想做的是在 application.properties 文件中以某种方式完成这种连接。 因此,我可以在 application.properties 文件中指定 elasticsearch 服务器的数量和模式。
elasticsearch.members=${environment}-es-[index]:9700;...
有什么方法可以实现吗? 也许将进行过滤的 class 子类化? 这个例子有点做作,因为我想保持简单。 但我想做的一件事是 append 每个服务器别名的部署模式(蓝色或绿色)。 虽然我可以在过滤器文件中执行此操作,但最好在 application.properties 文件中执行此操作,这样过滤器 class 中的拼写错误不会弄乱别名。
我喜欢用于此类任务的 groovy-maven-plugin。 下面是未经测试的,但应该给你的想法。
<properties>
<!-- these may be overridden on the cmdline -->
<target.env>ppd</target.env>
<es.server.count>2</es.server.count>
<properties>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version><latestPluginVersionHere></version>
<executions>
<execution>
<id>build-es-host-list</id>
<!-- phase must be earlier than process-resources -->
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<![CDATA[
def sb = new StringBuilder()
def targetEnv = project.properties.getProperty('targetEnv')
def numServers = project.properties.getProperty('es.server.count')
(1..numServers).eachWithIndex { it, idx ->
sb.append("${targetEnv}-es-${idx}:9700")
if (idx < numServers) {
sb.append(';')
}
}
project.properties.setProperty('elasticsearch.members', sb.toString())
]]>
</source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.12</version>
</dependency>
</dependencies>
</plugin>
然后,application.properties 文件行需要使用属性:
elasticsearch.members=${elasticsearch.members}
POM 需要为要在文件中使用的计算值启用资源过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.