Creating fat JARs using the Maven Assembly plugin
The Maven Assembly plugin provides the ability to combine project output into a single distributable archive that contains dependencies, modules, site documentation, and other files.
Configure the Assembly plugin
To build an assembly, you need to configure the Assembly plugin first:
Navigate to the pom.xml file and ensure the main application class is specified:
<properties> <main.class>com.example.ApplicationKt</main.class> </properties>Add the
maven-assembly-plugin
to theplugins
block as follows:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>${main.class}</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>assemble-all</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
Build an assembly
To build an assembly for the application, open the terminal and execute the following command:
This will create a new target directory for the assembly, including the .jar files.
Run the application
To run the built application, follow the steps below:
In a new terminal window, use the
java -jar
command to run the application. For the sample project it looks like the following:java -jar target/tutorial-server-get-started-maven-0.0.1-jar-with-dependencies.jarYou will see a confirmation message once your app is running:
[main] INFO Application - Responding at http://0.0.0.0:8080Click on the URL link to open the application in your default browser: