12

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.

daniels
  • 18,416
  • 31
  • 103
  • 173
  • 1
    Doing this will eradicate the constituent libraries’ manifests, which may contain important information. – VGR Aug 22 '16 at 21:47
  • 3
    How is this different to making an executable jar with dependencies using maven. http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – Acewin Aug 22 '16 at 21:49

2 Answers2

23

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>
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • thank you - this works well for me, but it seems to create two jars by default. How can I opt just for the fat one? – Open Food Broker Jun 21 '17 at 13:44
  • The other jar is created by `maven-jar-plugin`. Is the 2nd jar really in your way? – Stewart Jun 21 '17 at 14:05
  • well I find it slightly confusing and would be happy to have kind of a simple switch "make fat jar yes/no" – Open Food Broker Jun 21 '17 at 14:08
  • I think control of the name is the best you're going to get. https://stackoverflow.com/questions/4088701/maven-shaded-jar-is-prefixed-with-original-in-the-file-name – Stewart Jun 21 '17 at 21:29
  • @J.Doe Have you figured out how to create 1 file??? – Ilya Y Dec 18 '19 at 11:43
  • @Axel23 this was 2 years ago!! check for newest versions of all related libs, they keep get improved. At time being 2 years ago I think I got along with 2 files somehow. – Open Food Broker Dec 18 '19 at 14:10
6

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.

K139
  • 3,654
  • 13
  • 17
  • 2
    I like this solution but I think you should consider editing it to remove the version from the plugin and have the pom specify a parent pointing at spring-boot-start-parent. – wobblycogs May 27 '17 at 14:08