Why docker-maven-plugin fails to pick up correct jar file to include in docker image?

1.2k Views Asked by At

As far as I understand the build flow:

  1. maven-assembly-plugin builds /target/my-artifact-1.0-SNAPSHOT-jar-with-dependencies.jar

  2. maven-jar-plugin builds /target/my-artifact-1.0-SNAPSHOT.jar (not sure why we need it)

  3. docker-maven-plugin suppose to assemble artifact-with-dependencies /target/my-artifact-1.0-SNAPSHOT-jar-with-dependencies.jar

Nevertheless, for some reason, it picks up the wrong jar artefact.

What am I missing? How to make it work?

pom.xml

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.20.1</version>
    <extensions>true</extensions>
    <configuration>
        <images>
            <image>
                <name>my-artifact</name>
                <build>
                    <from>java:8-jre</from>
                    <volumes>
                        <volume>/target</volume>
                    </volumes>
                    <entryPoint>
                        <exec>
                            <arg>java</arg>
                            <arg>-jar</arg>
                            <arg>/target/my-artifact-1.0-SNAPSHOT-jar-with-dependencies.jar</arg>
                        </exec>
                    </entryPoint>
                    <assembly>
                        <descriptorRef>artifact-with-dependencies</descriptorRef>
                        <targetDir>/target</targetDir>
                        <mode>dir</mode>
                        <!--<basedir>/target</basedir>-->
                    </assembly>
                </build>
            </image>
        </images>
    </configuration>
    <executions>
        <execution>
            <id>docker-build</id>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
        <execution>
            <id>docker:start</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>docker:stop</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>create-archive</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <archive>
            <manifest>
                <mainClass>Client</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>Client</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
1

There are 1 best solutions below

2
On

To cite the documentation, :

     <descriptorRef>artifact-with-dependencies</descriptorRef>

will add the created artifact with the name ${project.build.finalName}.${artifact.extension} and all jar dependencies in the the targetDir (which is /maven by default).

Which is not what you want.

What you want I think is to customize the assembly to something like this:

<assembly>
 <targetDir>/target</targetDir>
 <inline>
    <fileSets>
      <fileSet>
        <directory>${project.basedir}/target/</directory>
        <outputDirectory>.</outputDirectory>
        <includes>
             <include>project-name-version-classifier-jar-with-dependencies.jar</include> 
         </includes>
         </fileSet>
       </fileSets>
     </inline>
   </assembly>

Bonus point: Have a look at Jib, it may better suit your needs if your are building an image for a Java project.