passing name of generated jar file from Maven pom.xml to Dockerfile

Viewed 3010

I have a java/hibernate/jpa/maven project that I am trying to convert so that it (the .jar file created from the project) is part of a Docker Image. I am also using the Spotify plugin.

Below, for the following Docker command

ADD <full_path_of_generated_file>.jar app.jar

The value full_path_of_generated_file is generated from the pom.xml file (on the Maven side). This information is needed in order for Docker to generate the image properly.

How can one get the name of the generated file over to the Dockerfile?

TIA

Plugin area of the Maven pom.xml file

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

        <configuration>
            <imageName>apx/${project.artifactId}</imageName>
            <dockerDirectory>${project.basedir}/docker</dockerDirectory>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <!-- <version>2.6</version> -->
        <executions>
            <execution>
                <id>analyze</id>
                <phase>package</phase>
                <configuration>
                    <failBuild>true</failBuild>
                    <ignoreDirect>false</ignoreDirect>
                </configuration>
            </execution>
        </executions>
        <configuration>

            <finalName>${project.artifactId}</finalName>
            <archive>
                <manifest>
                    <mainClass>ccinfw.MainApplication</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>

[... snip ...]

<artifactId>ccinf</artifactId>

Below is a copy of the Dockerfile being used

FROM java:8
VOLUME /tmp
ADD <full_path_of_generated_file>.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
2 Answers

Inside pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/docker</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Make sure to place the Dockerfile in the location mentioned in pom, so that it could build and copy the Dockerfile with replacing the references dynamically.

enter image description here

Inside Dockerfile

ADD @project.artifactId@-@project.version@.jar app.jar

credits: https://github.com/vmudigal/microservices-sample

Related