Saturday, April 26, 2014

How to Create a Web Application Project with Maven in Eclipse

In this tutorial, we will see how to create a web application in Eclipse using Maven's m2eclipse plugin. The m2eclipse plugin comes with the Eclipse IDE version 4.3.2.

Tools used:
1. Eclipse IDE for Java EE Developers 4.3.2   (Kepler)
2. Tomcat 7
3. JDK 1.7
4. Maven Eclipse Plugin - m2eclipse version 1.4


1. Create the Maven project in Eclipse

Click on File > New > Project > Maven > select Maven Project > click Next button





You will now get the screen below. Just check the box for 'Use default Workspace location' if it's not checked by default. Click the 'Next' button.





You will now get the screen below. You will need to select an archetype. An archetype is just ta template that Maven will use for your new web application project.

Enter 'webapp' into the filter textbox and select 'maven-archetype-webapp' from the list.

Click the 'Next' button.





You will now get the screen below. You need to enter your maven project's group Id, Artifact Id, Version and Package information. The 'Artifact Id' is your project's name.




Click the 'Finish' button. This will create your Maven project in eclipse.
You should now see the new project in the Package Explorer.



2.  Add the 'Server Runtime' library in the project's  'Java Build Path'  property.

Right-click on the project name > Properties > Java Build Path




Click on Add Library > select Server Runtime > select a servlet container (eg.; Apache Tomcat) > click on Finish.

You will be back on the screen above.

Click on Ok.

Eclipse should then automatically take the servlet container's libraries into the build path.

You should now see the servlet container library (eg.; Apache Tomcat library) in your projects directory.
You should also now see the following source folders created under your project's directory.

  • src/main/java
  • src/test/java

If it was not created, you may manually create it.



3.  Add or change (optional)  the 'JRE System Library' in the project's  'Java Build Path'  property.

Note that the list of libraries on the Java Build Path now shows Apache Tomcat (the Server Runtime library we added in the previous step).

You may now change the JRE System Library (optional - only if you want to) or add a JRE System Library if the  Java Build Path does not have one yet.

Right-click on the project name > Properties > Java Build Path 




Click on Add Library > select JRE System Library > click Next > choose your JRE (eg.; Workspace default JRE or Alternate JRE) > click on Finish.


Running this project in its bare form should give you the 'Hello World' webpage from index.jsp. Try it.

That's it. Your Maven web project in eclipse is now ready. You can now start adding dependencies and code to your project.

No comments:

Post a Comment