Packaging & Deployment: Creating JAR/WAR Files
Now that you've built your Spring Boot application, it's time to package it for deployment. Spring Boot simplifies this process significantly. We'll cover creating executable JARs (for standalone applications) and WAR files (for deployment to traditional application servers).
Understanding JAR vs. WAR
- JAR (Java Archive): Used for standalone applications. Contains your application code, dependencies, and an embedded server (like Tomcat, Jetty, or Undertow). You run the JAR directly using
java -jar. This is the most common approach for Spring Boot applications. - WAR (Web Application Archive): Used for deployment to traditional Java application servers (like Tomcat, WebLogic, or WebSphere). Contains your application's servlets, JSPs, and other web-related components. The application server provides the runtime environment.
Creating Executable JAR Files
Spring Boot's default behavior is to create an executable JAR. This is achieved using the Spring Boot Maven or Gradle plugins.
Maven:
Ensure you have the spring-boot-maven-plugin in your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
To build the JAR, run:
mvn clean package
This will create an executable JAR file in the target/ directory. The filename will typically be your-application-name-*.jar.
Gradle:
Ensure you have the org.springframework.boot plugin applied in your build.gradle:
plugins {
id 'org.springframework.boot' version 'YOUR_SPRING_BOOT_VERSION' // Replace with your Spring Boot version
id 'java'
}
To build the JAR, run:
./gradlew clean build
This will create an executable JAR file in the build/libs/ directory. The filename will typically be your-application-name-*.jar.
Running the JAR:
Once built, you can run the JAR directly:
java -jar target/your-application-name-*.jar # Maven
java -jar build/libs/your-application-name-*.jar # Gradle
This will start your Spring Boot application.
Creating WAR Files
If you need to deploy your application to a traditional application server, you can create a WAR file.
Maven:
You need to change the packaging type in your pom.xml to war:
<packaging>war</packaging>
And you must exclude the embedded server from the JAR. This is done by configuring the spring-boot-maven-plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>false</executable>
</configuration>
</plugin>
</plugins>
</build>
Then, build the WAR file:
mvn clean package
This will create a WAR file in the target/ directory. The filename will typically be your-application-name.war.
Gradle:
You need to set the mainClass and bootJar tasks to false. Also, set the packaging type to WAR.
plugins {
id 'org.springframework.boot' version 'YOUR_SPRING_BOOT_VERSION' // Replace with your Spring Boot version
id 'war'
id 'java'
}
bootJar {
enabled = false
}
jar {
enabled = false
}
Then, build the WAR file:
./gradlew clean build
This will create a WAR file in the build/libs/ directory. The filename will typically be your-application-name.war.
Deploying the WAR:
Deploy the WAR file to your application server (e.g., Tomcat) using the server's deployment mechanism (usually a web application manager or by copying the WAR file to a designated deployment directory).
Customizing Packaging
Both Maven and Gradle offer extensive configuration options for customizing the packaging process. You can:
- Include/Exclude Dependencies: Control which dependencies are included in the JAR/WAR.
- Resource Filtering: Replace placeholders in resource files during packaging.
- Manifest Configuration: Customize the JAR's manifest file.
- Layering: Create layered JARs for faster startup times (especially useful for cloud deployments). (Advanced topic)
Further Resources
- Spring Boot Maven Plugin: https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool.html#build-tool.maven-plugin
- Spring Boot Gradle Plugin: https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool.html#build-tool.gradle-plugin
- Packaging and Deployment with Spring Boot: https://spring.io/guides/gs/spring-boot-packaging/