Maven – Create a Simple Webapp Project

1. Create a new Maven project in Eclipse with war as the packaging type


 

2. Edit the pom.xml as follow

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>spring-mvc-trial</groupId>
	<artifactId>spring-mvc-trial</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<name>Spring MVC Trial</name>
	<description>Spring MVC Trial</description>
	
	<!-- Build Configuration -->
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 

3. Create /src/main/webapp/index.jsp

<head><title>Example :: Spring Application</title></head>
<body>
	<h1>Example - Spring Application</h1>
	<p>This is my test.</p>
</body>

 

4. Create /src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
	version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

5. Run Maven Install

 

6. A .war file is built at /target and copy it to the Tomcat webapp folder

 

7. Verify the deployment in browser

 

Done =)

19 thoughts on “Maven – Create a Simple Webapp Project”

  1. Can you explain how you installed the maven plugin on eclipse ?
    I’m trying to install it but I do not understand how to do it …

    Like

      1. Thank you for this help. I actually already had Maven (m2eclipse) installed. I am lost on how to use it from within Eclipse. Or, if from the command line, which commands to run.

        Like

      2. a maven project contains different life cycle phases. for example, if you want to run test cases, you can run the
        mvn test

        if you want to build the project
        mvn install

        the install phase will include the package and test phases etc.

        for more information about the maven command, you can take a look @
        Maven Getting Started Guide

        If you are new to Maven, the following book is really a nice start for you
        Sonatype – Maven: The Complete Reference

        Hope it can help you to start maven. it is really a great tool for java development. =)

        Like

  2. I am sorry for this newbie question. at step 5 and 6 it says /target folder… where is /target folder. Please help

    Like

      1. Thank you for your reply, actually I found the war file also, I done all the steps as you told but when I deploy it to tomcat that is I copy the war file to webapp of tomcat and run the tomcat, when hitting the same URL it says HTTP Status 404. please help me

        Like

      2. The path where I put the war file in the tomcat
        C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps

        Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.