• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Creating Simple Web Application Using Apache Maven

February 23, 2013 //  by Manisha Patil//  Leave a Comment

In the previous post we saw how to create simple Java project using Maven. We will create a simple web application. The goal of this post is to highlight the basic features Maven provides to develop web applications. This post covers the following :

  • Create simple web application using Maven Archetype plugin.
  • Prepare the Tomcat Manager application.
  • Define Tomcat Server in Maven settings.
  • Point POM to Tomcat Server.
  • Build and Deploy the web application to Tomcat server.

If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on Java topics, please subscribe here.

  • Apache Maven 2.0 – Maven Plugins
  • Buy: Apache Maven Implementation from flipkart.com

apache maven

Create Simple Web Application using Maven

To create a web application we will use maven-archetype-webapp plugin. Run mvn archetype:generate with an artifactId and a groupId. Open command line window, point to the desired directory (here we are creating project under C:MVNProject project) and execute the following command:

[java] C:MVNProject>mvn archetype:generate -DgroupId=net.javabeat -DartifactId=SampleWebapp
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[/java]

Maven Commands

    • mvn : is the Maven command.
    • archetype:generate : is called a Maven goal, analogous to an ANT target. It describes a unit of work to be completed in a build. The purpose of this goal is to quickly create(goal) a project from an archetype(plugin).
    • -Dname=value pairs : (DgroupId, DartifactId, DarchetypeArtifactId, DinteractiveMode) These are the arguments which are passed to the goal.They take the form of -D properties, similar to the system property options which you pass to the Java Virtual Machine via the command line.

The output on executing the above command is:

