如何通过Maven的Jetty插件运行Web工程

来源:互联网 发布:环球天下期货软件 编辑:程序博客网 时间:2024/05/16 09:27

首先建议使用jetty9,因为据官方文档显示,Jetty 7 and Jetty 8 are now EOL (End of Life),如下。但是由于项目使用的版本一般都比较低,这里以jetty8为例。

VersionServletJavaNamespaceLicensesSiteStatusJetty 9Servlet 3.1Java 1.8org.eclipse.jetty.*EPLv1 /ASLv2Eclipse.orgStable ReleaseJetty 8Servlet 3.0Java 1.6org.eclipse.jetty.*EPLv1 /ASLv2Eclipse.orgEnd of Life / Nov 2014Jetty 7Servlet 2.5Java 1.5org.eclipse.jetty.*EPLv1 /ASLv2Eclipse.orgEnd of Life / Nov 2014Jetty 6Servlet 2.5Java 1.4org.mortbay.*ASLv2Codehaus.orgEnd of Life / Nov 2010在pom.xml文件的<build></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>/</contextPath></webApp></configuration></plugin></plugins>
配置说明:
    configuration.scanIntervalSeconds 配置表示新代码的扫描时间间隔(秒),值 <= 0 表示不扫描。这里利用的是jetty 的定时重载代码的特性,做修改后不用重新启动项目,自动扫描出改动后会自动更新class文件的。
    configuration.webApp.contextPath 配置表示工程的虚拟目录名,如果配置为/,则届时访问路径为hostname:port/,如果配置为/jetty,则届时访问路径为hostname:port/jetty,有点相当于namespace的作用。

启动看效果,用Maven Build启动,需在Goals栏中配置如下:

jetty:run -Djetty.port=9080

其中9080是指定的端口,也可以在pom.xml文件中指定端口,且在pom.xml文件中指定的端口优先级要比Goals中指定的端口的优先级要高。配置如下:

<plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>8.1.16.v20140903</version><configuration><scanIntervalSeconds>5</scanIntervalSeconds><webApp><contextPath>/</contextPath></webApp><connectors><connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"><port>10000</port></connector></connectors></configuration></plugin></plugins>
此时启动的话,如果在Goals也配置了-Djetty.port=9080,则有效的访问路径还是hostname:10000/,因为在pom.xml文件中配置的端口的优先级比较高!


0 0
原创粉丝点击