How to build a ZIP output artifact in Maven

It’s quite a common situation that you have a bunch of files, most or all of them committed in svn, and need to make a release of this so that it can be reused with the Maven’s dependency mechanism.

There are several ways how to accomplish that:

  • use maven-assembly-plugin
  • use maven-antrun-plugin to build and build-helper-maven-plugin to attach
  • create custom plugin that handles both packaging and attaching

Each of them has its advantages in some situations, but my favorite is the second option for most cases.

Real life example – artifact with various svn output files

In some of my projects, I need to check that the code works properly with xml output files from Subversion. I could easily put such files among test resources, but the it would be there many times, and I would always have to check if it is “representative” enough etc.

So I decided to create a module, which will create the data during its build (see the maven-antrun-plugin).

These data are packaged into a zip file which is then marked as output artifact with type “zip” (see the build-helper-maven-plugin).

Look at this pom excerpt  (complete pom.xml is available here):

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
... phase=package, goal=run
                <mkdir dir="${}"/>
                <exec executable="svn" failonerror="true" dir="${}" taskname="svn-checkout">
                <zip destfile="${}/${}.zip">
... phase=package, goal=attach-artifact

What are the advantages over other mentioned alternatives ?

  • extensibility – the “generator” part is straightforward, so, as long as ant is enough for us it costs just a few lines of ant code
  • compactness – we could reach similar result with several invocations of exec-maven-plugin for preparing data, plus maven-assembly-plugin to pack and attach it, but that would be far far more verbose and harder to maintain

Just for completenes, it’s good to say when using the other approaches is more relevant:

  • custom maven plugin – when the algorithm for generating data is too complex to be written in ant, or needs other sources that are not reachable from antrun, like dependencies
  • maven-assembly-plugin – when we need to control file permissions in tgz/gz packages while running the build on windows

Do you have experience in this area ? Then please post your comments…

  • Ivan Najman

    Great work man, just what i needed.

    What i`ve been doing is trying to go without flexmojos for building my flex project.
    Ive been using ANT to build flex project and ant-run plugin for maven to execute it.
    With your help ive packaged everything to artifact (.swc)

    Thanks again, great post

  • David

    Nice job, very useful article.

    Another tutorial about the build-helper-maven plugin i found useful

  • Taher Ahmed

    Thanks for this. This was really really helpful

  • Sri

    Hi, Good Article. The link to the complete xml seems broken. Can you update the link ref.

  • Petr Kozelka

    @Sri: thanks, fixed