Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Packaging Spring Boot Applications

Tech 1

Packaging as JAR

  1. Pay attention to two key points: you must either comment out <skip>true</skip> or change its value to false. 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>
  1. Running clean followed by package will generate the artifact shown in the image below.

  1. Open your-app.jar/META-INF/MANIFEST.MF to inspect the complete manifest details.

  1. 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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.