How to create Dynamic Web Application using Maven and Eclipse?


In this below example will show steps to create Dynamic Web Project using maven and import to eclipse.

Maven provide maven-archetype-webapp archetype artifact to create Dynamic Web based application from command prompt.

Pre-Requisite

Go to directory/ workspace where you want to create maven Java project. Below is command to create it.

mvn archetype:generate -DgroupId=com.{group}.app -DartifactId={App Name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Example :

mvn archetype:generate -DgroupId=com.facingissuesonit.app -DartifactId=TestMavenWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Because we are creating our first project with maven that’s why it will download lots of jars and dependency from maven repository.

C:\Users\facingissuesonit\workspace-sample>mvn archetype:generate -DgroupId=com.facingissuesissueonit -DartifactId=TestMavenWebApp
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\facingissuesonit\workspace-sample
[INFO] Parameter: package, Value: com.facingissuesissueonit
[INFO] Parameter: groupId, Value: com.facingissuesissueonit
[INFO] Parameter: artifactId, Value: TestMavenWebApp
[INFO] Parameter: packageName, Value: com.facingissuesonit
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 25.374 s
[INFO] Finished at: 2017-06-09T16:52:33-04:00
[INFO] Final Memory: 14M/148M
[INFO] ------------------------------------------------------------------------

Above command will create project with name TestMavenWebApp in your current directory and will show directory structure as below.

TestMavenWebApp
-src
--main
---webapp
----WEB-INF
-----web.xml
----index.jsp
---resources
-pom.xml

Project pom.xml will have entry as below.

<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.facingissuesissueonit</groupId>
  <artifactId>TestMavenWebApp1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>TestMavenWebApp1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>TestMavenWebApp</finalName>
  </build>
</project>

Build Project : Go to project directory and run below command to build your project. It will go through all these steps to complete build life cycle as you can see from console logs.It will create war file for your application.

1. validate
2. generate-sources
3. process-sources
4. generate-resources
5. process-resources
6. compile
7. test

mvn package

C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestMavenWebApp Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestMavenWebApp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestMavenWebApp ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestMavenWebApp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestMavenWebApp ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TestMavenWebApp ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ TestMavenWebApp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [TestMavenWebApp] in [C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp\target\TestMavenWebApp]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp\src\main\webapp]
[INFO] Webapp assembled in [65 msecs]
[INFO] Building war: C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp\target\TestMavenWebApp.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.352 s
[INFO] Finished at: 2017-06-10T14:37:25-04:00
[INFO] Final Memory: 12M/114M
[INFO] ------------------------------------------------------------------------

Now our build is successful and architecture created by maven is ready. Now time to import this project in eclipse.

Import to Eclipse : To import this project in eclipse first we need to convert this project according to eclipse and then import it.

  • Convert to Eclipse Maven Project : Use below command
    mvn eclipse:eclipse -Dwtpversion=2.0
C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp>mvn eclipse:eclipse -Dwtpversion=2.0
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestMavenWebApp Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ TestMavenWebApp >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ TestMavenWebApp <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ TestMavenWebApp ---
[INFO] Adding support for WTP version 2.0.
[INFO] Using Eclipse Workspace: C:\Users\facingissuesonit\workspace-sample
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\Program Files\Java\jre1.8.0_73
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "TestMavenWebApp" to C:\Users\facingissuesonit\workspace-sample\TestMavenWebApp.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.608 s
[INFO] Finished at: 2017-06-09T16:54:24-04:00
[INFO] Final Memory: 14M/115M
[INFO] ------------------------------------------------------------------------ 

Import in Eclipse : Follow below steps to import TestMavenWebApp project in Eclipse

Eclipse File -> Import -> General-> Existing Project in Workspace -> Select Project TestMavenWebApp

It will show some compile time issue on index.jsp page because   not attached with any Web Server and also select Java compiler version from facet as given screen.

TestMavenWebApp Project -> Right Click ->Properties -> Project Facets

Select Runtime and Java compiler version as configured in class path. Click on apply and OK.

maven web facet server

Your Project is ready to more development and configured in eclipse . It will have below directory Structure.

Maven Web Project Directory

Run Project:

TestMavenWebApp Project ->Run As ->Run On Server

We will get below screen after successful execution.

Maven Web Run

Leave you feedback to enhance more on this topic so that make it more helpful for others.

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 10 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Maven and tagged , . Bookmark the permalink.