mavendockermaven-3docker-maven-plugin

Maven, Docker get the actual IP address of host system


With Maven and docker-maven-plugin I have configured Apache Kafka + ZooKeeper containers:

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

As you can see, in order to get it working I have to provide the actual IP address of my host system:

<KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>

Is there any way in Maven or docker-maven-plugin to get this IP address automatically with no needs to hardcode it?

UPDATED

I found plugin that allows me to retrieve the host IP address:

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

but in order to use it I need to execute the build-helper:local-ip goal before rest of the Maven commands:

mvn build-helper:local-ip docker:start

How to bind this plugin with some phase/goal in order to invoke it automatically on some early initialization phase with no needs to invoke build-helper:local-ip manually each time?


Solution

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

    Binds by default to the lifecycle phase: process-test-classes

    You can change it to bind to whatever phase you want, e.g.

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