Creating JAR with Dependencies in Maven

9

I want to use Maven to generate the JAR with its dependencies in .jar format, the solutions I found are not valid.

Eclipse has three ways to generate an executable JAR.

  • Extract required libraries into gerenated JAR
  • Package required libraries into generated JAR < I use this in Eclipse.
  • Copy required libraries into a subfolder next to the generated JAR
  • And does Maven have a form equivalent to Eclipse?

    Using maven-jar-plugin

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>qualquer.que.seja.seu.pacote.ClasseMain</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    

    Problem: This mode generates a JAR with no dependencies in .jar format.

    Using maven-assemply-plugin

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>qualquer.que.seja.seu.pacote.ClasseMain</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
    

    Problem: Generates a JAR with the "extracted" dependencies out of the .jars format.

        
    asked by anonymous 06.03.2014 / 13:08

    2 answers

    5

    There is also Maven Shade Plugin that allows you to package the artifact into a super-jar , including its dependencies, and even rename the packages to some dependencies if you wish.

    See an example:

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>org.sonatype.haven.HavenCli</mainClass>
                    </transformer>
                  </transformers>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    </project>
    

    In addition to putting everything together into a single jar, which makes it very easy to distribute for download, for example, Shade also allows you to rename dependency packages, helping a lot in cases where classpath, for example.

    It's important to note that shade and assembly plugins are not direct competitors, although the two allow packaging the dependencies in a jar. In the case of packaging, the assembly is more powerful, flexible and complicated. The shade has fewer features, but to create an uber-jar it is more direct and simple.

        
    06.03.2014 / 13:49
    4

    You have not set up how maven-assembly-plugin will run, that is, which goal and which phase of maven as well. To package a jar with dependencies during the package phase, the configuration looks like this:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>qualquer.que.seja.seu.pacote.ClasseMain</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>attached</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
        
    06.03.2014 / 13:22