The directory structure we will use has a root directory we will name with the project name. The necessary files are the pom.xml, App.java and web.xml.
mavenquickstart --pom.xml --src ----main ------java --------com ----------bookstore ------------App.java ------webapp --------WEB-INF ----------web.xml
The pom.xml file is as follows.
<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>mavenquickstart</groupId> <artifactId>mavenquickstart</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>mavenquickstart</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>mavenquickstart</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <url>http://localhost:8080/manager</url> <server>myserver</server> <warFile> ${CATALINA_HOME}/webapps/mavenquickstart.war </warFile> </configuration> </plugin> </plugins> </build> </project>
<!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> <servlet> <servlet-name>simple</servlet-name> <servlet-class>com.bookstore.App</servlet-class> </servlet> <servlet-mapping> <servlet-name>simple</servlet-name> <url-pattern>/simple</url-pattern> </servlet-mapping> </web-app>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>myserver</id> <username>Tomcat-Manager-Userid</username> <password>Tomcat-Manager-Password</password> </server> </servers> </settings>
package com.bookstore; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * report on status of servlet */ public class App extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println( "SimpleServlet Executed" ); out.flush(); out.close(); } }
Run the appropriate command to compile and deploy or redeploy the application.
mvn clean package tomcat:deploy
mvn clean package tomcat:redeploy