Executable JAR
You've built your Spring Boot app, tested it thoroughly, and it works perfectly on your machine. Now what? You need to package it so anyone can run it without installing Java or Maven. That's where executable JARs come in.
An executable JAR is a single file that contains your entire application โ code, dependencies, and an embedded server. It's like putting your house, furniture, and utilities into a shipping container. Just open it and live.
Spring Boot Maven Plugin
The Spring Boot Maven plugin is what makes executable JARs possible. It repackages your JAR with an embedded launcher so it can be started with a simple java -jar command. No WAR files, no external server setup โ just run and go.
Add the plugin to your pom.xml, and Spring Boot does the heavy lifting. It figures out which classes go where and creates a JAR that just works.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Building the Executable JAR
Building is simple. Run mvn clean package from your project root. Maven compiles your code, runs tests, and creates two JARs โ the original and the executable one. The executable JAR is usually in the target directory with a .jar extension.
The executable JAR is larger because it includes all your dependencies. That's expected โ it needs everything to run independently. Don't worry about the size; modern servers can handle it easily.
mvn clean package
ls target/
myapp-0.0.1-SNAPSHOT.jar
myapp-0.0.1-SNAPSHOT.jar.original
Running the JAR
Running your app is now a one-liner: java -jar myapp.jar. That's it. The embedded Tomcat server starts, your Spring context loads, and your app is live. No deployment descriptors, no server configuration โ just one command.
You can pass configuration as command-line arguments or environment variables. Spring Boot handles everything, making deployment as simple as running a local development server.
java -jar myapp-0.0.1-SNAPSHOT.jar
java -jar myapp-0.0.1-SNAPSHOT.jar --server.port=8081
java -jar myapp-0.0.1-SNAPSHOT.jar --spring.profiles.active=production