配置独立运行的jsp预编译ant脚本

作者:lizongbo 发表于:8:23 pm. 星期四, 05月 8th, 2008
版权声明:可以任意转载,转载时请务必以超链接形式标明文章和作者信息及本版权声明

配置resin开启预编译jsp,在启动时,resin会自动编译jsp,出错信息在 stdout.log中。

[code]
<web-app id=”/lizongbo” document-directory=”webapps/lizongbo”>
<listener>
<listener-class>com.caucho.jsp.JspPrecompileListener</listener-class>
<init>
<extension>jsp</extension>
<extension>jspx</extension>
<extension>xtp</extension>
</init>
</listener>
</web-app>
[/code]

在开发过程,经常将页面传到resin上,才发现页面编译失败,浪费了不少时间,因此需要一个在本地预编译进行检查的操作。

resin 3.1以前的版本提供命令行方式进行jsp预编译。

完整的命令例子如下
%JAVA_HOME%\bin\java.exe -classpath %JAVA_HOME%\lib\tools.jar;D:\Java\resin-3.0.19\lib\aopalliance.jar;D:\Java\resin-3.0.19\lib\ejb-20.jar;D:\Java\resin-3.0.19\lib\ejb-30.jar;D:\Java\resin-3.0.19\lib\j2eedeploy.jar;D:\Java\resin-3.0.19\lib\jca-15.jar;D:\Java\resin-3.0.19\lib\jms-11.jar;D:\Java\resin-3.0.19\lib\jmx-12.jar;D:\Java\resin-3.0.19\lib\jsdk-24.jar;D:\Java\resin-3.0.19\lib\jstl-11.jar;D:\Java\resin-3.0.19\lib\jta-101.jar;D:\Java\resin-3.0.19\lib\portlet-10.jar;D:\Java\resin-3.0.19\lib\quercus.jar;D:\Java\resin-3.0.19\lib\resin-jdk15.jar;D:\Java\resin-3.0.19\lib\resin.jar;D:\Java\resin-3.0.19\lib\resinboot.jar;D:\Java\resin-3.0.19\lib\script-10.jar;D:\Java\resin-3.0.19\lib\webutil.jar;D:\Java\resin-3.0.19\plugins\resin-ant.jar com.caucho.jsp.JspCompiler -app-dir  E:\lizongbo\workspace\webapp E:\lizongbo\workspace\webapp
使用起来很不方便。(注意classpath里特意加了%JAVA_HOME%\lib\tools.jar,否则会报错,
出错信息如下:
Exception in thread “main” com.caucho.java.JavaCompileException: Resin can’t loa
d com.sun.tools.javac.Main.  Usually this means that the JDK tools.jar is missin
g from the classpath, possibly because of using a JRE instead of the JDK.  You c
an either add tools.jar to the classpath or change the compiler to an external o
ne with <java compiler=’javac’/> or jikes.

java.lang.ClassNotFoundException: com.sun.tools.javac.Main
at com.caucho.java.InternalCompiler.compileInt(InternalCompiler.java:77)
at com.caucho.java.AbstractJavaCompiler.run(AbstractJavaCompiler.java:101)
at java.lang.Thread.run(Thread.java:595)
)

resin 3.1.5及以后的版本终于提供了 ant方式的jspc的atsk,但是却有bug。
[code]
<project name=”test” default=”test” basedir=”.”>
<property name=”resin.home” value=”/usr/local/share/resin”/>

