1. 程式人生 > 其它 >日常問題-使用maven jetty外掛啟動慢的一些解決方法

日常問題-使用maven jetty外掛啟動慢的一些解決方法

背景

開發工具-idea

使用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 theelement, 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>

產生原因

專案中未配置事務或資料庫造成啟動時等待時間過長。

參考:關於使用maven jetty外掛啟動慢的解決方法 | Zacard's Notes