[java] C:MVNProject>mvn archetype:generate -DgroupId=net.javabeat -DartifactId=SampleWebapp
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects…
Downloading: https://www.javabeat.net/pluginrepo1/org/codehaus/mojo/maven-metadata.xml
Downloading: https://www.javabeat.net/pluginrepo1/org/apache/maven/plugins/maven-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins/maven-metadata.xml from pluginrepo1
(https://www.javabeat.net/pluginrepo1/): Error transferring file: Connection reset
[WARNING] Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from pluginrepo1
(https://www.javabeat.net/pluginrepo1/): Error transferring file: Connection reset
Downloading: https://www.javabeat.net/pluginrepo1/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml
from pluginrepo1 (https://www.javabeat.net/pluginrepo1/): Error transferring file: Connection reset
[INFO] [INFO] ————————————————————————
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ————————————————————————
Downloading: https://www.javabeat.net/pluginrepo1/org/apache/maven/plugins/maven-metadata.xml
Downloading: https://www.javabeat.net/pluginrepo1/org/codehaus/mojo/maven-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins/maven-metadata.xml from pluginrepo1
(https://www.javabeat.net/pluginrepo1/): Error transferring file: Connection reset
[WARNING] Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from pluginrepo1
(https://www.javabeat.net/pluginrepo1/): Error transferring file: Connection reset
Downloading: https://www.javabeat.net/pluginrepo1/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins:maven-archetype-plugin/maven-metadata.xml
from pluginrepo1 (https://www.javabeat.net/pluginrepo1/): Error transferring file: Connection reset
[INFO] [INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
[INFO] [INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
[INFO] [INFO] — maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom —
[INFO] Generating project in Batch mode
[INFO] —————————————————————————-
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] —————————————————————————-
[INFO] Parameter: groupId, Value: net.javabeat
[INFO] Parameter: packageName, Value: net.javabeat
[INFO] Parameter: package, Value: net.javabeat
[INFO] Parameter: artifactId, Value: SampleWebapp
[INFO] Parameter: basedir, Value: C:MVNProject
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: C:MVNProjectSampleWebapp
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 10:01.500s
[INFO] Finished at: Fri Feb 22 21:32:54 IST 2013
[INFO] Final Memory: 8M/24M
[INFO] ————————————————————————
[/java]

Maven creates the complete web based java application project structure as in the screen below:
webapp_struct

We see that a default pom.xml has been created. POM file generated is:

[java] <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.javabeat</groupId>
<artifactId>SampleWebapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SampleWebapp 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>SampleWebapp</finalName>
</build>
</project>
[/java]

Here we notice that the packaging element has a value war. A project with a war packaging creates a WAR file in the target directory. Default name of this file is ${artifactId}-${version}.war. Hence our WAR generated would be in target/SampleWebapp-1.0-SNAPSHOT.war. The element finalName customizes the name of the generated WAR.

Maven also created a sample JSP Source file index.jsp:

[java] <html>
<body>
<h2>Hello World!</h2>
</body>
</html>
[/java]

Prepare the Tomcat Manager application

We will deploy our web application to Tomcat server. We first need to ensure that we can access the Tomcat Manager application at: http://localhost:8080/manager/html. Just ensure that the <tomcat>/conf/tomcat-users.xml file has the following defined:(where <tomcat> is the Tomcat installed directory).

[java] &amp;lt;?xml version=’1.0′ encoding=’utf-8′?&amp;gt;
&amp;lt;tomcat-users&amp;gt;
&amp;lt;role rolename=&amp;quot;manager&amp;quot;/&amp;gt;
&amp;lt;role rolename=&amp;quot;admin&amp;quot;/&amp;gt;
&amp;lt;user username=&amp;quot;admin&amp;quot; password=&amp;quot;admin&amp;quot; roles=&amp;quot;admin,manager&amp;quot;/&amp;gt;
&amp;lt;/tomcat-users&amp;gt;
[/java]

To login into Tomcat Manager app we will use:

[java]

Username=admin
Password=admin

[/java]

 

Define Tomcat Server in Maven settings

Edit the Maven settings.xml (e.g:C:Documents and SettingsManisha.m2settings.xml), add a server myserver. Add the credentials to log into the Tomcat Manager application:

[java] &amp;lt;settings&amp;gt;
&amp;lt;servers&amp;gt;
&amp;lt;server&amp;gt;
&amp;lt;id&amp;gt;myserver&amp;lt;/id&amp;gt;
&amp;lt;username&amp;gt;admin&amp;lt;/username&amp;gt;
&amp;lt;password&amp;gt;admin&amp;lt;/password&amp;gt;
&amp;lt;/server&amp;gt;
&amp;lt;/servers&amp;gt;
…….
…..
[/java]

Point POM to Tomcat Server

Edit section in the the pom.xml file (under the directory C:MVNProjectSampleWebapppom.xml) as follows:

[java] &amp;lt;build&amp;gt;
&amp;lt;finalName&amp;gt;SampleWebapp&amp;lt;/finalName&amp;gt;
&amp;lt;plugins&amp;gt;
&amp;lt;plugin&amp;gt;
&amp;lt;groupId&amp;gt;org.codehaus.mojo&amp;lt;/groupId&amp;gt;
&amp;lt;artifactId&amp;gt;tomcat-maven-plugin&amp;lt;/artifactId&amp;gt;
&amp;lt;configuration&amp;gt;
&amp;lt;server&amp;gt;myserver&amp;lt;/server&amp;gt;
&amp;lt;path&amp;gt;/SampleWebapp&amp;lt;/path&amp;gt;
&amp;lt;/configuration&amp;gt;
&amp;lt;/plugin&amp;gt;
&amp;lt;/plugins&amp;gt;
&amp;lt;/build&amp;gt;
[/java]

We have added the Tomcat plugin for Maven. The <configuration> section points to the server we defined in settings.xml (‘myserver’). Through <finalName> and the <path> we define the web context we want to deploy to. We’ll be able to access our application at http://localhost:8080/SampleWebapp.

Build and Deploy the web application to Tomcat server

Open the command window console, and go to the directory where pom.xml is located (C:MVNProjectSampleWebapppom.xml). Execute the following command:
mvn tomcat:deploy
If “BUILD SUCCESS” message appears, then access the application at:
http://localhost:8080/SampleWebapp/.
If you want to deploy again, then use the following command:
mvn tomcat:redeploy

Summary

  • Apache Maven 2.0 – Maven Plugins
  • Buy: Apache Maven Implementation from flipkart.com

In this post we did learn how to create a simple webapp using the Maven archetype plugin. We checked the directory structure and the generated source code. We then did learn about how to build and deploy this simple web app in the Tomcat server using Maven command. If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on Java topics, please subscribe here.

Category: Apache MavenTag: Apache Maven

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Previous Post: « Example for a class implementing comparable interface in Java
Next Post: Working with DOM Attributes using jQuery jquery»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact