I was looking for JSP Pre-compilation using Ant, Tomcat 6.0. It took me a while to find required resources and complete the pre-compilation for my project. I am creating this blog with the steps i have used for JSP Pre-compilation for Tomcat 6.0. There is not a lot of information available on internet which really works.
Below is the ant script for jsp-precompilation:
<target name="builddev_jspprecompile" depends="setup">
The below task helps in creating a release.jsp and places under the jsp source folder. Any time we run a build, the release jsp is updated with the MODE (DEV, QA, STAGE, PROD). Along with that the build time stamp is added to the jsp. Both these information helps to ensure each environment has the right build. The jsp can be accessed using http://localhost/myapp/release.jsp.
<!-- Build timestamp in the release.jsp -->
<echo file="${build}/src/release.jsp" message="Build in DEV mode. "/>
<tstamp>
<format property="timestamp" pattern="yyyy/MM/dd hh:mm:ss z" />
</tstamp>
<echo file="${build}/src/release.jsp" message=" Built at: ${timestamp}." append="true"/>
The below copy task copies the configuration/properties files required by the application.
<copy file="WEB-INF/src/oscache.properties" overwrite="true" tofile="${build}/${src}/WEB-INF/classes/oscache.properties" />
<!--copy file="WEB-INF/src/dev.log4j.properties" overwrite="true" tofile="${build}/${src}/WEB-INF/classes/log4j.properties" /-->
<copy file="WEB-INF/src/log4j.properties" overwrite="true" tofile="${build}/${src}/WEB-INF/classes/log4j.properties" />
<echo message="builddev: ${classpath} build: ${build}"/>
Below statements are properties for setting up J2EE-HOME. In my case, it will refer to Tomcat 6.0. J2EE-HOME has been set as an environmental variable in my computer.
<!-- these two properties are the generated web.xml fragment, and the resulting merged xml-->
<property name="jspc.webxml.fragment" value="${build}/src/WEB-INF/jspc-xml-fragment-web.xml"/>
<property name="jspc.webxml.merged" value="${build}/src/WEB-INF/jspc-merged-web.xml"/>
<property name="dir.jspc.gensrc" value="${build}/src/WEB-INF/src"/>
<property environment="env"/>
<!--property name="tomcat.home" value="C:/Software/Tomcat6.0"/ -->
<property name="tomcat.home" value="${env.J2EE-HOME}"/>
<property name="ant.home" value="${env.ANT-HOME}"/>
<echo message="Tomcat Home: ${tomcat.home}" />
Jspc class was used to generate Java code from JSP, and generates the fragmented xml file specified by the property "jspc.webxml.fragment". Below is the code snipplet for generating the java classes.
<!-- create the java class from jsp files -->
<java classname="org.apache.jasper.JspC" fork="true" failonerror="true">
<arg line="-d ${dir.jspc.gensrc} -p com.wwe.jspc -v -s -l
-uriroot ${build}/${src}
-webinc ${jspc.webxml.fragment}
-webapp ${build}/${src}"/>
<classpath>
<fileset dir="${classpath}" includes="ant.jar"/>
<fileset dir="${ant.home}/lib" includes="*.jar"/>
<fileset dir="${tomcat.home}/lib" includes="*.jar"/>
<fileset dir="${tomcat.home}/bin" includes="*.jar"/>
</classpath>
</java>
Finally Javac compiler compiles the project Java files ( including JSP files).
<!-- compile the java & jsp files -->
<javac debuglevel="lines,vars,source" srcdir="${build}/${src}/WEB-INF/src" destdir="${build}/src/${classes}" classpath="${classpath}" fork="true" failonerror="true" verbose="true">
<classpath>
<fileset dir="${classpath}" includes="*.jar"/>
<fileset dir="${tomcat.home}/lib" includes="*.jar"/>
</classpath>
<exclude name="**/common/galleryThumbnail_jsp.java" />
<exclude name="**/common/gallerybuilderfooter_jsp.java" />
</javac>
Below code merges the generated web xml with the project specific web.xml. I am using the string "<!-- @JSPC-INSERT-HERE@ -->" in my web.xml. The below code always looks for that string, and then replaces the generated xml with it.
<!-- merge the jspc generated web.xml fragment with the real web.xml-->
<loadfile property="jspc.webxml.fragment.contents" srcFile="${jspc.webxml.fragment}"/>
<copy file="${build}/src/WEB-INF/web.xml" tofile="${jspc.webxml.merged}"/>
<replace file="${jspc.webxml.merged}">
<replacefilter token="<!-- @JSPC-INSERT-HERE@ -->" value="${jspc.webxml.fragment.contents}"/>
</replace>
The last step is to create the war file including all the .class files and libraries. Any generated code can be eliminated here using excludes.
<!-- create the war file, exclude all unwanted files -->
<war destfile="${build}/${dist}/dev/ROOT.war" webxml="${jspc.webxml.merged}" duplicate="preserve">
<fileset dir="${build}/${src}">
<exclude name="**/WEB-INF/src/**" />
<exclude name="**/WEB-INF/web.xml" />
<exclude name="**/WEB-INF/*.web.xml" />
<exclude name="**/WEB-INF/*-web.xml" />
<exclude name="**/WEB-INF/classes/*.*.properties" />
<exclude name="**/WEB-INF/classes/test/**" />
<!--exclude name="**/WEB-INF/classes/**/*.*" / -->
<exclude name="**/WEB-INF/templates/samples/**/*.*" />
</fileset>
<!--classes dir="${build}/${src}/${classes}" /-->
</war>
The below task moves the war file to the J2EE-HOME deployment directory.
<!-- move the file to J2EE-HOME -->
<copy file="${build}/${dist}/dev/ROOT.war" overwrite="true" todir="${tomcat.home}/webapps" />
</target>
I kept everything under one task. This is just for me to follow up the steps. I am working on a new version to seperated the task, i might publish it in future.
Tuesday, December 16, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment