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
Download Maven.
If you haven’t used Maven before in Eclipse then you may also need to install theMaven-Eclipse plugin.
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.
Select Maven plugin. Click on Next and then Finish.
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
Click on ‘Maven’->’Maven Project’
Maven Project
We now need to select archetype so leave ‘Create Simple Project’ unchecked and move ahead.
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
Now that we have created a template, enter your own ArticfactId 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.
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:
Now let’s run our webapp. It should open up a browser and display ‘Hello World’ page.
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
Once tomcat is selected and clicked on Finish, our web application will open up.
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