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

(Continued from page 1)

What we need to do to fix how our project builds is to add the necessary dependency or dependencies to pom.xml. We are missing the servletW classes, so we can fix this by adding the servlet-api jarW file as a dependency in pom.xml. I set the scope to 'provided' since this jar file will be provided by TomcatSW, so it doesn't need to be included in the warW file.

	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<scope>provided</scope>
	</dependency>

Here is the updated pom.xml after adding the servlet-api jar file.

pom.xml after 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>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</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>

If we now perform a "mvn package" on the project, the mavenSW command will be completed without errors, as shown below. The "mywebapp.war" file has been created in the project's target directory.

output from 'mvn package' after 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] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] [war:war]
[INFO] Packaging webapp
[INFO] Assembling webapp[mywebapp] in [C:\dev\workspace\mywebapp\target\mywebapp]
[INFO] Processing war project
[INFO] Webapp assembled in[93 msecs]
[INFO] Building war: C:\dev\workspace\mywebapp\target\mywebapp.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Wed Feb 06 00:16:25 PST 2008
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------

In this tutorial, we've seen how the jars in a classpathW are not necessarily equivalent to the jars required to build a project. We've seen how we can build a simple web application project by adding a dependency to pom.xml. We scoped this servlet-api dependency to have 'provided' scope since it will be provided by Tomcat, so it doesn't need to be packaged in the war file.

Page: < 1 2