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:
- Create
Dynamic Web Project
in Eclipse, i.e.CrunchifyTutorial
- Create simple .java file into your project, i.e.
CrunchifyWarUsingMaven.java
- Convert Java Project into Maven project in Eclipse
- Add
maven-war-plugin
topom.xml
file - 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 🙂
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).
Step-6
Maven clean install
on project.
Step-7
Checkout your Build result in Eclipse console.
Step-8
That’s it. Now get your .war file from /target
directory and deploy into Apache Tomcat or any other Web Container.
thanks for you tutorial very helpfull
you can configure maven eclipse to create war with different profiles, thanks.
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.
Hi Venki. Could you please share screenshot or complete exception log? Will help you debug more. Thanks.
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!
Hi Harriet – please follow tutorial https://crunchify.com/how-to-create-only-one-executable-jar-file-with-all-required-jars-properties-and-resources/. This covers all steps on how to put resource folder in the .war file.
If you are running project in Eclipse and deploying it in Tomcat then resource folder should load by itself. You could verify it by your project’s build path.
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
Hi Santosh – you could achieve that by using
‘maven-resources’, ‘maven-dependency’ & ‘maven-jar’ Plugins. Here is a complete tutorial: https://crunchify.com/how-to-create-build-java-project-including-all-dependencies-using-maven-maven-resources-maven-dependency-maven-jar-plugin-tutorial/.
As you see – it puts all .jar files under /lib folder.
hey, i need to zip individual files in an directory, can you please suggest how can it be done
Hi Joe – can you please share your requirements in details? Didn’t get what you mean by “zip individual files”.
Thank you very much for your help ! Very useful for my project :).
I’m glad it worked for you Mylene.. Keep visiting and happy coding..
Thanks much for your help! I’ll give it a shot.