使用Maven和docker-maven-plugin我已经配置了Apache Kafka + ZooKeeper容器:
<image>
<name>wurstmeister/zookeeper:${zookeeper.version}</name>
<alias>zookeeper</alias>
<run>
<ports>
<port>${zookeeper.port}:2181</port>
</ports>
</run>
</image>
<image>
<name>wurstmeister/kafka:${kafka.version}</name>
<alias>kafka</alias>
<run>
<ports>
<port>9093:9092</port>
</ports>
<links>
<link>zookeeper:zookeeper</link>
</links>
<env>
<KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>
<KAFKA_ADVERTISED_PORT>9093</KAFKA_ADVERTISED_PORT>
<KAFKA_ZOOKEEPER_CONNECT>zookeeper:${zookeeper.port}</KAFKA_ZOOKEEPER_CONNECT>
</env>
</run>
</image>
如您所见,为了使其正常工作,我必须提供主机系统的实际IP地址:
<KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>
有没有办法在Maven或docker-maven-plugin自动获取此IP地址而无需对其进行硬编码?
更新
我找到了允许我检索主机IP地址的插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>get-local-ip</id>
<goals>
<goal>local-ip</goal>
</goals>
<configuration>
<localIpProperty>local.ip</localIpProperty>
</configuration>
</execution>
</executions>
</plugin>
但是为了使用它,我需要在其余的Maven命令之前执行build-helper:local-ip
目标:
mvn build-helper:local-ip docker:start
如何将此插件与某个阶段/目标绑定,以便在某些早期初始化阶段自动调用它,而不需要每次手动调用build-helper:local-ip
?
From the docs for build-helper-maven-plugin:
默认情况下绑定到生命周期阶段:process-test-classes
您可以将其更改为绑定到您想要的任何阶段,例如
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>local-ip</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
</plugin>