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
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:
C:MVNProject>mvn archetype:generate -DgroupId=net.javabeat -DartifactId=SampleWebapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
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:
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] ------------------------------------------------------------------------
Maven creates the complete web based java application project structure as in the screen below:
We see that a default pom.xml has been created. POM file generated is:
<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>
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:
<html> <body> <h2>Hello World!</h2> </body> </html>
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).
&lt;?xml version='1.0' encoding='utf-8'?&gt; &lt;tomcat-users&gt; &lt;role rolename=&quot;manager&quot;/&gt; &lt;role rolename=&quot;admin&quot;/&gt; &lt;user username=&quot;admin&quot; password=&quot;admin&quot; roles=&quot;admin,manager&quot;/&gt; &lt;/tomcat-users&gt;
To login into Tomcat Manager app we will use:
Username=admin Password=admin
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:
&lt;settings&gt; &lt;servers&gt; &lt;server&gt; &lt;id&gt;myserver&lt;/id&gt; &lt;username&gt;admin&lt;/username&gt; &lt;password&gt;admin&lt;/password&gt; &lt;/server&gt; &lt;/servers&gt; ....... .....
Point POM to Tomcat Server
Edit section in the the pom.xml file (under the directory C:MVNProjectSampleWebapppom.xml) as follows:
&lt;build&gt; &lt;finalName&gt;SampleWebapp&lt;/finalName&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt; &lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;server&gt;myserver&lt;/server&gt; &lt;path&gt;/SampleWebapp&lt;/path&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt;
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.