Maven,Docker获取主机系统

时间:2017-11-23 07:32:43

标签: maven docker maven-3 docker-maven-plugin

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

2 个答案:

答案 0 :(得分:1)

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>

答案 1 :(得分:1)

build-helper-maven-plugin中的document.getElementById('text').addEventListener('change', function() { this.value = removeRepeatedCharacters(this.value); }); function removeRepeatedCharacters(string) { return string .split('') .filter(function(item, pos, self) { return self.indexOf(item) == pos; }) .join(''); } 仅返回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> 包含所需的网络IP。

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