How to create Maven based Web Application? You will learn to create web application using maven archetype. We are using the Maven 3 build tool.
In this tutorial I will explain you the process and command of creating a web application using Maven 3 project management tool. You will also learn how to run it on the Tomcat Server. We will create the war file using
I am assuming you are aware of web applications and you have experience in the development of web applications. Here we will create a web application using Maven archetype.
Let's Start developing the application.
Step 1: Create sample web application using Maven archetype
Now we will tell you about the maven archetype for creating the web application. The maven-archetype-webapp is used to create web application using maven tool. Here is the syntax of the command:
mvn archetype:generate -DgroupId={package} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
You can replace the package and the project-name values and then create the project.
Here is the video instruction of creating web application in Mave: "How to make a web application with Maven?"
Here is simple example of creating web application:
mvn archetype:generate -DgroupId=net.roseindia -DartifactId=myfirstwebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Here is the output of the command:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\user>d: D:\>mkdir mavenexamples D:\>cd mavenexamples D:\mavenexamples>mvn archetype:generate -DgroupId=net.roseindia -DartifactId=myfirstwebapp -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.0-alpha-3:generate (default-cli) @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.0-alpha-3:generate (default-cli) @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.0-alpha-3:generate (default-cli) @ standalone-pom --- [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] Generating project in Interactive mode Define value for version: 1.0-SNAPSHOT: : Confirm properties configuration: groupId: net.roseindia artifactId: myfirstwebapp version: 1.0-SNAPSHOT package: net.roseindia Y: : y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: net.roseindia [INFO] Parameter: packageName, Value: net.roseindia [INFO] Parameter: package, Value: net.roseindia [INFO] Parameter: artifactId, Value: myfirstwebapp [INFO] Parameter: basedir, Value: D:\mavenexamples [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: D:\mavenexamples\myfirstwebapp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.411s [INFO] Finished at: Tue Jan 14 11:07:49 IST 2014 [INFO] Final Memory: 7M/17M [INFO] ------------------------------------------------------------------------ D:\mavenexamples>
Step 2: Understanding Maven directory structure.
The above command will create a web application. Here is the directory structure of the web application:
myfirstwebapp |- src |---main |----resources |----webapp |-----index.jsp |-----WEB-INF |------web.xml |-pom.xml
Step 3: The maven pom.xml file
The maven pom.xml file contains the information about project and its dependencies. Here is the code of the 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>net.roseindia</groupId> <artifactId>myfirstwebapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>myfirstwebapp 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>myfirstwebapp</finalName> </build> </project>
Step 4: Code of the web.xml file
Here is the code of the 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>
Step 5: Code of index.jsp file.
Here is the code of the index.jsp file:
<html> <body> <h2>Hello World!</h2> </body> </html>
The index.jsp file simply displays the "Hello World!" message on browser.
Step 6: Building war file
First of go to the directory "myfirstwebapp". To create a war file you just have to type following command on the console:
mvn package
Here is the output of the program:
D:\mavenexamples\myfirstwebapp>mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myfirstwebapp Maven Webapp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myfirstwebapp --- [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:2.5.1:compile (default-compile) @ myfirstwebapp --- [INFO] No sources to compile [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myfirstwebapp --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\mavenexamples\myfirstwebapp\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ myfirstwebapp --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myfirstwebapp --- [INFO] No tests to run. [INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ myfirstwebapp --- [INFO] Packaging webapp [INFO] Assembling webapp [myfirstwebapp] in [D:\mavenexamples\myfirstwebapp\target\myfirstwebapp] [INFO] Processing war project [INFO] Copying webapp resources [D:\mavenexamples\myfirstwebapp\src\main\webapp] [INFO] Webapp assembled in [27 msecs] [INFO] Building war: D:\mavenexamples\myfirstwebapp\target\myfirstwebapp.war [INFO] WEB-INF\web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.421s [INFO] Finished at: Tue Jan 14 12:09:53 IST 2014 [INFO] Final Memory: 5M/15M [INFO] ------------------------------------------------------------------------ D:\mavenexamples\myfirstwebapp>
Step 7: Deploying application
Copy the war file into webapps directory of your Tomcat server and start the Tomcat. Type http://localhost:8080/myfirstwebapp in your browser. The "Hello World!" message should be displayed in the brower.