Maven Web Application Eclipse Example

0

In this article we will see how to use Maven to setup web application project in Eclipse.

  • I am using Eclipse as the IDE, version Luna 4.4.1.
  • Apache Maven is a software project management and comprehension tool. It manages the project build using the project object model (POM) XML.

Download Maven and Install Maven-Eclipse Plugin

    1. Download Maven.
    2. If you haven’t used Maven before in Eclipse then you may also need to install theMaven-Eclipse plugin.
    3. You need to click on the Help->Install New Software. Enter http://download.eclipse.org/technology/m2e/releases in ‘Work with’ an click on Add.
    4. Select Maven plugin. Click on Next and then Finish.
Maven Eclipse Plugin

Maven Eclipse Plugin

Now that we have installed Maven and Eclipse-Maven plugin, we will be able to create Maven Projects. Let’s create one.

MavenProject

Click on File->New->Other

Eclipse Other Projects

Eclipse Other Projects

Click on ‘Maven’->’Maven Project’

Maven Project

Maven Project

We now need to select archetype so leave ‘Create Simple Project’ unchecked and move ahead.

Maven Project Main Screen

Maven Project Main Screen

Now what is an archetype?
Archetype is a Maven project templating toolkit which means we can select one of its template it will create a sample project for us. In our case, we want a web-app so we will enter ‘org.apache.maven.archetypes’ in filter and then select row with Artifact-id as ‘maven-archetype-webapp’. This will create sample Maven Webapp project. Once selected, click on Next.

Maven Archetype Web-app

Maven Archetype Web-app

Now that we have created a template, enter your own ArticfactId and GroupId.

Enter ArtifactId and GroupId

Enter ArtifactId and GroupId

In order to just run the sample app, we still need servlet api. Let’s add javax.servlet-api to our pom.xml, version 3.1.0.

<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>com.javarticles.webApp</groupId>
	<artifactId>webAppExample</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>webAppExample Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>webAppExample</finalName>
	</build>
</project>

If you don’t see Simply create a java directory under main (i.e. src/main/java) and right-click on your project and select Maven > Update Project Configuration.

The project structure should look like below:

maven_webapp_project_structure

Run the web application

Now let’s run our webapp. It should open up a browser and display ‘Hello World’ page.

Click on 'Run on Server'

Click on ‘Run on Server’

We will be using tomcat as our server.

If you have configure more than one tomcat or not yet configure, this is time to do and select one.

 

 

Select Tomcat Server

Select Tomcat Server

Once tomcat is selected and clicked on Finish, our web application will open up.

Webapp 'Hello World' page

Webapp ‘Hello World’ page

Download the Eclipse Project

This was an example about creating web application project in Eclipse using Maven. You can download the source code here: webAppExample.zip

Share.

Comments are closed.