Create a zip with all dependencies with Maven

Jan picture Jan · Apr 19, 2011 · Viewed 34.7k times · Source

Possible Duplicate:
With Maven, how can I build a distributable that has my project's jar and all of the dependent jars?

maybe I'm blind, but can the maven assembly plugin just create a zip file containing the current project.jar and all my dependency jars? the "bin" id contains just my project.jar and the jar-with-dependencies puts all together in one jar which I don't like.

Answer

jewles picture jewles · Apr 26, 2011

You need a custom assembly descriptor to do what you want. It's a combination of the bin and the jar-with-dependencies predefined descriptors. Created a custom assembly descriptor called assembly.xml in the src/main/assembly folder.

Here's an example that is basically the bin descriptor with the dependency set added:

    <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>
  <formats>
    <format>tar.gz</format>
    <format>tar.bz2</format>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>README*</include>
        <include>LICENSE*</include>
        <include>NOTICE*</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}/site</directory>
      <outputDirectory>docs</outputDirectory>
    </fileSet>
  </fileSets>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Then change your pom to use a descriptor instead of a descriptorRef to tell it where your custom descriptor is.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        [...]
</project>