| |||||||||
Compiling JSP pages using ANT
Compiling JSP pages using ANTWhen JSP pages are uploaded to a web server and they are not compiled before a request for the page. Upon displaying the JSP for the first time, the webserver automatically compiles the JSP file into a servlet java file which then is compiled into a servlet class file. Then, an internal mapping similar to theweb.xml is updated. This approach raises a number of issues.
.war file which can be dumped into your webservers webapp/ dir. The
server should then auto-deploy it. To get to the .war file a number of steps are taken in ANT.
<!-- path of the web source files -->
<property name="webapp.path" value="." />
<!-- build output name -->
<property name="output_war" value="myapp.war" />
<!-- paths to the Tomcat JSP compiler -->
<property name="catalina.home" value="C:\Programmer\Tomcat 5.5" />
<target name="make_war" depends="compile">
<description>Harvest the compiled files and html files into a WAR file ready for deployment</description>
<war destfile="${output_war}" webxml="${webapp.path}/WEB-INF/web.xml">
<classes dir="${webapp.path}/WEB-INF/classes"/>
<zipfileset dir="${webapp.path}/graphics/images/" prefix="images"/>
</war>
</target>
<target name="compile" depends="jsp_compile">
<description>
Compile the generated servlets and change web.xml into mapping the servlets to the corresponding .jsp urls.
</description>
<mkdir dir="${webapp.path}/WEB-INF/classes"/>
<mkdir dir="${webapp.path}/WEB-INF/lib"/>
<javac destdir="${webapp.path}/WEB-INF/classes" srcdir="${webapp.path}/WEB-INF/src" >
<classpath>
<pathelement location="${webapp.path}/WEB-INF/classes" />
<fileset dir="${webapp.path}/WEB-INF/lib"> <include name="*.jar" /></fileset>
<pathelement location="${catalina.home}/common/classes" />
<fileset dir="${catalina.home}/common/lib"> <include name="*.jar" /></fileset>
<pathelement location="${catalina.home}/shared/classes" />
<fileset dir="${catalina.home}/shared/lib"> <include name="*.jar" /></fileset>
<fileset dir="${catalina.home}/bin"> <include name="*.jar" /></fileset>
</classpath>
<include name="**" />
<exclude name="tags/**" />
</javac>
</target>
<target name="jsp_compile" depends="clean_jspcompile">
<description>Take all .jsp pages and convert them into servlets using the Tomcat jsp compiler</description>
<taskdef classname="org.apache.jasper.JspC" name="jasper2">
<classpath id="jspc.classpath">
<fileset dir="${catalina.home}/bin"><include name="*.jar"/> </fileset>
<fileset dir="${catalina.home}/server/lib"> <include name="*.jar"/> </fileset>
<fileset dir="${catalina.home}/common/lib"> <include name="*.jar"/> </fileset>
</classpath>
</taskdef>
<jasper2 uriroot="${webapp.path}"
webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"
addWebXmlMappings="true"
outputDir="${webapp.path}/WEB-INF/src/" />
<!-- path to the compiler generated web.xml -->
<!-- automatically merge the "generated_web.xml" into "web.xml" -->
<!-- path to the output of the compilation -->
</target>
<target name="clean_jspcompile">
<description>clear the generated source files</description>
<delete dir="${webapp.path}/WEB-INF/src"/>
</target>
Finally, you may run into problems when using the JSP include mechanism, since if you have
a lot of "generic building blocks" these may be compiled into servlets,
but they are not valid classes, i.e. when being compiled to byte-code, the compilation fails.
To prevent this you need a target after the JSPs have been compiled
that deletes these invalid servlet classes, before the servlets and the java code base is compiled.
CommentsIf you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.If you like this site consider Help spread the wordShare this post on your favorite social bookmarking sites:
The most recent contributions 28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers. 13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise" 20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba". 16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words. Nothing of interest? Try browsing the entire article archive... | |||||||||