How do I precompile my jsps?
Author: Deron Eriksson
Description: This tutorial describes how to precompile your JSPs using the maven jspc plugin.
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 >

The mavenSW jspc plugin ( http://mojo.codehaus.org/jspc-maven-plugin/ ) is a maven plugin that can be used to precompile your jspsW. It does this by compiling the jsps in a project and inserting servletW and servlet mapping entries into the final web.xmlW file, which gets packaged into the project's warW file.

To use it, you need references to jspc-maven-plugin and maven-war-plugin in your project's pom.xml. The precompiling takes place during the "compile" phase of the maven build (default) lifecycle. The jspc maven plugin writes the servlet and servlet mappings to a target/jspweb.xml file, and the maven war plugin copies this file and uses it in the war file that it builds.

I inserted the jspW precompilation modifications required in my pom.xml in between the <!-- begin - precompiling jsps --> and <!-- end - precompiling jsps --> comments.

pom.xml

<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>
			<!-- begin - precompiling jsps -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jspc-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>jspc</id>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<webXml>${basedir}/target/jspweb.xml</webXml>
				</configuration>
			</plugin>
			<!-- end - precompiling jsps -->
			<!-- begin - deploying/undeploying to Tomcat -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<configuration>
					<url>http://192.168.1.7:8080/manager</url>
					<server>mytomcat</server>
					<path>/mywebapp</path>
				</configuration>
			</plugin>
			<!-- end - deploying/undeploying to Tomcat -->
			<!-- begin - compiling project to a particular version of java -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<!-- end - compiling project to a particular version of java -->
			<!-- begin - setting classpath to use an Eclipse user library for Tomcat (for eclipse:eclipse) -->
			<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>
			<!-- end - setting classpath to use an Eclipse user library for Tomcat (for eclipse:eclipse) -->
		</plugins>
	</build>
</project>

Note that in previous tutorials I used a servlet spec 2.3 deployment descriptor (ie, web.xml file). For this tutorial, I bumped it up to 2.5. I did this by updating the top of my web.xml file to reference the 2.5 xsd. Supposedly, you can use a 2.3 web.xml file, but the fragment insertion did not occur in the correct location when I tried it. So, I just updated my web.xml to 2.5 (2.4 would have been fine too), since the order of the servlet and servlet mappings in web.xml doesn't matter for the 2.4 and 2.5 specs. For more information about using this plugin with the servlet 2.3 specification, please consult the plugin documentation.

web.xml (src/main/webapp/WEB-INF/web.xml)

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>My Web App</display-name>
	<servlet>
		<description></description>
		<display-name>TestServlet</display-name>
		<servlet-name>TestServlet</servlet-name>
		<servlet-class>my.TestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/test</url-pattern>
	</servlet-mapping>
</web-app>

I performed a "mvn package" on the "mywebapp" project:

Executing 'mvn package' on 'mywebapp' project

The console output from "mvn package" is shown below. Notice that the index.jsp file gets precompiled during the compile phase of the maven lifecycle.

Console output from 'mvn package'

[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] [jspc:compile {execution: jspc}]
[INFO] Built File: \index.jsp
[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[105 msecs]
[INFO] Building war: C:\dev\workspace\mywebapp\target\mywebapp.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Fri Feb 08 10:13:46 PST 2008
[INFO] Final Memory: 10M/19M
[INFO] ------------------------------------------------------------------------

(Continued on page 2)

Page:    1 2 >