Creating a zip archive of the maven "target" directory

77,364

Solution 1

If the bin predefined assembly descriptor doesn't suit your needs, then you have three options:

  1. Using the maven-assembly-plugin - The maven-zip-plugin never came out because the assembly plugin can do everything the zip plugin was doing, and more, see MNG-2243.
  2. Using the maven-antrun-plugin (and maybe the build-helper-plugin to attach the zip) - There is an example here (and this looks more verbose than the assembly plugin at the end).
  3. Writing your own plugin - why would you do this when you have the assembly plugin.

Personally, I would just use the maven-assembly-plugin with the following zip.xml descriptor:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}</directory>
    </fileSet>
  </fileSets>
</assembly>

And in your POM:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/zip.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- append to the packaging phase. -->
      <goals>
        <goal>single</goal> <!-- goals == mojos -->
      </goals>
    </execution>
  </executions>
</plugin>

Solution 2

If you don't have any "special" needs for the generated .zip file, you can use one of the pre-defined Maven Assembly descriptors. The pre-defined assembly descriptors make it easy to quickly and easily create specific assemblies without having to provide your own assembly descriptor. Suppose you wanted to use the bin pre-defined descriptor. Then in the plugins section of your POM's build section, you could just add the following.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <descriptorRefs>
      <descriptorRef>bin</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

Of course, as always with Maven, if you want to do something beyond the default configuration, you'll have to create your own configuration, and in this case, that means your own assembly descriptor.

The list of pre-defined descriptors is documented here.

Share:
77,364
Yaneeve
Author by

Yaneeve

family man ⊗ coder ⊗ geek ⊗ history buff ⊗ outdoors lover ⊗ wannabe audiophile

Updated on October 18, 2020

Comments

  • Yaneeve
    Yaneeve over 3 years

    I wish to create a zip archive of my "target" directory (${project.build.directory). using the maven-assembly-plugin seems to me like overkill for such a simple task (and a bit complicated - why must I use another file, the assembly descriptor, for such a task) I can't locate the seemingly more simple maven-zip-plugin in the http://repo1.maven.org/maven2 repository.

    Any input?

  • Yaneeve
    Yaneeve about 14 years
    Not bad options. Yet at the end of my process the target directory simply contains an exe file and a directory with other exe files - al linstallers. I simply wish to zip them all up together
  • RTBarnard
    RTBarnard about 14 years
    So your trouble is that the default descriptors are going to package up more than you need, such as the contents of working folders, etc.? Is it the case that there's a specific folder under target that you want packaged (e.g. ${project.build.directory}/installers) but you want the other directories omitted? Or do you have some other objective?
  • Yaneeve
    Yaneeve about 14 years
    I had deleted all unwanted files and directories from the ${project.build.directory} directory and just wish to zip up its contents completely. If you do not have a suggestion better than Pascal's I guess I'll just have to create that "redundant" assembly file as he showed and opt for his solution...
  • febot
    febot almost 11 years
    Doesn't work - the built-in "bin" is used. And I can't figure out how to point to custom non-built-in.
  • Nick Wilson
    Nick Wilson over 7 years
    There is one advantage of using the antrun plugin over the assembly plugin as it stores the file sizes differently inside the zip. If you're reading the zip with 'ZipInputStream' and use 'getSize()' you'll get the entry size with zips created by antrun, for zips created with assembly it returns -1.