Start a webapp (web application) project with Maven
September 07, 2009 16:39:37 Last update: September 07, 2009 18:43:04
It's easiest to use the archetype plugin to start a new Maven project. I'll use struts 1 as example since it's not in the built-in archetypes for
archetype:generate
.
- Generate a simple
webapp
witharchetype:generate
:C:\work\maven>mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=maven-tutorial -DartifactId=struts1app [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:generate] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Preparing archetype:generate [INFO] No goals needed for project - skipping [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] [archetype:generate {execution: default-cli}] [INFO] Generating project in Interactive mode Define value for version: 1.0-SNAPSHOT: : Confirm properties configuration: groupId: maven-tutorial artifactId: struts1app version: 1.0-SNAPSHOT package: maven-tutorial Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: maven-tutorial [INFO] Parameter: packageName, Value: maven-tutorial [INFO] Parameter: package, Value: maven-tutorial [INFO] Parameter: artifactId, Value: struts1app [INFO] Parameter: basedir, Value: C:\work\maven [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: C:\work\maven\struts1app [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 20 seconds [INFO] Finished at: Mon Sep 07 10:36:45 CDT 2009 [INFO] Final Memory: 8M/14M [INFO] ------------------------------------------------------------------------ C:\work\maven>
It generates a directory structure like this:struts1app struts1app/pom.xml struts1app/src struts1app/src/main struts1app/src/main/resources struts1app/src/main/webapp struts1app/src/main/webapp/index.jsp struts1app/src/main/webapp/WEB-INF struts1app/src/main/webapp/WEB-INF/web.xml
with a simple POM:<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>maven-tutorial</groupId> <artifactId>struts1app</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>struts1app 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>struts1app</finalName> </build> </project>
- Create
settings.xml
in$HOME/.m2
, add Java.net repository for Java EE dependencies:<?xml version="1.0" encoding="UTF-8"?> <settings> <profiles> <profile> <id>DefaultProfile</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> </profile> </profiles> </settings>
- Add Java EE and Struts dependencies in
pom.xml
. Note that the Java EE dependency has scopeprovided
, meaning that the web app container provides the jars, therefore we don't need to bundle them with ourwar
fie.<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>maven-tutorial</groupId> <artifactId>struts1app</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>struts1app 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> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0-SNAPSHOT</version> <scope>provided</scope> </dependency> <dependency> <groupId>struts</groupId> <artifactId>struts</artifactId> <version>1.2.9</version> </dependency> </dependencies> <build> <finalName>struts1app</finalName> </build> </project>
- Create a directory named
java
undermain
, create the Struts form and action classes:src/main/java src/main/java/org src/main/java/org/xinotes src/main/java/org/xinotes/HelloAction.java src/main/java/org/xinotes/HelloForm.java
package org.xinotes; import org.apache.struts.action.ActionForm; public class HelloForm extends ActionForm { private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
package org.xinotes; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.*; public class HelloAction extends Action { public ActionForward execute(ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { HelloForm f = (HelloForm) form; req.setAttribute("name", f.getName()); return map.findForward("hello"); } }
- Create a directory named
jsp
underwebapp
, and create 2 jsp files:
index.jsp:<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html> <body> <html:form action="/hello" method="post"> Enter Name: <html:text property="name"/> <br/> <input type="submit" name="submit" value="Go"/> </html:form> </body> </html>
hello.jsp:<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <html> <body> <h2>Hello, <bean:write name="name"/>!</h2> </body> </html>
- Change the contents of the existing
index.jsp
to:<html> <head> <meta http-equiv="refresh" content="0;URL=index.do"> </head> <body> </body> </html>
- Create
struts-config.xml
underWEB-INF
:<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <global-forwards> <forward name="index" path="/jsp/index.jsp"/> </global-forwards> <form-beans> <form-bean name="helloForm" type="org.xinotes.HelloForm" /> </form-beans> <action-mappings> <action path="/index" name="helloForm" type="org.apache.struts.actions.ForwardAction" parameter="/jsp/index.jsp" validate="false"> </action> <action path="/hello" name="helloForm" scope="request" type="org.xinotes.HelloAction" validate="false"> <forward name="hello" path="/jsp/hello.jsp" /> </action> </action-mappings> </struts-config>
- Change the contents of
WEB-INF/web.xml
to:<!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>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
- The final structure of the project should look like:
struts1app struts1app/pom.xml struts1app/src struts1app/src/main struts1app/src/main/java struts1app/src/main/java/org struts1app/src/main/java/org/xinotes struts1app/src/main/java/org/xinotes/HelloAction.java struts1app/src/main/java/org/xinotes/HelloForm.java struts1app/src/main/resources struts1app/src/main/webapp struts1app/src/main/webapp/index.jsp struts1app/src/main/webapp/jsp struts1app/src/main/webapp/jsp/hello.jsp struts1app/src/main/webapp/jsp/index.jsp struts1app/src/main/webapp/WEB-INF struts1app/src/main/webapp/WEB-INF/struts-config.xml struts1app/src/main/webapp/WEB-INF/web.xml
- Finally, build with "
mvn package
".