First Maven Project Tutorial

Jamax Logo

Summary: The Jamax Maven tutorials are practical guides for beginners in programming. This set of tutorials will show you how to creat simple web page based on Maven technology inculding Junit, Selenium and Integration testing. In this tutorial we are going to creat simple Maven Web Project that includes two simple forms.

Hi, welcome to the first Maven tutorial for beginners. With this tutorial we will show you the way to create simple Website with two forms in Maven project. For this tutorial we will need: • Java Developer Kit 1.8 • Apache Tomcat 8.0.37 • STS (Spring Tool Suite) 3.8.1 Fist of all, what is Maven? Maven primarily represents tool for creating Java based projects. First important thing to know is that Maven project is configured by one .xml file named pom.xml. POM stands for Project Object Model. Maven is based around a concept of build lifecycle that gives us opportunities of creating simple project structures. There are three built-in build lifecycles: default, clean, site. Each one of these lifecycles is consist of certain number of phases. For more detailed informations go to site

Let’s start with our first simple Maven project. Before we start, it is important to mention that in this tutorial we won’t use database, scripts for design nor scripts for developing unit, integration or Selenium tests. We will talk more about this in one of our next tutorials.

Step 1. (Creating Maven project) • Open STS software. • Click on File -> New -> Other -> Maven -> Maven Project. • In newly opened window choose: Create a simple project and Use default workspace location. • Fill these fields: GroupID, ArtifactID, Version and Packaging. GroupID will identify your project uniquely across all projects, in our case the value of this field is: "ba.jamax.www.maven.tutorial". ArtifactID is the name of the jar without version, in our case name of the project is: "maven.tutorial". Version field leave at its default value: "0.0.1-SNAPSHOT" and for packaging choose "war". • Click on Finish. After finishing this first step, the structure should look like this:

	Maven Tutorial1 package explorer

Step 2. (pom.xml) In this step we will edit pom.xml file so that we can add Maven dependencies that are needed in this phase of creating our project.

FILE (pom.xml)

First six lines are generated automatically so we shouldn’t change them unless we need to change something. In "properties" tag we should input informations about .jar versions because they will be needed in this project. It should be noted that informations about these .jar versions could be inputted even without using tag. Maven properties are value placeholder, their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. This will help us define Spring version along with the jdk that we are using. Now we should define Maven Dependencies, input spring and servlet dependencies that we need for this project. By running this script (pressing on F5 button) Maven will automatically add .jar files. This can be seen inside Maven Dependencies list in package explorer.

Step 3. (web. xml) In this step, we will create web.xml file in src/main/webapp/WEB_INF folder. Inside this file, we have to configure dispatcher servlet that is responsible for managing HTTP requests.

FILE (web.xml)

Step 4. (dispatcher-servlet.xml) After we are finished with configuring dispatcher servlet in web.xml, we should create dispatcher-servlet.xml in the same folder. We should specify packet that has basic controller inside along with the folder that has views and extension of views. Representation of this is shown in code below.

FILE (dispatcher-servlet.xml)

Step 5. (index.jsp) In this step we will create index.jsp file inside src/main/webapp folder. This file represents the form that will be shown first when we run the project. Goal is to create two simple forms. Inside the first form we will create reference to the other one. We will be able to access the other one by clicking on corresponding link.

FILE (index.jsp)

Step 6. (WelcomeController.java) In this step we will need to create a class WelcomeController inside src/main/java in ba.jamax.controller packet. In this class we will use three annotation. First annotation is @Controller and this annotation alludes that the created class is actually controller. @RequestMapping annotation is used to map web requests into specific handler classes and/or handler methods. In this care, we use it for method. Third annotation is @RequestParam and this one indicates that a method parameter should be bound to a web request parameter. Beside this, inside class we will create an object that is type ModelAndView. That is holder for both Model and View in the web MVC framework. When using this object controller can return both model and view in a single return value.

FILE (WelcomeController.java)

Step 7. (welcome.jsp) In this step we will create view welcome.jsp inside src/main/webapp/WEB-INF/views.

FILE (welcome.jsp)

Step 8. (running project) To run the created project, we need to have apache tomcat server. Steps for this are: • Right click on project -> Run As -> Run on Server • Choose Manually define new server -> Apache -> Tomcat v8.0 Server • Choose directory that has Tomcat v8.0 Server, Next • Transfer project maven.tutorial in field Configured • Finish

Super Jamax