繁体   English   中英

Maven 配置文件过滤与列表

[英]Maven profile filtering with lists

我正在使用 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.

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