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
, aweb.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
- Open your project in IntelliJ.
- Go to File > Project Structure > Artifacts.
- Click on the + button and choose Web Application: Archive > From Modules with Dependencies.
- Select your module and specify the location of your
web
andMETA-INF
folders. - Apply and save changes.
b) Build the WAR File
- Go to Build > Build Artifacts > Build.
- 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 secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025