Monday, April 26, 2010

Pre-compile JSPs from a Maven Overlay

Maven WAR Overlays are wonderful things, allowing for manipulation of a WAR artifact published by someone else into what you need for your application.

Pre-compiling JSPs (JSPC) is great when you don't want users to potentially be hit with the wait for your container to compile the JSP on first visit.

Here is how to put the two together:

    
    
        org.jasig.portlet
        WeatherPortlet
        ${WeatherPortlet.version}
        war
    
    
    
        javax.portlet
        portlet-api
        provided
    
    
        org.apache.pluto
        pluto-taglib
        provided
    


    
        
            org.apache.maven.plugins
            maven-war-plugin
            2.0.2
            
            
                
                    build-directory-tree
                    process-resources
                    
                        exploded
                    
                    
                        
                            
                                org.jasig.portlet
                                WeatherPortlet
                                
                                    **
                                    META-INF/context.xml
                                
                            
                        
                    
                
                
                    build-war
                    package
                    
                        war
                    
                    
                        ${project.basedir}/target/jspweb.xml
                    
                
            
        
        
            org.codehaus.mojo.jspc
            jspc-maven-plugin
            2.0-alpha-3
            
                
                    
                        compile
                    
                
            
            
                ${project.basedir}/target/${project.artifactId}/WEB-INF/web.xml
                
                    ${project.basedir}/target/${project.artifactId}
                    
                        **/*.jsp
                    
                
            
            
                
                    org.codehaus.mojo.jspc
                    jspc-compiler-tomcat6
                    2.0-alpha-3
                
            
        
    



In the above example the following things are happening:
  1. The org.jasig.portlet:WeatherPortlet WAR artifact is declared as a dependency. This is the WAR that will have its JSPs compiled.
  2. Due to operation ordering the maven-war-plugin is split into two executions.
    1. First the build-directory-tree execution extracts the overlay WAR during Maven's process-resources phase. This ensures the WAR contents are in place before JSPC runs.
    2. Second the build-war execution re-packages the WAR using the JSPC generated XML file as the web.xml included in the WAR.
  3. The JSPC plugin is configured to look in the target directory for the web.xml to modify and the JSPs to compile.

No comments: