Generate a WAR File in Maven
Last Updated :
30 May, 2024
Maven is an open-source build automation tool primarily used for Java applications in the software industry. It provides numerous commands to manage Maven projects, such as build, clean, install, and others. Maven automates source code compilation, provides dependency management features, assembles binary codes into packages, and executes test scripts.
With Maven, we can create Java deliverables like WAR, JAR, and EAR files. The pom.xml
file assists in performing these tasks, containing project information such as the project name, group ID, and other relevant details. In this article, we will explain how to generate a WAR file in Maven.
Generate a WAR File in Maven:
There are two ways to Generate a WAR File in Maven.
- Using IDEs like Spring Tool Suite
- Using CMD and Commands.
1. Generate a WAR File in Maven Using Spring Tool Suite
To Generate a WAR File in Maven, first we need create a maven project then we perform the required operation that. Below we provide the step by step process to generate a war file in the maven using STS IDE.
Step 1: Create Spring Starter Project
First open your favorite IDE, here, we use Spring Tool Suite. Now, click on File then select New option, after that select Spring Stater Project. Then a new window is opened.

Step 2: Provide Project Metadata
Now provide the project name, artifact, group name and other information you need to provide. Below we provide the output image for your refence.

Step 3: Add Required Dependencies
Now click on next then a new window is open now select required project dependencies from here. You can observe this in the below image.

Step 4: Click on Finish Button
Now click on finish button, now the project is created by using your requirements.
Step 5: Run the Project
Once project is created successfully, now run this project as maven build.

Step 6: Edit Configuration
After this one more new window is opened then provide In the Goals field, enter clean package and click Run.

Note: STS will execute the Maven build process, and upon completion, the WAR file will be located in the target directory of your project.
Step 7: Open target Folder
Now open your target folder in the project folder, you observe the war files in that folder.

2. Generate a WAR File in Maven Using Commend Prompt
This is another way for generate a WAR File. Here, we use maven commands only.
Step 1: Set up Maven Project
First, you need a Maven project. If you don't already have one, you can create a new one using the following command. Here we already created a maven project.
mvn archetype:generate
-DgroupId=com.example
-DartifactId=my-webapp
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false
Step 2: Navigate to Project Directory
Use the below command, to navigate to project directory.
cd my-webapp
Step 3: Build the WAR File
Run the following command to build the WAR file.
mvn clean package
After the build completes, you should find the generated WAR file in the target directory of your project.
Similar Reads
What Is an Apache Maven Artifact? An Apache Maven artifact is a file, typically a JAR (Java Archive), created when a project is built. Each artifact is uniquely identified by three main coordinates: groupId, artifactId, and version.Maven artifacts are package units of your software. When building a project, Maven uses these artifact
2 min read
Servlet - War File A war (web archive) A web project's files are contained in this file. It may contain files such as servlet, xml, jsp, image, html, css, and js. Here, we'll go through what a war file is, how to make one, how to deploy one, and how to remove one. What is WAR File? The WAR file (Web Application Resour
2 min read
How to Configure and Integrate Maven in Jenkins? Maven integration with Jenkins makes Java project development and deployment easier. Step-by-step instructions for integrating Maven with Jenkins are provided in this guide. You will discover how to set up Jenkins for Maven, manage project dependencies, automate builds, and enhance software delivery
5 min read
Clearing the Maven Cache The Maven cache, or local repository, is a directory on your computer where Maven stores all downloaded files. These include project dependencies, plugins, and other artifacts required for building projects. Storing these files locally accelerates build times since Maven can reuse previously downloa
3 min read
Optional Dependency in Maven Optional dependencies in Maven are used when it is difficult to break up a project into sub-modules (for whatever reason). The idea is that some dependencies are only required for specific aspects of the project and will be removed if such features are not used. Ideally, such a feature would be sepa
2 min read
Features of Maven Maven is a powerful project management and build automation tool in the software industry. developed by the Apache software foundation. It is primarily used for java based projects only now It can support other languages also for building the project. Maven tool provides a complete build life cycle
2 min read