Updated February 20, 2023
Definition of Maven WAR Plugin
Maven war plugin is responsible for collecting dependencies of classes and artifacts of web applications and needs to package the same into the archive of a web application. It is the default goal that was invoked during the packaging phase of products during the type of war. War plugin is used to speed up the testing during a development phase.
Introduction to Maven WAR Plugin
The war in place is the variation of exploding, whereas our web application is generated into the web application of the source directory into the web app by default. There are four types of war plugin goals available as follows:
- Exploded – This goal is used to create an exploded web application into a specified directory.
- Help – This type of goal displays the help information from the war plugin. We are using the mvn command to show the details of the parameters.
- InPlace – This goal type is used to generate the web application into the source directory of the web application.
- War – This goal type is used to build the war file.
To use the war plugin in the java application, we have required the Jdk version as 1.7 and above. We have needed the maven version as 3.1.0. For memory and disk usage, we do not have a minimum requirement; as per the application, we can increase or decrease the memory.
How to Use Maven WAR Plugin?
The below example shows how we can use the package phase as follows. To define the package war plugin, we need to add the below code into the pom.xml file where we need to define the packaging as war.
<groupId> com.example </groupId> <artifactId> maven_war </artifactId> <version> 0.0.1-SNAPSHOT </version> <packaging> war </packaging> <name> maven_war </name> <description> Project for maven_war </description> <properties> <java.version> 1.8 </java.version> </properties>
In the below example, we are using the invocation of war.exploded goal to use the war plugin are as follows. While using the exploded goal we are using the plugin directory to define the war plugin into the pom.xml file.
<plugin> <groupId> org.apache.maven.plugins </groupId> <artifactId> maven-war </artifactId> <version> 3.3.2 </version> <configuration> <webappDirectory> /dir </webappDirectory> </configuration> </plugin>
The below example shows to use of the war plugin by using war.inplace goal are as follows. To define the inplace war plugin we are using the mvn command, also we can run our project by using a maven build.
mvn compile war:inpace
Maven WAR Web Application
The below example shows the application as follows. While developing the application into the maven war plugin first we need to create the template. The below steps show how we can create an application of the war plugin as follows.
- In this step, we are creating the war plugin project template into the spring initializer. We are giving the name of the maven project as maven_war. We are defining the packaging as war and selecting the java version as 8.
Group name – com.example Artifact – maven_war
Name – maven_war Packaging – war
Java version – 8
- After creating maven war project, in this step, we are opening the war plugin application project into the spring tool suite.
- After creating the project template of the war plugin and opening the project in this step we are checking the project structure and files.
- Basically in the java application, there are four ways to use the war plugin. The first way to use the war plugin as use the package phase by using the project package type as war invocation as follows. In the below example, we are defining the package name as war as follows.
<groupId> com.example </groupId> <artifactId> maven_war_plugin </artifactId> <version> 0.0.1-SNAPSHOT </version> <packaging> war </packaging> <name> maven_war_plugin </name> <description> Project for maven_war_plugin </description> <properties> <java.version> 1.8 </java.version> </properties>
- In the below step, we are running the project by using maven build. We using mvn command for the same.
Maven WAR File Content
The war plugin of maven is responsible for compiling and collecting the dependencies, resources, classes from web applications. We have defined multiple goals in the war plugin i.e. inplace, war, and exploded. In the below example we are adding the maven war plugin into the pom.xml file. After adding the maven war plugin then we are running the maven install command.
<plugin> <groupId> org.apache.maven.plugins </groupId> <artifactId> maven-war </artifactId> <version> 3.3.2 </version> </plugin>
By using the exploded command we are generating the war file into the directory which was inside the target directory. In the below example, we are including or excluding the content of war file as follows. By using the war plugin we are filtering the contents of a war file as follows.
<plugin> <artifactId> maven-war </artifactId> <version> 3.3.1 </version> <configuration> <webResources> <resource> <directory> War resources </directory> </resource> </webResources> </configuration> </plugin>
Maven WAR Setting
At the time of using the war plugin into the maven application, we are using the below required parameter as follows.
- Output directory
- War source directory
- Web app directory
- Work directory
Also, there are multiple optional parameters available that we are using in the application of the war plugin. The maven war plugin is allowing us to customize the file of the manifest. We are adding the classpath into the manifest file. It is useful at the time when war files are in a more complex structure and also it is more useful when we need to share the dependencies into multiple modules. In the below example, we are using maven war settings as follows.
<plugin> <artifactId> maven-war-plugin </artifactId> <version> 3.3.1 </version> <configuration> <archive> <manifest> <addClasspath> true </addClasspath> </manifest> </archive> </configuration> </plugin>
- The war plugin is nothing but web application archives of web application resources. The war file is used to deploy the web application onto the application server.
- Inside the war file, web components are packed into one unit, it includes the jar files.
Given below are the FAQs mentioned:
Q1. Why are we using the maven war plugin in the application of maven?
Answer: War plugin is used to deploy the application of java into the application server. Maven war plugin is the tool for build management.
Q2. How many types of goals we are using in the maven war plugin?
Answer: We are using four types of goals in the war plugin i.e. war.exploded, war.help, war.war, and war.inplace.
Q3. Which version we have required at the time of using the war plugin in web application?
Answer: We need to use the maven version of 3.1.0 and above and for java we need to use the JDK 1.7 version.
To use the maven war plugin in the java application we have required the Jdk version as 1.7 and above. Maven war plugin is responsible for collecting dependencies of classes and artifacts of web applications and needs to package the same into the archive of the web application.
This is a guide to Maven WAR Plugin. Here we discuss the introduction and how to use the Maven WAR Plugin along with the web application, file content, and settings. You may also have a look at the following articles to learn more –