How to Create a .war file using Eclipse Maven Plugin? Apache ‘maven-war-plugin’ usage

Last updated
App Shah

Crunchify » Java and J2EE Tutorials » How to Create a .war file using Eclipse Maven Plugin? Apache ‘maven-war-plugin’ usage

how-to-create-a-war-file-using-eclipse-maven-plugin-apache-maven-war-plugin

Maven is pretty amazing, isn’t it? Well – for me at least I love using Maven in my daily Java Development practice. Maven is nothing but a plugin execution framework; all work is done by plugins.

I’ve written number of Maven tutorials on Crunchify mainly on Maven Plugins. Here are couple of them:

==> Use maven-shade-plugin to create 1 single executable jar.

==> Use maven-resources, maven-dependency and maven-jar plugins to create executable jar file with all dependencies into 1 folder.

Now how about creating .war file instead of .jar file with Maven in Eclipse? This Maven Tutorial focuses on maven-war-plugin.

Our requirement here is to generate single .war file using Maven which you could run on Apache Tomcat or any other web container.

Let’s get started:

  1. Create Dynamic Web Project in Eclipse, i.e. CrunchifyTutorial
  2. Create simple .java file into your project, i.e. CrunchifyWarUsingMaven.java
  3. Convert Java Project into Maven project in Eclipse
  4. Add maven-war-plugin to pom.xml file
  5. Run command clean install to generate .war file

Step-1

Create Dynamic Web Project in to Eclipse, i.e. CrunchifyTutorial

Click

  • File ->
  • New ->
  • Other ->
  • Dynamic Web Project ->
  • Provide ProjectName (CrunchifyTutorial)
  • Click Finish

Step-2

Convert Project to Maven Project. Follow these steps: https://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/

Step-3

Create simple java file named CrunchifyWarUsingMaven.java

NOTE: In your dynamic web project you must have web.xml file which represents your servlet and servlet-mapping. You could use also Simple Spring MVC project too if you want for Step-3 here. If you don’t see web.xml then you could create one by following this tutorial.

package crunchify.com.tutorials;

/**
 * @author crunchify.com
 *
 */

public class CrunchifyWarUsingMaven {
	public static void main(String[] args) {
		System.out
				.println("Test.. Test by Crunchify.. \nThis is a simple tutorial on how to create .war file using Maven Plugin..");
	}
}

Here is a project structure. You could ignore other files and folders from below diagram 🙂

create-a-war-file-using-eclipse-maven-plugin-apache-maven-war-plugin-usage

Step-4

Open your pom.xml file and update it with below code. Mainly look at line 7 and 20. We are packaging project as war using <packaging>war</packaging>.

<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>CrunchifyTutorial</groupId>
	<artifactId>CrunchifyTutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
	</dependencies>
</project>

Step-5

Now Right Click on Project -> Run-As ->Maven Build (Number 5).

Maven Build Option in Eclipse

Step-6

Maven clean install on project.

Maven Clean Install Steps For Eclipse Web Project

Step-7

Checkout your Build result in Eclipse console.

Maven war plugin - Building War Console Output

Step-8

That’s it. Now get your .war file from /target directory and deploy into Apache Tomcat or any other Web Container.

13 thoughts on “How to Create a .war file using Eclipse Maven Plugin? Apache ‘maven-war-plugin’ usage”

  1. Hi
    After getting war file I deployed this in tomcat then while running this I am getting this error “the selection cannot be run on any server” any help please ? & I can’t see web.xml file in my war so I believe its might be cause but how it is missing while war file creation no Idea as I am new to maven? Thanks in advance.

    Regards,
    Venki Y.

    Reply
  2. How do you include the resources folder in the war? I have deployed the war to the tomcat server, but when I press submit button on my web page, nothing happens. No console output in eclipse. I’m not sure whether the war isn’t deployed correctly or whether resources are missing? Help!

    Reply
  3. Hi

    Thank you very much for Article , i have working in one project where after building war all dependencies(3rd Party Library) is going inside WARWEB-INFlib because of this WAR becoming very big and there are multiple WAR’s using same Libraries., please can you help with putting those JAR’s outside WAR while building using maven

    Regards,
    santhosh N

    Reply
    • Hi Joe – can you please share your requirements in details? Didn’t get what you mean by “zip individual files”.

      Reply

Leave a Comment