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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<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.org.j2eereference</groupId> <artifactId>mywebapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>mywebapp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.4</source> <target>1.4</target> </configuration> </plugin> </plugins> </build> </project> |
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.