How to build WAR file for Jakarta EE Servlet Container?

To build a WAR file for your Jakarta EE servlet application, the steps will vary depending on the build tool you are using. In this post I will show you how to use Apache Maven, build it in the IDE, or create it manually. Here’s how you can build a WAR file:


1. Using Maven

Maven is a widely used build tool in Java projects. If your project uses Maven, follow these steps:

a) Add WAR Packaging to pom.xml

Your pom.xml file should include these configurations:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.kodejava</groupId>
    <artifactId>helloworld-servlet</artifactId>
    <version>1.0</version>
    <packaging>war</packaging> <!-- Specify that the packaging is WAR -->
    <name>HelloWorldServlet</name>

    <dependencies>
        <!-- Jakarta Servlet dependency -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.1.0</version>
            <scope>provided</scope> <!-- mark as provided since the container will provide the API -->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- WAR Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml> <!-- Optional for Servlet 3.0+ -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

b) Directory Structure for WAR Projects

Ensure your project follows the standard Maven directory structure:

src/
└── main/
    ├── java/                     --> Your Java source files
    │   └── org/kodejava/servlet/HelloWorld.java
    ├── resources/                --> (Optional) Resource files
    └── webapp/                   --> Web application files
        ├── WEB-INF/              --> Configuration files
        │   └── web.xml           --> web.xml file (if required)
        └── index.html            --> Static files (Optional)

Important Notes:

  • webapp/WEB-INF/ is the location for configuration files. For modern servlets with annotations like @WebServlet, a web.xml file may not be required.
  • Additional static resources (like HTML, CSS, or JS files) can be placed under webapp/.

c) Build the WAR File

Run the following Maven command:

mvn clean package

Once the build is successful, the WAR file will be generated in the target/ directory, named as <artifactId>-<version>.war.

For instance: target/helloworld-servlet-1.0.war


2. Using an IDE (IntelliJ IDEA, Eclipse)

Some IDEs let you build a WAR file without using Maven explicitly. For IntelliJ IDEA:

a) Configure Your Project as a WAR

  1. Open your project in IntelliJ.
  2. Go to File > Project Structure > Artifacts.
  3. Click on the + button and choose Web Application: Archive > From Modules with Dependencies.
  4. Select your module and specify the location of your web and META-INF folders.
  5. Apply and save changes.

b) Build the WAR File

  1. Go to Build > Build Artifacts > Build.
  2. The WAR file will be created in the specified output folder.

3. Manually Create a WAR

If you’re not using a build tool and want to manually create a WAR file:

a) Prepare the Required Directory Structure

Manually create a structure similar to this:

project/
└── WEB-INF/
    ├── web.xml           --> Optional (for configurations)
    └── classes/          --> Compiled `.class` files (Java code goes here)
        └── org/kodejava/servlet/HelloWorld.class

Copy all your .class files, resources, and configuration files into their respective folders.

b) Create the WAR File

Navigate to the folder containing the root project directory and run:

jar -cvf HelloWorldServlet.war *

This command will package your project into a .war file.


By following any of these methods, you’ll have a .war file ready for deployment in your Tomcat 11 server.

How do I compile and execute a JDK preview features with Maven?

To compile and execute a JDK preview features with Maven, you need to add in the following configurations in your pom.xml file:

  • Compiler Plugin: The configuration should specify the JDK version and enable the preview features. It should look similar to this:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>21</release>
                <compilerArgs>--enable-preview</compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Surefire Plugin: If you’re using the Maven Surefire Plugin to run your tests, you should also enable the preview features there. The configuration may look similar to this:
<build>
    <plugins>
    ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <argLine>--enable-preview</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

Before building your project, ensure that you have JDK 21-preview installed on your computer, and it’s properly set in JAVA_HOME environment variable or in the IDE settings.

Then use Maven to package or install your project:

mvn clean package
# or
mvn clean install

Please ensure that you have the correct version of maven-compiler-plugin and maven-surefire-plugin that support the JDK 21-preview features. For the project that uses Spring MVC, you should make sure all dependencies are compatible with JDK 21-preview as well.