Creating webapps with Maven
I started working on a small webapp this weekend, something that might some day become public, but who knows ;). Anyways here are some notes and examples of using maven to create a webapp, (un)deploying it to a running tomcat instance and using JSTL 1.1 in it.
First, lets create the standard layout to work with in maven, we can use the webapp archetype for this:
mvn archetype:create -DgroupId=org.credmp \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-webapp
This will create the necessary structure and a simple startup webapplication. Now I want to be able to deploy it to an running tomcat instance without too much hassle; cargo to the rescue!
<build>
<finalName>my-app</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>tomcat5x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.servlet.port>8080</cargo.servlet.port>
<cargo.remote.username>tomcat</cargo.remote.username>
<cargo.remote.password>tomcat</cargo.remote.password>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
Cool, we can now run ‘mvn cargo:deploy’ and the built artifact will be deployed to a running instance. Right, now what? Well, I want to use JSTL 1.1 and some other things that are usefull in webapplications. First thing is to bring the web.xml up to date to version 2.4.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
</web-app>
And lets also add the necessary jars to the pom so that they are built / used automatically.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
We need the standard taglibs to be able to use JSTL taglib in the jsps.
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
Cool, we now have the basis to build a proper webapplication with a proper build system. For more configuration options for the cargo-maven2-plugin see the documentation.
Accurate, and succinct. I love it.
One thing that you neglected to point out, but might be valuable to mention is that with the webbapp archetype, you have to create a few more directories yourself if you are doing anything more than a “hello world” jsp.
The template creates a structure like this:
/src
/main
/resources
/webapp
/WEB-INF
web.xml
you will want to create the directories for the path:
/src/main/java/
and within there create your package directories, which should probably correspond with your groupId (the one you used during the mvn archetype:create operation)