Javatpoint Logo
Javatpoint Logo

Maven Web Application

We can create a simple maven web application example by executing the archetype:generate command of mvn tool.

To create a simple java project using maven, you need to open command prompt and run the archetype:generate command of mvn tool.

Syntax

The syntax to generate the project architecture is given below:

mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=booleanValue

Example

The example to generate the project architecture is given below:

mvn archetype:generate -DgroupId=com.javatpoint -DartifactId=CubeGeneratorWeb 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Note: Here, we are using maven-archetype-webapp to create simple maven web application. if you use maven-archetype-quickstart, it will generate a simple maven core project.

Output

Now it will generate following code in the command prompt:

mvn archetype:generate -DgroupId=com.javatpoint -DartifactId=CubeGeneratorWe
b -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:2.2:generate (default-cli) @ standalone-pom >>
>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave
n-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 3.8 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav
en-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave
n-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 0.8 KB/sec)
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-webapp:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.javatpoint
[INFO] Parameter: packageName, Value: com.javatpoint
[INFO] Parameter: package, Value: com.javatpoint
[INFO] Parameter: artifactId, Value: CubeGeneratorWeb
[INFO] Parameter: basedir, Value: D:\
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: D:\CubeGeneratorWeb
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.273s
[INFO] Finished at: Thu Dec 26 19:25:04 IST 2013
[INFO] Final Memory: 10M/24M
[INFO] ------------------------------------------------------------------------
'cmd' is not recognized as an internal or external command,
operable program or batch file.

Generated Directory Structure

Now go to the current directory from where you have executed the mvn command. For example: d:\CubeGeneratorWeb. You will see that a simple java project is created that has the following directory:

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

As you can see, there are created 3 files pom.xml, index.jsp and web.xml. Let's have a quick look at these files:

1) Automatically Generated pom.xml file

<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.javatpoint</groupId>
  <artifactId>CubeGeneratorWeb</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CubeGeneratorWeb 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>CubeGeneratorWeb</finalName>
  </build>
</project>

2) Automatically Generated index.jsp file

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

3) Automatically Generated web.xml file

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>


Deploy and Run the Maven Web Project

Now you need to deploy the project on the server and access it by the following url:

http://<host-name>:<portnumber>/projectname, for example: http://localhost:8888/CubeGeneratorWeb

maven webapp output

Maven Webapp in Eclipse

You can import the maven web project in eclipse. To do so, perform following steps:

1) Open eclipse IDE

2) Import the maven project

File Menu -> Import -> Maven -> Existing Maven Projects

maven webapp eclipse import

-> Next -> Browse Project

maven webapp eclipse project

-> Finish.

3) Run the maven web project

Right click on project -> Run As -> Run on Server


Directory Structure of Maven Webapp in Eclipse

maven webapp eclipse directory structure
Next TopicMaven Plugin




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA