How to make a “fat jar” of a Maven project? [dupli

2019-01-12 07:05发布

问题:

This question already has an answer here:

  • How can I create an executable JAR with dependencies using Maven? 33 answers

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.

回答1:

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>


回答2:

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.