A Web Application Using Maven in Eclipse:
In this post we are going to see how to create a web application using Maven in Eclipse. There are many advantages of creating a Web application using Maven. For that we need to have Maven Plug-in integrated into Eclipse.
From Eclipse, New Project Wizard, select Maven Project as shown in the picture and click Next
In the next window, we will see options to skip the archetype selection. Leave it unchecked. Select the workspace location and click Next.
The next Window will show a list archetypes. We have to choose one archetype based what kind of project we want to build. Since we are going to build a web app, we’ll choose maven-archetype-webapp. Click Next.
In the next window, enter the GroupId, ArtifactId, Version and Package as required. Click Finish.
On successful build, /src folder and pom.xml will be created in the file system. When we look at the pom.xml that is created,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<name>mywebapp Maven Webapp</name>
Note the packaging is ‘war’, because we have selected a web application selected in the archetype. Look at the dependencies. It has dependencies on servlet-api and jsp-api. These dependencies will be used if we are going to use servlet and jsps. We have junit dependency which we will be used to run our test cases.
We have <build></build> section at the end which has build plug-ins. There is one plug-in defined that is ‘maven-compiler-plugin’. What to be noted here is that the compiler in Maven is actually a plug-in. What is happening in this plug-in section is, we are configuring how the compiler plug-in should behave. We have <configuration> tag with some values to define it. What happens if we do not use this mention this plug-in in the pom.xml? Maven uses this plug-in by default with default behaviour even if it is not mentioned in the pom.xml. When we run the mvn compile command, it is this maven compiler plug-in that is doing the job. The only reason we would need to mention this in pom.xml is to override the default behaviour of this plug-in and change the configuration.
Now all we need to do is export the project as WAR file, drop this .war into a container (tomcat probably) and it should work. Lets look at what will be there inside /src folder. We haven’t written any code yet. If we open /src/main/java/com/org/j2eereference (the package structure), there won’t be any java class by default. But inside /src/main/webapp, we will find WEB-INF folder and index.jsp. WEB-INF will contain the web.xml, which will be pointing the only available jsp that is index.jsp. Deploy this .war int tomcat container and run it.