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 ( ) 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.


<project xmlns="" xmlns:xsi=""
	<name>mywebapp Maven Webapp</name>
			<!-- begin - precompiling jsps -->
			<!-- end - precompiling jsps -->
			<!-- begin - deploying/undeploying to Tomcat -->
			<!-- end - deploying/undeploying to Tomcat -->
			<!-- begin - compiling project to a particular version of java -->
			<!-- end - compiling project to a particular version of java -->
			<!-- begin - setting classpath to use an Eclipse user library for Tomcat (for eclipse:eclipse) -->
			<!-- end - setting classpath to use an Eclipse user library for Tomcat (for eclipse:eclipse) -->

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="" xmlns=""
	<display-name>My Web App</display-name>

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] ------------------------------------------------------------------------
[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 >