Maven 2: Building Web Projects

By default Maven 2 provides a standard way of building web applications. All of the web files such as web.xml and tag libraries and must be stored in /src/main/webapp directory.

While this is perfectly fine for the new applications for some existing projects this file structure might be inappropriate. Unfortunately some times you cannot change the existing file structure as you pleased. For example, if you use PVCS as your source control system, then you don’t want to change the existing file structure because it is very difficult to update the source repository.

Another issue that you might have is integration with development tools such as WSAD or Eclipse Web Tools. By default they use different file structure for web projects and unless you want to take a way from people the ability to use debugger you better follow the same file structure as WSAD suggests.

To implement this we will have to modify our POM file and configure maven-war-plugin to tell it where to find web resources. You need to specify the path to the folder that contains the web content. This is how you configure the war plug-in

<?xml version="1.0" encoding="UTF-8"?>
<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.garbuz.maven</groupId>
	<artifactId>test-web-project</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.0</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Comments are closed.