Maven Web Application

Maven web application for beginners and professionals with topics on maven example, plugin, pom, dependency, eclipse, repository, web application, eclipse example, servlet, jsp, struts, hibernate, spring etc.

Maven Web Application

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:

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

Example

The example to generate the project architecture is given below:

  1. mvn archetype:generate -DgroupId=com.samtpoint -DartifactId=CubeGeneratorWeb   
  2. -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:

  1. mvn archetype:generate -DgroupId=com.javatpoint -DartifactId=CubeGeneratorWe  
  2. -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false  
  3. [INFO] Scanning for projects...  
  4. [INFO]  
  5. [INFO] ------------------------------------------------------------------------  
  6. [INFO] Building Maven Stub Project (No POM) 1  
  7. [INFO] ------------------------------------------------------------------------  
  8. [INFO]  
  9. [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>  
  10. >  
  11. [INFO]  
  12. [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<  
  13. <  
  14. [INFO]  
  15. [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --  
  16. -  
  17. [INFO] Generating project in Batch mode  
  18. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav  
  19. en-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar  
  20. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave  
  21. n-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 3.8 KB/sec)  
  22. Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mav  
  23. en-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom  
  24. Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/archetypes/mave  
  25. n-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 0.8 KB/sec)  
  26. [INFO] -------------------------------------------------------------------------  
  27. ---  
  28. [INFO] Using following parameters for creating project from Old (1.x) Archetype:  
  29.  maven-archetype-webapp:1.0  
  30. [INFO] -------------------------------------------------------------------------  
  31. ---  
  32. [INFO] Parameter: groupId, Value: com.javatpoint  
  33. [INFO] Parameter: packageName, Value: com.javatpoint  
  34. [INFO] Parameter: package, Value: com.javatpoint  
  35. [INFO] Parameter: artifactId, Value: CubeGeneratorWeb  
  36. [INFO] Parameter: basedir, Value: D:\  
  37. [INFO] Parameter: version, Value: 1.0-SNAPSHOT  
  38. [INFO] project created from Old (1.x) Archetype in dir: D:\CubeGeneratorWeb  
  39. [INFO] ------------------------------------------------------------------------  
  40. [INFO] BUILD SUCCESS  
  41. [INFO] ------------------------------------------------------------------------  
  42. [INFO] Total time: 10.273s  
  43. [INFO] Finished at: Thu Dec 26 19:25:04 IST 2013  
  44. [INFO] Final Memory: 10M/24M  
  45. [INFO] ------------------------------------------------------------------------  
  46. 'cmd' is not recognized as an internal or external command,  
  47. 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:

  1. CubeGenerator  
  2. -src  
  3. --main  
  4. ---resources  
  5. ---webapp  
  6. ----WEB-INF  
  7. -----web.xml  
  8. ----index.jsp  
  9. -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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  4. http://maven.apache.org/maven-v4_0_0.xsd">  
  5.   <modelVersion>4.0.0</modelVersion>  
  6.   <groupId>com.javatpoint</groupId>  
  7.   <artifactId>CubeGeneratorWeb</artifactId>  
  8.   <packaging>war</packaging>  
  9.   <version>1.0-SNAPSHOT</version>  
  10.   <name>CubeGeneratorWeb Maven Webapp</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   <dependencies>  
  13.     <dependency>  
  14.       <groupId>junit</groupId>  
  15.       <artifactId>junit</artifactId>  
  16.       <version>3.8.1</version>  
  17.       <scope>test</scope>  
  18.     </dependency>  
  19.   </dependencies>  
  20.   <build>  
  21.     <finalName>CubeGeneratorWeb</finalName>  
  22.   </build>  
  23. </project>  

2) Automatically Generated index.jsp file

  1. <html>  
  2. <body>  
  3. <h2>Hello World!</h2>  
  4. </body>  
  5. </html>  

3) Automatically Generated web.xml file

  1. <!DOCTYPE web-app PUBLIC  
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.   <display-name>Archetype Created Web Application</display-name>  
  7. </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 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

-> Next -> Browse Project

-> Finish.

3) Run the maven web project

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