CodeGym /Courses /Module 5. Spring /Lecture 147: Preparing a Spring Boot app for deployment (...

Lecture 147: Preparing a Spring Boot app for deployment (creating jar/war files)

Module 5. Spring
Level 22 , Lesson 6
Available

Today we'll talk about how to turn your Spring Boot app from cute code into a real, working artifact (jar or war file) that's ready to run anywhere — from your local laptop to cloud servers.

Maybe some of you already ran into the classic "works on my machine" situation where it completely fails on the server. That instantly turns you into the team's local meme hero. Our goal today is to fix that.

Main goals of this lecture

  • Find out how to properly prepare a Spring Boot app for deployment.
  • Understand what jar and war files are and how they differ.
  • Configure Maven to create these artifacts.
  • Prepare application versioning for smooth CI/CD workflow.

A bit of theory: JAR, WAR and why you need them

JAR (Java Archive) is an archived file that packages your application and all its dependencies. Put simply, it's like a "suitcase" with your stuff that you can run as-is:


java -jar your-app.jar
  • Advantage of JAR: they're "self-contained". Spring Boot puts dependency JARs and an embedded web server (for example, Tomcat) inside the JAR.
  • When to use:
    • If the application will run as an independent process.
    • If the server is already set up to run Spring Boot apps.

WAR (Web Application Archive) is an archive for web applications that's typically deployed to an external web server like Apache Tomcat or Jetty. It doesn't include an embedded web server.

  • Advantage of WAR: suitable for deployment to a long-running server where you drop your artifact into the server.
  • When to use:
    • If your project is developed as part of a larger system.
    • If the server infrastructure is strictly regulated (for example, "we use Tomcat and that's it").

Practice 1. Creating a JAR file

Let's try to package our Spring Boot app as a JAR file. You'll need to tweak pom.xml and run a few commands.

Step 1. Prepare the project

Make sure you have a working Spring Boot project. If you don't yet — create a basic app via Spring Initializer.

The project structure should look roughly like this:


src/
|-- main/
|   |-- java/
|   |   |-- com.example.demo
|   |       |-- DemoApplication.java
|   |-- resources/
|       |-- application.properties
pom.xml

Step 2. Configure Maven to build a JAR

In pom.xml make sure JAR packaging is configured. The setup looks like this:


<build>
    <plugins>
        <!-- Plugin for building the Spring Boot application -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- You can also add the resources plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Note that spring-boot-maven-plugin does the magic for us: it not only builds the JAR but also bundles dependencies and the embedded web server.

Step 3. Build the project

Build the JAR with:


mvn clean install

After running this, you'll find a file like this in the target folder:


demo-0.0.1-SNAPSHOT.jar

Run it:


java -jar target/demo-0.0.1-SNAPSHOT.jar

If everything is set up correctly, you'll see a message that the server is up at http://localhost:8080.


Practice 2. Creating a WAR file

Now we'll do the same for a WAR file. The process is slightly different here.

Step 1. Configure Maven to build a WAR

Add WAR configuration to pom.xml:


<packaging>war</packaging>

<build>
    <plugins>
        <!-- Plugin for building the Spring Boot application -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

Note the packaging tag — it tells Maven to build the project as a WAR.

Step 2. Changes in the application code

Since a WAR doesn't include an embedded server, we need to prepare the app to run in an external container. In DemoApplication.java extend SpringBootServletInitializer:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Step 3. Build the project

Now build the project:


mvn clean install

The output will include a file named demo-0.0.1-SNAPSHOT.war.


Artifact versioning

Since we're doing deployment, let's make the app a bit "smarter". Add a project version to pom.xml so artifacts get unique names:


<version>1.0.0</version>

Now your JAR or WAR will be named, for example, demo-1.0.0.jar. This is especially useful in CI/CD because it makes it easy to track which version is deployed on the server.


Common errors and how to fix them

  1. Missing <packaging> tag in pom.xml.
    If you didn't set <packaging>jar</packaging> (or war), Maven will build a JAR by default. Always double-check that tag.
  2. Error "No Main Manifest Attribute".
    That means the built artifact is missing an entry point. Make sure the DemoApplication file has a main method and that the Spring-Boot-Maven-Plugin is added to pom.xml.
  3. WAR doesn't deploy on the server.
    Check that you've added a class that extends SpringBootServletInitializer.

That's it! Now your app is ready for deployment — whether it's a local server, the cloud, or a container platform. In the next lectures we'll dive deeper into containerization with Docker.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION