Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a "fat jar" of a Maven project? [duplicate]

Using IntelliJ I just created a new Maven project and added the following to the pom file http://undertow.io/downloads.html and the following to a Main.java file http://undertow.io/index.html

Now if I run the code all works well, but how do I make this as a "fat jar" that will contain all the dependencies in the pom file and that I'll be able to run by just java -jar my.jar ? Like you are able to do with a Spring Boot app.

like image 487
daniels Avatar asked Aug 22 '16 21:08

daniels


People also ask

Which Maven plugins can be used to create fat jars?

Other Maven plugins that can be used to create a fat jar are the Maven Assembley Plugin and the One-Jar Plugin.

What is a Maven JAR file?

Maven is one of the most used tools for Java application development. Maven packaging will create a Jar file without dependencies. Maven provides other plugins using which we can generate the jar with all its dependencies which is usually called FatJar, which is used as an executable Jar file.

What is a fat jar?

A Fat JAR is a single JAR file that contains all the compiled Java classes from your project, and all compiled Java classes from all JAR files your project depends on (see Maven Dependencies ). In this Maven Fat JAR Tutorial I will show you how to create a Fat JAR with Maven.

How to create a single Maven jar with all dependencies?

After configuring the shade plugin in your build the command mvn package will create one single jar with all dependencies merged into it. This is now the correct way to do it. The maven-assembly-plugin documentation states: "If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support.


2 Answers

Maven Shade Plugin does this well.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.4.3</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>package.Main</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
like image 160
Stewart Avatar answered Oct 22 '22 04:10

Stewart


1) Add the Spring-boot-maven-plugin to the pom.xml file

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2) Change the packing to jar

<packaging>jar</packaging>

3) mvn package will create the executable jar.

like image 36
K139 Avatar answered Oct 22 '22 02:10

K139