Deploy submodules with dependencies in multi module maven project

2k views Asked by At

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

2

There are 2 answers

0
Anshul On BEST ANSWER

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

6
Dmitry Rakovets On

What you describe works correctly under the following conditions:

Project structure:

project
├── module-1
│   ├── Dockerfile
│   ├── pom.xml
│   └── src
├── module-2
│   ├── pom.xml
│   └── src
├── module-3
│   ├── pom.xml
│   └── src
├── module-4
│   ├── Dockerfile
│   ├── pom.xml
│   └── src
└── pom.xml

Parent POM:

<build>
    <pluginManagement>
        <plugins>
            <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>
                <executions>
                    <execution>
                        <id>docker-build</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>docker-push</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <contextDirectory>${basedir}/</contextDirectory>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Submodule POM:

<build>
    <plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Build docker image for module 1 with command:

mvn -pl module-1,module-2,module-3 -am clean install

Output:

[INFO]  ---> 04ceb1feb02c
[INFO] Step 4/4 : ENTRYPOINT ["java", "-jar", "/app.jar"]
[INFO] 
[INFO]  ---> Running in 2d9866a14643
[INFO] Removing intermediate container 2d9866a14643
[INFO]  ---> ad16766ec096
[INFO] Successfully built ad16766ec096
[INFO] Successfully tagged ***/module-1:1.0-SNAPSHOT
[INFO] 
[INFO] Detected build of image with id ad16766ec096
[INFO] Building jar: ***/maven-docker/module-1/target/module-1-1.0-SNAPSHOT-docker-info.jar
[INFO] Successfully built ***/module-1:1.0-SNAPSHOT
[INFO]          ------------------------------------------------------------------------
[INFO] Reactor Summary for module-2 1.0-SNAPSHOT:
[INFO] 
[INFO] module-2 ............................................ SUCCESS [  0.797 s]
[INFO] module-3 ............................................ SUCCESS [  0.051 s]
[INFO] module-1 ............................................ SUCCESS [  5.019 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.016 s
[INFO] Finished at: 2021-07-30T14:47:53+03:00
[INFO] -----------------------------------------------------------------------

Used Dockerfile from module-1 because it has 4 steps (and module-4 has 2 steps).

Check docker image:

$ docker image list

Output:

REPOSITORY                     TAG              IMAGE ID       CREATED          SIZE
***/module-1               1.0-SNAPSHOT     ad16766ec096   16 minutes ago   456MB
...