Open In App

Generate a WAR File in Maven

Last Updated : 30 May, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

 Create Project


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.

Project Metadata


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.

Add dependencies


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.

Run the Project


Step 6: Edit Configuration

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

Edit Configuration

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.

Project Structure


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.


Article Tags :

Similar Reads