<target name=”test”>
<taskdef name=”resin-jspc
classname=”com.caucho.ant.Jspc”>
<classpath>
<fileset dir=”${resin.home}”>
<include name=”plugins/resin-ant.jar”/>
<include name=”lib/*.jar”/>
</fileset>
</classpath>
</taskdef>

<resin-jspc rootDirectory=”/home/ferg/ws/dist/my-webapp”/>
</target>

</project>
[/code]
来源: http://wiki.caucho.com/Ant
在该方式下,会遇到提示说contentType重复定义的bug
出错信息如下:
E:\lizongbo\workspace\webapp\WEB-INF\ant\resin-jspc.xml:38:
com.caucho.jsp.JspLineParseException: /618119/test.jsp:6: contentType ‘text/vnd.wap.wml; charset=UTF-8′ conflicts with previous value of contentType ‘text/html; charset=UTF-8′.  Check the .jsp and any included .jsp files for conflicts.

原因是resin 3.1 对带BOM格式的UTF-8编码的jsp识别有问题。
参考: http://anotherbug.blog.chinajavaworld.com/entry/3683/0/

此时改页面是件痛苦的事情,于是想到使用Tomcatant jspC task

Tomcat 6.0.16的文件目录已经发生了变化,而官方的jspC的文档还没有更新:
http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html

以前写过一篇笔记,基本是copy tomcat的官方文档
参考: http://blog.donews.com/lizongbo/archive/2005/06/06/413985.aspx
而现在主要用resin,因此想要一个可以独立进行jsp预编译的ant脚本,
所以手工清理后制作了个独立的jspc,方便随时预编译.

所用到的jar文件为:D:\Java\apache-tomcat-6.0.16\lib和D:\Java\apache-tomcat-6.0.16\bin 目录下所有jar
[code]
D:\Java\jspc\tomcat6lib 的目录

2008-05-08  20:09    <DIR>          .
2008-05-08  20:09    <DIR>          ..
2008-01-28  23:39            10,368 annotations-api.jar
2008-01-28  23:39            17,530 bootstrap.jar
2008-01-28  23:39            49,240 catalina-ant.jar
2008-01-28  23:39           122,869 catalina-ha.jar
2008-01-28  23:39           228,175 catalina-tribes.jar
2008-01-28  23:39         1,128,229 catalina.jar
2008-01-28  23:39             9,341 commons-daemon.jar
2008-01-28  23:39            27,699 el-api.jar
2008-01-28  23:39           102,129 jasper-el.jar
2008-01-28  23:39         1,395,270 jasper-jdt.jar
2008-01-28  23:39           511,240 jasper.jar
2008-01-28  23:39            72,408 jsp-api.jar
2008-01-28  23:39            85,287 servlet-api.jar
2008-01-28  23:39           742,089 tomcat-coyote.jar
2008-01-28  23:39           197,846 tomcat-dbcp.jar
2008-01-28  23:39            18,980 tomcat-juli.jar
16 个文件      4,718,700 字节
2 个目录    895,870,976 可用字节
[/code]

tomcat-jspc.bat的内容为:
(因为jsp文件太多,消耗内存较大,因此通过 ANT_OPTS=-Xmx512m 指定内存)
[code]
set ANT_OPTS=-Xmx512m
ant -f tomcat-jspc.xml -Dwebapp.name=mywebapp
[/code]
tomcat-jspc.properties的内容为:
[code]
webapp.root=E:/lizongbo/workspace
webapp.name = webapp
[/code]

tomcat-jspc.xml的内容如下:

[code]
<project name=”Webapp Precompilation” default=”all” basedir=”.”>
<property file=”${basedir}/tomcat-jspc.properties”/>
<property name=”tomcatlib.home” value=”${basedir}/tomcat6lib”/>
<property name=”webapp.path” value=”${webapp.root}/${webapp.name}”/>
<description> JSPC Tasks</description>
<taskdef resource=”org/apache/catalina/ant/catalina.tasks”>
<classpath>
<fileset file=”${tomcatlib.home}/tomcat-juli.jar”/>
<fileset file=”${tomcatlib.home}/jasper.jar”/>
<fileset file=”${tomcatlib.home}/jasper-el.jar”/>
<fileset file=”${tomcatlib.home}/el-api.jar”/>
<fileset file=”${tomcatlib.home}/jsp-api.jar”/>
<fileset file=”${tomcatlib.home}/servlet-api.jar”/>
<fileset file=”${tomcatlib.home}/catalina-ant.jar”/>
</classpath>
</taskdef>
<taskdef resource=”org/apache/catalina/ant/jmx/jmxaccessor.tasks”>
<classpath>
<fileset file=”${tomcatlib.home}/catalina-ant.jar”/>
</classpath>
</taskdef>
<typedef
name=”jmxEquals”
classname=”org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition”>
<classpath>
<fileset file=”${tomcatlib.home}/catalina-ant.jar”/>
</classpath>
</typedef>
<typedef
name=”jmxCondition”
classname=”org.apache.catalina.ant.jmx.JMXAccessorCondition”>
<classpath>
<fileset file=”${tomcatlib.home}/catalina-ant.jar”/>
</classpath>
</typedef>
<!– 先把WEB-INF/src下的java文件编译成class –>
<target name=”compilejava”>
<echo>jdk path: ${java.home} </echo>
<echo>webapp path: ${webapp.path} </echo>

<mkdir dir=”${webapp.path}/WEB-INF/classes”/>
<mkdir dir=”${webapp.path}/WEB-INF/lib”/>

<javac destdir=”${webapp.path}/WEB-INF/classes”
optimize=”off”
debug=”on” failonerror=”false”
encoding=”utf-8″ nowarn=”true” target=”1.5″
srcdir=”${webapp.path}/WEB-INF/src”
excludes=”**/*.smap”>
<classpath>
<pathelement location=”${webapp.path}/WEB-INF/classes”/>
<fileset dir=”${webapp.path}/WEB-INF/lib”>
<include name=”*.jar”/>
</fileset>
<pathelement location=”${tomcatlib.home}”/>
<fileset dir=”${tomcatlib.home}”>
<include name=”*.jar”/>
</fileset>
</classpath>
<include name=”**” />
<exclude name=”tags/**” />
</javac>

</target>
<!– 再把jsp页面生成java文件 –>
<target name=”jspc”>
<jasper
validateXml=”false”
uriroot=”${webapp.path}”
webXmlFragment=”${webapp.path}/WEB-INF/generated_web.xml”
outputDir=”${webapp.path}/WEB-INF/jspsrc” />

</target>
<!– 再把jsp的java代码编译成class –>
<target name=”compilejspjava”>

<mkdir dir=”${webapp.path}/WEB-INF/work”/>
<mkdir dir=”${webapp.path}/WEB-INF/lib”/>

<javac destdir=”${webapp.path}/WEB-INF/work”
optimize=”off”
debug=”on” failonerror=”true”
encoding=”utf-8″ nowarn=”true” target=”1.5″
srcdir=”${webapp.path}/WEB-INF/jspsrc”
excludes=”**/*.smap”>
<classpath>
<pathelement location=”${webapp.path}/WEB-INF/classes”/>
<fileset dir=”${webapp.path}/WEB-INF/lib”>
<!– need ignore mywebapp/WEB-INF/lib/servlet.jar 在这里忽略掉webapp lib里的低版本的servlet,以免编译失败–>
<exclude name=”servlet.jar”/>
<include name=”*.jar”/>
</fileset>
<pathelement location=”${tomcatlib.home}”/>
<fileset dir=”${tomcatlib.home}”>
<include name=”*.jar”/>
</fileset>
</classpath>
<include name=”**” />
<exclude name=”tags/**” />
</javac>

</target>

<target name=”all” depends=”compilejava,jspc,compilejspjava,cleanup”>
</target>

<target name=”cleanup”>
<delete includeEmptyDirs=”true”>
<fileset dir=”${webapp.path}/WEB-INF/jspsrc”/>
<fileset dir=”${webapp.path}/WEB-INF/work”/>
</delete>
</target>

</project>
[/code]

Tags: ant, eclipse, javac, jspc, Resin, Tomcat

Related posts

Tags: , , , , ,

recent related 12 searches :

This entry was posted on 星期四, 05月 8th, 2008 at 8:23 pm and is filed under JAVA IDE, Java, Resin, Tomcat. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply