Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a zip with all dependencies with Maven [duplicate]

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.

like image 401
Jan Avatar asked Apr 19 '11 13:04

Jan


People also ask

How do I force maven to download dependencies again?

We can use -U/--update-snapshots flag when building a maven project to force maven to download dependencies from the remote repository. Here, -U,--update-snapshots : Forces a check for missing releases and updated snapshots on remote repositories.

Does maven bundle dependencies into jar?

The difference is that the Maven Assembly Plugin will automatically copy all required dependencies into a jar file. In the descriptorRefs part of the configuration code, we provided the name that will be added to the project name. Output in our example will be named as core-java-jar-with-dependencies. jar.


1 Answers

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>
like image 158
jewles Avatar answered Sep 17 '22 21:09

jewles