简体   繁体   English

在不同阶段运行 maven-shade 和 dockerfile-maven

[英]Running maven-shade and dockerfile-maven in different phases

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.我的应用程序使用maven-shade-plugin将东西打包到单个fatjar ,然后我想使用dockerfile-maven-plugin构建一个docker映像,我的问题是我可以正确设置 pom 文件以便它可以工作。 What happens is that the docker plugin runs before the jar file was created...会发生什么是docker插件在创建jar文件之前运行...

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?我试图强制在prepare-package上创建jar并在package上构建docker映像,但它没有按预期工作......有什么想法吗?

EDIT : added pom snippet编辑:添加了 pom 片段

<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>

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.我以与您相同的顺序列出了这两个相同的插件( docker-maven-pluginmaven-shade-plugin之前)并且看到了同样的问题。 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.我发现 Maven 按照它们列出的顺序在同一阶段执行插件,因此移动maven-shade-plugin以首先在本地为我解决问题。 (Older versions of Maven don't order plugins this way, so use the latest if possible.) (旧版本的 Maven 不会以这种方式订购插件,因此请尽可能使用最新版本。)

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 .还要确保您使用的是相关插件的最新版本,例如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 ;由于我的项目的父项目包含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. Maven 构建配置文件也可以对排序产生类似的影响。 My solution there was to bind the docker-maven-plugin:build execution to the install phase.我的解决方案是将docker-maven-plugin:build执行绑定到install阶段。

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. mvn help:effective-pom的输出应该让您看到插件、执行和配置文件的确切列表,以便您可以准确地看到您的项目的排序。 Note that profiles are executed bottom-to-top, which is the opposite of plugins!请注意,配置文件是自下而上执行的,这与插件相反!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM