Maven, Docker 获取主机系统的实际IP地址

Maven, Docker get the actual IP address of host system

使用 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

如何将此插件与某些 phase/goal 绑定以便在某些早期初始化阶段自动调用它而无需每次手动调用 build-helper:local-ip

From the docs for build-helper-maven-plugin:

Binds by default to the lifecycle phase: 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>

来自 build-helper-maven-plugin 的 local-ip 仅返回 127.0.0.1

在我的用例中,网络地址是必需的 - 类似于 192.168.17.112

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>
                    java.net.DatagramSocket socket = new java.net.DatagramSocket()
                    socket.connect(java.net.InetAddress.getByName('google.com'), 80)
                    project.properties['host.address'] = socket.getLocalAddress().getHostAddress()
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

然后 属性 host.address 包含想要的网络 IP。

(不要给我投票,而是给对我有帮助的答案投票:Getting the IP address of the current machine using Java