背景
使用maven的jetty插件启动web(spring)项目时,可能会遇到项目启动很慢,甚至可能直接timeout或者报一些其他错误。我们可以根据错误来优化maven中jetty的启动速度。
常见错误一
当遇到类似如下错误:
java.lang.ArrayIndexOutOfBoundsException: 51889
或者:
java.lang.Exception: Timeout scanning annotations
解决办法
在web.xml中的web-app标签设置属性metadata-complete=”true”
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
产生原因
官方原因解释如下:
One important thing to be aware of is that the 2.5 Servlet Specification has introduced a new attribute into the
element, the metadata-complete attribute. If true, then the web container will NOT search the webapp for source code annotations, and your web.xml file must contain all resources required. If false or not specified, then jetty is required to examine all servlets, filters and listeners in the webapp for annotations. Therefore, you can save startup time by using this attribute correctly - if you don’t want to use annotations then ensure you mark metadata-complete=”true”, otherwise you will pay the penalty of the code examination.
也就是说如果不设置metadata-complete=”true”,那么jetty会检查程序中所有的annotations,而程序中spring和其他的annotations是不需要jetty来检查的。
常见错误二
出现如下提示信息:
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
解决办法
首先修改pom.xml中jetty插件的配置:
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.0.M1</version>
<configuration>
<httpConnector>
<port>8888</port>
</httpConnector>
<!-- 本地装载contextXml,来解决未配置事务或数据库造成启动时等待时间过长 -->
<contextXml>src/main/resources/jetty-deploy.xml</contextXml>
</configuration>
</plugin>
</plugins>
关键是新增了contextXml项的配置,jetty-deploy.xml具体内容如下:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- =============================================================== -->
<!-- Add a ContextProvider to the deployment manager -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- This scans the webapps directory for war files and directories -->
<!-- to deploy. -->
<!-- This configuration must be used with jetty-deploy.xml, which -->
<!-- creates the deployment manager instance -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.webapp.WebAppContext">
<Call name="setAttribute">
<Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg>
<Arg>.*/mwa-web-.*\.jar$</Arg>
<!--<Arg>.*/.*jsp-api-[^/]\.jar$|./.*jsp-[^/]\.jar$|./.*taglibs[^/]*\.jar$</Arg>-->
</Call>
</Configure>
产生原因
项目中未配置事务或数据库造成启动时等待时间过长。