How do I package a basic maven web application?
Author: Deron Eriksson
Description: This tutorial describes how to package a basic maven web application.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1) || Tomcat 6.0.14


Page:    1 2 >

In other tutorials, we created a mavenSW web application called "mywebapp" that we ran in TomcatSW via EclipseSW. We set up the classpathW to use an Eclipse user library that contained a set of Tomcat jarW files.

The pom.xml file is shown here. Notice that it is set up so that the eclipse:eclipse goal generates a classpath with the TOMCAT_6.0.14_LIBRARY Eclipse user library that we created.

pom.xml before update

<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>com.maventest</groupId>
	<artifactId>mywebapp</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mywebapp 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>mywebapp</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<inherited>true</inherited>
				<configuration>
					<classpathContainers>
						<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
						<classpathContainer>org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY</classpathContainer>
					</classpathContainers>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

I added a basic servletW called TestServlet to the project. I was able to hit this servlet without problems in Tomcat via Eclipse.

Now, let's see what happens when we try a "mvn package" on the "mywebapp" project:

Executing 'mvn package' on 'mywebapp' project

The output of "mvn package" executed on "mywebapp" is shown below.

output from 'mvn package' before update

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mywebapp Maven Webapp
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to C:\dev\workspace\mywebapp\target\classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[5,20] package javax.servlet does not exist

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[6,20] package javax.servlet does not exist

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[7,25] package javax.servlet.http does not exist

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[8,25] package javax.servlet.http does not exist

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[9,25] package javax.servlet.http does not exist

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[11,33] cannot find symbol
symbol: class HttpServlet
public class TestServlet extends HttpServlet implements Servlet {

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[11,56] cannot find symbol
symbol: class Servlet
public class TestServlet extends HttpServlet implements Servlet {

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[18,22] cannot find symbol
symbol  : class HttpServletRequest
location: class my.TestServlet

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[18,50] cannot find symbol
symbol  : class HttpServletResponse
location: class my.TestServlet

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[18,87] cannot find symbol
symbol  : class ServletException
location: class my.TestServlet

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[22,23] cannot find symbol
symbol  : class HttpServletRequest
location: class my.TestServlet

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[22,51] cannot find symbol
symbol  : class HttpServletResponse
location: class my.TestServlet

C:\dev\workspace\mywebapp\src\main\java\my\TestServlet.java:[22,88] cannot find symbol
symbol  : class ServletException
location: class my.TestServlet


[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue Feb 05 23:29:47 PST 2008
[INFO] Final Memory: 7M/13M
[INFO] ------------------------------------------------------------------------

We received several errors since several servlet classes could not be found by maven. This can be a little confusing at first, since our classpath contained all of the Tomcat jar files (including the required servlet classes) necessary to run the application. However, notice from the above output that the compile:compile goal fails because it can't find the necessary servlet classes. Why is our classpath (via eclipse:eclipse) fine but our project won't compile? This is because maven's compiling is based on jars in the maven repository. A working classpath is no guarantee that a project will build, since a build and a classpath are different (but related) things.

(Continued on page 2)

Page:    1 2 >