摘要: 通过 Maven 的 Tomcat 插件来运行 Web 工程,每次启动Tomcat速度很慢,开发起来不爽。听说 Jetty 可以动态加载修改后的类, 为了能够更快提高开发效率, 绝对要尝试一下。
Jetty7/8 的Maven插件配置:http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin
Jetty9 最新的Maven插件配置:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html 或者 最新RELEASE版配置
Jetty 各个版本的限制:http://wiki.eclipse.org/Jetty/Starting/Jetty_Version_Comparison_Table
Jetty 7 必须 Jdk 1.5+,Servlet 2.5,类似于 Tomcat 6,文章编写时的 Release 版本:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.6.17.v20150415</version>
</plugin>
Jetty 8 必须 Jdk 1.6+,Servlet 3.0,类似于 Tomcat 7,文章编写时的 Release 版本:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
</plugin>
Jetty 9 必须 Jdk 1.7+,Servlet 3.0+,类似于 Tomcat 8,文章编写时的 Release 版本:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.6.v20141205</version>
<configuration>
<jettyConfig>${project.basedir}/src/main/resources/config/jetty.xml,${project.basedir}/src/main/resources/config/jetty-ssl.xml,${project.basedir}/src/main/resources/config/jetty-https.xml</jettyConfig>
<stopKey>STOP</stopKey>
<stopPort>9999</stopPort>
<stopWait>5</stopWait>
<scanIntervalSeconds>5</scanIntervalSeconds>
<httpConnector>
<port>8090</port>
</httpConnector>
<scanTargets>
<scanTarget>${project.basedir}/src/main</scanTarget>
<scanTarget>${project.basedir}/src/test</scanTarget>
</scanTargets>
<webAppConfig>
<contextPath>/${project.artifactId}</contextPath>
</webAppConfig>
</configuration>
</plugin>
国内很多应用也许会去升级JDK版本,但往往还在使用Servlet2.5做开发,所以本文使用 Jetty 8 作为案例。
单独使用HTTP
步骤一
在项目的 pom.xml 里面添加如下配置:
<project>
....
<build>
....
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<webApp>
<contextPath>/test</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
....
</build>
....
</project>
配置说明:
configuration.scanIntervalSeconds 配置表示新代码的扫描时间间隔(秒),值 <= 0 表示不扫描
configuration.webApp.contextPath 配置表示工程的虚拟目录名
混合使用HTTP和HTTPS
步骤一
pom.xml 里面的配置改为如下:
<project>
....
<build>
....
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<webApp>
<contextPath>/test</contextPath>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
</connector>
<connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
<port>9443</port>
<password>xxxx</password>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
....
</build>
....
</project>
说明,从配置中可以看出,添加了connectors配置,一个是http的配置,一个是https的配置。\
connector.port 是端口号,这个端口号必须设置,且优先级比 -Djetty.port 高。
connector.password 是 .keystore 的密码,https 连接必须设置
说明:配置jetty+ssl请参考其他文档 http://zhangwei8607.iteye.com/blog/2205127
idea配置Jetty9
步骤1:
commond line 添加配置:
org.mortbay.jetty:maven-jetty-plugin:xx.xx.xx:run(jetty9的版本)
如图
1、
2、
pom文件中加入
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
</plugins>
3、修改端口
Runner选项->VM Options 设置 -Djetty.port=8081
总结
Jetty 的定时重载代码的实现比 Tomcat 的好很多,使用 Tomcat 的 onreload 经常修改几下就整个应用在重启,速度慢,而且经常排队,非常耗时。
Jetty 的配置对 pom.xml 的侵入比较大,尤其是 http 和 https 的混合配置更是需要在 pom.xml 中配置固定的端口号,让代码看起来不够干净。
补充
Jetty有一个问题,默认使用nio连接会把资源文件(html, css)锁定,解决方案是把nio改为bio:
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
</connector>
<connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
<port>9443</port>
<password>changeit</password>
</connector>
<connector implementation="org.eclipse.jetty.server.bio.SocketConnector">
<port>8080</port>
</connector>
<connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
<port>8443</port>
<password>xxxx</password>
</connector>