2

My app uses maven-shade-plugin to pack things into single fatjar and then I would like to build a docker image using dockerfile-maven-plugin, my problem is that I can set the pom file properly so it would work. What happens is that the docker plugin runs before the jar file was created...

I've tried to force the jar creation on prepare-package and the docker image build on package but it didn't work as expected... any ideas?

EDIT: added pom snippet

<build>
    <plugins>
      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>${dockerfile-maven.version}</version>
        <configuration>
          <repository>test-docker-image</repository>
          <tag>${docker.tag}</tag>
          <buildArgs>
            <JAR_FILE>${project.artifactId}-${project.version}-fat.jar</JAR_FILE>
            <CONFIGURATION_FILE>configuration.json</CONFIGURATION_FILE>
          </buildArgs>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>build</goal>
              <!-- <goal>push</goal> -->
            </goals>
            <phase>install</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>${maven.shade.plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>shade</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>io.vertx.core.Launcher</Main-Class>
                    <Main-Verticle>MyVerticle</Main-Verticle>
                  </manifestEntries>
                </transformer>
              </transformers>
              <minimizeJar>false</minimizeJar>
              <outputFile>${project.build.directory}/deploy/${project.artifactId}-${project.version}-fat.jar</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
Shvalb
  • 1,835
  • 2
  • 30
  • 60

1 Answers1

0

I had both of these same plugins listed in the same order you have (docker-maven-plugin before maven-shade-plugin) and was seeing the same issue. I discovered that Maven executes plugins in the same phase in the order that they are listed, so moving maven-shade-plugin to be first resolved the issue for me locally. (Older versions of Maven don't order plugins this way, so use the latest if possible.)

This doesn't explain why it is not working for you when you change them to use different phases, but I'd suggest at least trying it out that reordering. Also be sure that you are using the latest versions of related plugins, like maven-release-plugin.

I was still seeing the undesired behavior in my build environment after doing all of the above, due to the parent of my project containing an execution for docker-maven-plugin; the fact that I was customizing it and reordering it in my own project didn't help, although it is unclear why it worked locally. Maven build profiles can also have a similar impact on ordering. My solution there was to bind the docker-maven-plugin:build execution to the install phase.

The output of mvn help:effective-pom should let you see the exact listing of plugins, executions, and profiles so that you can see exactly what the ordering will be for your project. Note that profiles are executed bottom-to-top, which is the opposite of plugins!

Matthew Read
  • 1,365
  • 1
  • 30
  • 50