Search code examples
mavendockerpackaging

Can Maven package a WAR as .tar.gz?


I'm building a WAR application in Maven and I need to deploy it to Docker. The issue is that docker doesn't support the zip format (but can use a .tar.gz or .tar.xz).

So, how can I package my mywebapp-1.0.0.war as mywebapp-1.0.0.tar.gz?

We have this requirement so it would become easier to perform the hardening steps for the docker image.

I tried using the Assembly plugin and it produces a target/mywebapp-1.0.0.tar.gz. However, it ends up placing the whole WAR file inside a .tar.gz file, and NOT its contents. Here's my assembly.xml file:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
                      http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>distribution</id>
  <formats>
    <format>tar.gz</format>
  </formats>
  <fileSets>
    <fileSet>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>target/mywebapp-1.0.0.war</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

Solution

  • Although I find the requirement strange (the application server should unpack your war, not you), I guess it is possible by tarring the directory in target that is used to build the war.

    Before the package step, all the files for the war are gathered in a directory just below target (I forgot the name, but it should be easy to reverse engineer) - just pack that directory by using the assembly plugin.

    Edit by the OP:

    I wanted to accept this answer but I also wanted it to be useful to someone else so I added the working solution (assembly.xml file). Here it is:

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
        http://maven.apache.org/xsd/assembly-2.0.0.xsd">
      <id>docker</id>
      <formats>
        <format>tar.gz</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <fileSets>
        <fileSet>
          <directory>target/${project.build.finalName}</directory>
          <outputDirectory>.</outputDirectory>
        </fileSet>
      </fileSets>
    </assembly>
    

    This solution generates the file target/mywebapp-1.0.0-docker.tar.gz in addition to the typical war file.

    Notice it added the docker classifier.