为什么我使用maven jetty debug+jetty开发Web感觉忒不方便

用Maven构建Java Web开发环境(Jetty容器)之二 - Stop here ! - ITeye技术网站
博客分类:
&&& 本文接上一篇继续来介绍。
&&& 目前为止我们还是手工命令行方式执行程序的,没有和IDE结合,其实Maven天生就对Eclipse做了集成,我们使用mvn eclipse:eclipse就得到了一个Eclipse的项目结构,在Eclipse中使用import功能就能直接导入到IDE中了。我们来看一下这个过程:
&&& 此时的demo就是Eclipse项目格式的了,出现了.project和.classpath文件。我们在Eclipse中引入这个项目,此时的Eclipse没有安装Maven插件,不能自动运行Maven命令,我们来安装Maven的Eclipse插件M2E。
&&& 在Eclipse的Install New Software中直接选择安装即可,非常简单。下面我们来创建Web项目并导入Eclipse中,在Jetty容器中运行程序。首先执行mvn archetype:generate命令创建。
&&& 可以看到,刚创建的web项目结构包含了resources目录,而没有java代码目录,我们需要手工创建,在Eclipse中创建source folder,路径为src/main/java/src,现在我们得到如下一个项目结构,新建一个Servlet用于测试。
&&& 此时,项目中没有Servlet的依赖,需要添加,我们使用m2eclipse插件来直接添加依赖,如下所示:
&&& 相应的XML为:
&dependency&
&groupId&javax.servlet&/groupId&
&artifactId&servlet-api&/artifactId&
&version&2.5&/version&
&type&jar&/type&
&scope&compile&/scope&
&/dependency&
&&& 下面就可以编写Servlet了,很简单,就输出HelloWorld吧。
package org.ourpioneer.
import java.io.IOE
import java.io.PrintW
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String title="Webapp Demo";
out.println("&!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"&");
out.println("&html xmlns=\"http://www.w3.org/1999/xhtml\"&");
out.println("&head&");
out.println("&meta http-equiv=\"Content-Type\" content=\"text/charset=utf-8\" /&");
out.println("&title&" + title + "&/title&");
out.println("&body&");
out.println("&h1&Hello World!&/h1&");
out.println("&/body&");
out.println("&/html&");
&&& 然后不能忘了在web.xml中配置这个Servlet,这里是Servlet 2.5的规范,不是Servlet 3,不能用注解。这也很简单。
&?xml version="1.0" encoding="UTF-8"?&
&web-app version="2.5" xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd"&
&display-name&Archetype Created Web Application&/display-name&
&servlet-name&helloworld&/servlet-name&
&servlet-class&org.ourpioneer.servlets.HelloWorldServlet&/servlet-class&
&/servlet&
&servlet-mapping&
&servlet-name&helloworld&/servlet-name&
&url-pattern&/helloworld&/url-pattern&
&/servlet-mapping&
&/web-app&
&&& 程序都有了,剩下就是运行了,Maven既然天生和Jetty是一对儿,这里我们就使用Jetty吧,在Maven中配置Jetty,首先是webdefault.xml要准备好,它是配置Jetty的,这个可以从Jetty的包中找到,并复制到resources下,这里多说一点,默认Jetty运行时是锁定JS/CSS等静态文件的,如果想在Jetty运行时也能修改它们,要在webdefault.xml中修改如下设置:
&init-param&
&param-name&useFileMappedBuffer&/param-name&
&param-value&false&/param-value&
&/init-param&
&&& Jetty也准备了,运行命令是jetty:run,这要在Maven中设置,那么需要在pom.xml中加入Jetty的插件的设置信息。这里直接贴出其整体构建信息。
&finalName&webapp&/finalName&
&sourceDirectory&src/main/java/src&/sourceDirectory&
&testSourceDirectory&src/test&/testSourceDirectory&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-compiler-plugin&/artifactId&
&version&2.0.2&/version&
&configuration&
&source&1.6&/source&
&target&1.6&/target&
&encoding&utf-8&/encoding&
&/configuration&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-resources-plugin&/artifactId&
&configuration&
&encoding&UTF-8&/encoding&
&/configuration&
&groupId&org.mortbay.jetty&/groupId&
&artifactId&jetty-maven-plugin&/artifactId&
&version&7.1.6.v&/version&
&configuration&
&stopKey&stop&/stopKey&
&stopPort&5599&/stopPort&
&webAppConfig&
&contextPath&/&/contextPath&
&defaultsDescriptor&src/main/resources/webdefault.xml&/defaultsDescriptor&
&/webAppConfig&
&scanIntervalSeconds&0&/scanIntervalSeconds&
&connectors&
&connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"&
&port&80&/port&
&maxIdleTime&60000&/maxIdleTime&
&/connector&
&/connectors&
&/configuration&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-eclipse-plugin&/artifactId&
&version&2.7&/version&
&configuration&
&addVersionToProjectName&false&/addVersionToProjectName&
&useProjectReferences&false&/useProjectReferences&
&encoding&UTF-8&/encoding&
&wtpmanifest&false&/wtpmanifest&
&wtpapplicationxml&true&/wtpapplicationxml&
&wtpversion&1.5&/wtpversion&
&additionalBuildcommands&
&buildcommand&org.eclipse.jdt.core.javabuilder&/buildcommand&
&buildcommand&org.mon.project.facet.core.builder&/buildcommand&
&buildcommand&org.eclipse.wst.validation.validationbuilder&/buildcommand&
&/additionalBuildcommands&
&additionalProjectnatures&
&nature&org.springframework.ide.eclipse.core.springnature&/nature&
&nature&org.maven.ide.eclipse.maven2Nature&/nature&
&nature&org.mon.project.facet.core.nature&/nature&
&nature&org.eclipse.jdt.core.javanature&/nature&
&nature&org.mon.modulecore.ModuleCoreNature&/nature&
&/additionalProjectnatures&
&classpathContainers&
&classpathContainer&org.eclipse.jdt.launching.JRE_CONTAINER&/classpathContainer&
&/classpathContainers&
&/configuration&
&groupId&org.apache.maven.plugins&/groupId&
&artifactId&maven-war-plugin&/artifactId&
&version&2.1-beta-1&/version&
&configuration&
&warName&webapp&/warName&
&/configuration&
&/plugins&
&&& 此时,更新一下Maven依赖,它们就都自动下载到本地了,到这个过程结束,我们就可以在Eclipse中配置Debug运行了。配置很简单,如下。
&&& 这是Debug模式运行,Run模式下是一样的,用Debug模式可以在Eclipse中断点运行程序,非常便于调试。下面我们就让它跑起来吧。运行命令是jetty:run,Base directory配置是:${workspace_loc:/应用名},启动调试,看到如下信息,Jetty就成功启动了。
&&& 这里我们使用了80端口,配置方式在pom.xml中,上面的代码已经体现了。在浏览器中访问地址如下:http://localhost/helloworld,之后,我们就看到了效果。
&&& 本文系作者本人的实践和探索,希望对使用者有用,欢迎交流。
(全篇完)
浏览 58201
jetty 怎么加进来的阿?
通过插件方式
觉得 用Maven构建Java Web开发环境(Jetty容器)之一 这篇文章,跟着操作步骤走还行得通
但,用Maven构建Java Web开发环境(Jetty容器)之二 这篇文章,就对不上号了
应该像一那样,以怎么创建一个war工程(wabapp) 详细描述,开始还以为是demo工程呢,弄了半天,才看到图片里的maven有 webapp on jetty 和 webdemo on jetty
另外
&& m2eclipse插件也没详细说明怎么安装成功,我是花了一天时间才装上这个插件,网上找得资料都不全,而且按说明也没装成功,最后还是自己摸索出来的
安装经验已放上我的blog
&& http://blog.csdn.net/sghys/archive//5957420.aspx
您好,这可能是由于我们的机器环境有差异,您的问题在我这没有出现,这可能是特例。至于图片里的项目,有我自己使用的,也有用于演示的,没有特别说明罢了,但这不影响使用吧。
写得还行,适合入门级。
对,就是入门用的
写的很好,我们用的MyEclipse8.5不需要添加eclipse插件
MyEclipse太大了,而且破解版最好不要商用。
浏览: 1344584 次
来自: 大连
浏览量:122421
浏览量:296665
浏览量:136348
浏览量:63717
可以参考最新的文档:如何在eclipse jee中检出项目并转 ...
spring mvc demo教程源代码下载,地址:http: ...
看帖回复是美德,楼主讲的很清晰明了,看了豁然开朗.
iris_1992 写道2005年以前,国外开原报表完全碾压国 ...
不错,不错trackbacks-0
Eclipse Luna
pom.xml 配置
在你的 pom.xml 文件中添加 jetty 插件的描述信息():
&&&plugins&
&&&&&plugin&
&&&&&&&groupId&org.eclipse.jetty&/groupId&
&&&&&&&artifactId&jetty-maven-plugin&/artifactId&
&&&&&&&version&9.2.8.v&/version&
&&&&&/plugin&
&&&/plugins&
启动 & 停止
命令行方式启动 jetty mvn jetty:run,可以通过 Ctrl + C 停止 jetty 服务。
或者,在 eclipse 中选中项目 --& 右键 --& Run As --& Maven build...,在 Goals 栏输入 jetty:run(与命令行方式相比,仅仅是
少了 mvn 前缀,为方便起见,以下均以命令行方式介绍。)
jetty 9 部署的项目的 Context path 默认是 /,也就是说,项目的访问入口地址是:(不带项目名)
如果你希望通过命令 mvn jetty:stop 执行关闭 jetty 服务,你需要像下面一样在你的 pom.xml 配置文件中添加一个特殊的端口和控制键:
&configuration&
&&&stopKey&shutdown&/stopKey&
&&&stopPort&9966&/stopPort&
&/configuration&
你仍可以通过 mvn jetty:run 启动 jetty 服务,可以通过 mvn jetty:stop&来停止 jetty 服务。
取消文件映射缓存
jetty 默认开启了 useFileMappedBuffer,在 jetty 运行期间,页面所使用的静态文件(如 css 文件等)不允许修改。如果你尝试去修改它
们,保存的时候就会出现 Save could not be completed.
解决办法,找到 %repo%/org/eclipse/jetty/jetty-webapp/9.2.8.v/jetty-webapp-9.2.8.v.jar(%repo% 表示你
本地的 maven 仓库的目录,另外,将 9.2.8.v 换成你所使用的版本)。用压缩工具打开它, 找到 jetty-webapp-9.2.8.v2015021
7.jar/org/eclipse/jetty/webapp/webdefault.xml,将 webdefault.xml 文件解压缩一份出来,用文本编辑器打开它,搜索找到
useFileMappedBuffer 配置的行,将 true 改成 false 以禁掉缓存。
&init-param&
&&&param-name&useFileMappedBuffer&/param-name&
&&&param-value&false&/param-value&
&/init-param&
先确认 jetty 服务已经停止,将原文件&jetty-webapp-9.2.8.v.jar/org/eclipse/jetty/webapp/webdefault.xml 删除,将刚
才那份修改好的 webdefault.xml 文件重新压缩进去即可。
jetty 默认使用的端口是 8080,命令行的方式修改端口的命令是:mvn -Djetty.port=8081 jetty:run&。pom.xml 配置方式如下:
&configuration&
&&&httpConnector&
&&&&&port&8081&/port&
&&&/httpConnector&
&/configuration&
自动热部署
在你的 pom.xml 中添加如下配置:
&configuration&
&&&scanIntervalSeconds&2&/scanIntervalSeconds&
&/configuration&
默认值是 0。大于 0 的数值表示开启,0 表示关闭,单位为秒。以配置数值为一个周期,自动的扫描文件检查其内容是否有变化,如果发现文件的
内容被改变,则自动重新部署运用。命令行的方式:mvn -Djetty.scanIntervalSeconds=2 jetty:run&。
手动重加载
在你的 pom.xml 文件中添加如下配置,reload 的可选值 :[automatic|manual]
&configuration&
&&&reload&manual&/reload&
&/configuration&
默认值为 automatic,它与大于 0 的 scanIntervalSeconds 节点一起作用,实现自动热部署的工作。设为 manual 的好处是,当你改变文件
内容并保存时,不会马上触发自动扫描和重部署的动作,你还可以继续的修改,直至你在 Console 或命令行中敲回车键(Enter)的时候才触发重
新加载的动作。这样可以更加的方便调试修改。命令行的方式是:mvn -Djetty.reload=manual jetty:run 。
在你的 pom.xml 文件添加如下配置:
&configuration&
&&&requestLog&implementation="org.eclipse.jetty.server.NCSARequestLog"&
&&&&&filename&target/access-yyyy_mm_dd.log&/filename&
&&&&&filenameDateFormat&yyyy_MM_dd&/filenameDateFormat&
&&&&&logDateFormat&yyyy-MM-dd&HH:mm:ss&/logDateFormat&
&&&&&logTimeZone&GMT+8:00&/logTimeZone&
&&&&&append&true&/append&
&&&&&logServer&true&/logServer&
&&&&&retainDays&120&/retainDays&
&&&&&logCookies&true&/logCookies&
&&&/requestLog&
&/configuration&
的一个实现类。
org.eclipse.jetty.server.NCSARequestLog 是一种伪标准的 NCSA 日志格式。下面是一些节点参数的解释:
filename:日志文件的名称
filenameDateFormat:日志文件的名称的日期格式,它要求日志文件名必须含有 yyyy_mm_dd 串
logDateFormat:日志内容的时间格式
logTimeZone:时区
append:追加到日志
logServer:记录访问的主机名
retainDays:日志文件保存的天数, 超过删除
logCookies:记录 cookies
启动 jetty 服务,在项目的 target 目录下会生成一个 access-.log 文件,该文件中的其中一条记录如下:
localhost&0:0:0:0:0:0:0:1&-&-&[&01:17:05]&"GET&/css/main.css&HTTP/1.1"&304&-&
"http://localhost:8081/"&
"Mozilla/5.0&(Windows&NT&6.3;&WOW64)&AppleWebKit/537.36&(KHTML,&like&Gecko)&
Chrome/35.0.&Safari/537.36&SE&2.X&MetaSr&1.0"&"JSESSIONID=2gyikovul2izafo4f"
在你的 pom.xml 文件添加如下配置:
&configuration&
&&&dumpOnStart&true&/dumpOnStart&
&/configuration&
dumpOnStart 默认值为 false,如果设为 true,jetty 在启动时会把当前服务进程的内存信息输出到控制台中,但这并不会保存到文件中。
最常用的是 contextPath,它的配置如下:
&configuration&
&&&webApp&
&&&&&contextPath&/${project.artifactId}&/contextPath&
&&&/webApp&
&/configuration&
contextPath 的默认值的 /,${project.artifactId} 引用了 &artifactId& 节点的值,即项目的名称。
项目的静态资源文件目录默认是 src/main/webapp,如果静态资源目录有多个,或者不在默认的 src/main/webapp 目录下,可做如下配置:
&configuration&
&&&webApp&
&&&&&contextPath&/${project.artifactId}&/contextPath&
&&&&&resourceBases&
&&&&&&&resourceBase&${project.basedir}/src/main/webapp&/resourceBase&
&&&&&&&resourceBase&${project.basedir}/commons&/resourceBase&
&&&&&/resourceBases&
&&&/webApp&
&/configuration&
引用静态资源文件时,路径不包含资源目录的名称,如 commons/main.css,引用方式为:&link href="main.css" rel="stylesheet" /&&
更多参数信息可参考
完整的配置
附 pom.xml 文件中 jetty 插件的完整配置片段:
&&&plugins&
&&&&&plugin&
&&&&&&&groupId&org.eclipse.jetty&/groupId&
&&&&&&&artifactId&jetty-maven-plugin&/artifactId&
&&&&&&&version&9.2.8.v&/version&
&&&&&&&configuration&
&&&&&&&&&httpConnector&
&&&&&&&&&&&port&8081&/port&
&&&&&&&&&/httpConnector&
&&&&&&&&&stopKey&shutdown&/stopKey&
&&&&&&&&&stopPort&9966&/stopPort&
&&&&&&&&&!--
&&&&&&&&&scanIntervalSeconds&2&/scanIntervalSeconds&
&&&&&&&&--&
&&&&&&&&&reload&manual&/reload&
&&&&&&&&&dumpOnStart&true&/dumpOnStart&
&&&&&&&&&webApp&
&&&&&&&&&&&contextPath&/${project.artifactId}&/contextPath&
&&&&&&&&&&&!--
&&&&&&&&&&&resourceBases&
&&&&&&&&&&&&&resourceBase&${project.basedir}/src/main/webapp&/resourceBase&
&&&&&&&&&&&&&resourceBase&${project.basedir}/commons&/resourceBase&
&&&&&&&&&&&/resourceBases&
&&&&&&&&&&--&
&&&&&&&&&/webApp&
&&&&&&&&&requestLog&implementation="org.eclipse.jetty.server.NCSARequestLog"&
&&&&&&&&&&&filename&target/access-yyyy_mm_dd.log&/filename&
&&&&&&&&&&&filenameDateFormat&yyyy_MM_dd&/filenameDateFormat&
&&&&&&&&&&&logDateFormat&yyyy-MM-dd&HH:mm:ss&/logDateFormat&
&&&&&&&&&&&logTimeZone&GMT+8:00&/logTimeZone&
&&&&&&&&&&&append&true&/append&
&&&&&&&&&&&logServer&true&/logServer&
&&&&&&&&&&&retainDays&120&/retainDays&
&&&&&&&&&&&logCookies&true&/logCookies&
&&&&&&&&&/requestLog&
&&&&&&&/configuration&
&&&&&/plugin&
&&&/plugins&
更多有关 jetty 的配置信息可参考
阅读(15511)
&re: maven jetty plugin
写的不错哦&&&&&&
&re: maven jetty plugin
+1,博主加油!&&&&&&
&re: maven jetty 插件使用
内容格式都很清晰,希望博主提供jetty更深入一点的介绍和应用(优点、为什么在maven中使用这个插件、和tomcat插件比怎么样、和CARGO怎么联系)&&&&&&
&re: maven jetty 插件使用
博文写的很详细,非常棒!!&&&&&&
&re: maven jetty 插件使用
赞,楼主写得真棒&&&&&&
随笔分类(8)
随笔档案(104)
积分与排名
阅读排行榜为什么我使用maven+jetty开发Web感觉忒不方便_百度知道
为什么我使用maven+jetty开发Web感觉忒不方便
提问者采纳
可以使用更新的)jspc maven-war-plugin ${basedir}&#47.web ${basedir}&#47.16 jspc jspc ${basedir}/target&#47.jetty maven-jetty-jspc-plugin 6.1;web ${basedir}/WEB-INF/web ${basedir}/web/web添加插件(里面的版本
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁二次元同好交流新大陆
扫码下载App
汇聚2000万达人的兴趣社区下载即送20张免费照片冲印
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
爱自由,反独裁,反专制,倡导言论自由.新闻自由.拒绝洗脑.独立思想者,坚信思想是个好东西,它会要了独裁的命...........
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1166)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'eclipse中利用jetty进行Maven web项目开发调试',
blogAbstract:'配置好jetty后,开发起来就爽多了。具体配置如下:& & & &一、配置pom.xml文件,添加如下内容:& & & &&plugin&& & & & & & & & &groupId&org.mortbay.jetty&/groupId&& & & & & & & & &artifactId&maven-jetty-plugin&/artifactId&',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:2,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'爱自由,反独裁,反专制,倡导言论自由.新闻自由.拒绝洗脑.独立思想者,坚信思想是个好东西,它会要了独裁的命...........',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}}

我要回帖

更多关于 maven jetty 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信