javamavendeploymentdockerfiledocker-maven-plugin

Deploy submodules with dependencies in multi module maven project


I have a project structure like:

module-1
    pom.xml
    Dockerfile
module-2
    pom.xml
module-3
    pom.xml
module-4
    pom.xml
    Dockerfile
pom.xml

module-2 and module-3 are dependencies for module-1 and module-4

Now I want to deploy module-4 and module-1 independently. Now in my parent pom, I have added dockerfile-maven-plugin and added <skip> to true, while for both child projects I have skip false because I want to deploy them. However when I am trying to deploy module-1 its picking Dockerfile for module-4. So how should I configure my project so that each module pick it's respective Dockefile

My parent pom section looks like:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.3.6</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

My child pom section for both child looks like :

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.3.6</version>
    <dependencies>
        <dependency>
            <groupId>jakarta.activation</groupId>
            <artifactId>jakarta.activation-api</artifactId>
            <version>1.2.2</version>
        </dependency>
    </dependencies>
    <configuration>
        <skip>false</skip>
        <repository>${docker.image.prefix}/${project.artifactId}</repository>
        <tag>${project.version}</tag>
        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
        <contextDirectory>${basedir}/</contextDirectory>
    </configuration>
</plugin>

Also , in the jenkins build I am running command : mvn -pl module-1,module-2,module-3 -am clean install

My Dockerfile for module-1 looks like

FROM openjdk:11-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} module-1.jar
CMD java $JVM_OPTS -Djava.security.egd=file:/dev/./urandom -jar /module-1.jar

Need help


Solution

  • So, what we can do is for the parent pom :

    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.3.6</version>
        <configuration>
            <skip>true</skip>
        </configuration>
    </plugin>
    

    In the child pom :

    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <configuration>
            <skip>false</skip>
            <repository>${docker.image.prefix}/${project.artifactId}</repository>
            <tag>${project.version}</tag>
            <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
            <buildArgs>
                <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
            </buildArgs>
            <contextDirectory>${project.basedir}/</contextDirectory>
        </configuration>
     </plugin>
    

    Override skip to false

    So now if we run mvn dockerfile:build , it would run docker plugin and read Dockerfile for each sub-module . However if you want to apply this to a specific module , you can run mvn -pl your-module dockerfile:build so the other modules that we don't want will not run in the reactor summary.

    Also , you can add the executions in the child where you want