Packaging Spring Boot Applications
Packaging as JAR
- Pay attention to two key points: you must either comment out
<skip>true</skip>or change its value tofalse. Otherwise, you will encounter a "no main manifest attribute" error upon startup. Additionally, update the<mainClass>tag with the fully qualified name of your application's entry point.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<mainClass>com.mycompany.app.ApplicationLauncher</mainClass>
<!-- Ensure skip is commented out or set to false -->
</configuration>
<executions>
<execution>
<id>executable-jar-packaging</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
- Running
cleanfollowed bypackagewill generate the artifact shown in the image below.
- Open
your-app.jar/META-INF/MANIFEST.MFto inspect the complete manifest details.
- Verify the execution on Windows by running:
java -jar your-app.jar
Packaging as WAR
1. Declare WAR packaging in the pom.xml
<packaging>war</packaging> <!-- Place this outside the <properties> block -->
2. Exclude the embeddded Tomcat and use an external Tomcat dependency
Add the Servlet API dependency and exclude the Tomcat starter from the web dependency.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3. Extend SpringBootServletInitializer in your main class
This provides a launcher for the Spring Boot project when deployed to a Servlet container (its purpose is to initialize the internal Servlet components).
4. Override the configure() method in the main class
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
public static void main(String[] arguments) {
SpringApplication.run(WebApplication.class, arguments);
}
@Override // Specifies the entry point for the Servlet container initialization
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebApplication.class);
}
}
5. Execute the Maven package phase
You might encounter a packaging error.
Method 1: Add a configuration property to skip the check for web.xml.
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
Method 2: Upgrade the Maven War Plugin version to 3.0.0 or higher.