2012-05-24 31 views
0

當我在eclipse中使用Tomcat,並讓我的web應用程序通過eclipse運行時,任何對Java類或JSP頁面的更改似乎都被直接推入進入tomcat而不必重新啓動tomcat或應用程序。當你在編輯你的代碼時,你如何告訴eclipse實時更新Jetty代碼

有沒有一種方法可以在eclipse下配置Jetty,使其以相同的方式工作?到目前爲止,我似乎只能做代碼更改,然後手動重新啓動jetty。

回答

0

將jetty.xml配置爲使用啓用「熱部署」的ContextDeployer。這可能已經被默認配置。本質上,Jetty會按照預先確定的時間間隔掃描上下文文件夾。如果它檢測到WAR文件或war目錄已更改,則會自動重新加載上下文。

在下面的示例中,將$ {jetty.home}/contexts文件夾配置爲查找指示Jetty要監視哪些Web應用程序的上下文XML文件。 scanInterval設置爲5毫秒,這意味着Jetty每5毫秒檢查一次更改。你會發現這裏面配置的jetty.xml:

<!-- =========================================================== --> 
<!-- Configure the context deployer        --> 
<!-- A context deployer will deploy contexts described in  --> 
<!-- configuration files discovered in a directory.    --> 
<!-- The configuration directory can be scanned for hot   --> 
<!-- deployments at the configured scanInterval.     --> 
<!--                --> 
<!-- This deployer is configured to deploy contexts configured --> 
<!-- in the $JETTY_HOME/contexts directory      --> 
<!--                --> 
<!-- =========================================================== --> 
<Call name="addLifeCycle"> 
    <Arg> 
    <New class="org.mortbay.jetty.deployer.ContextDeployer"> 
     <!-- the ContextHandlerCollection to modify once a webapp is added or removed (Allows Hot Deployment) --> 
     <Set name="contexts"><Ref id="Contexts"/></Set> 

     <!-- the directory which will contain your context.xml files --> 
     <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set> 

     <!-- the interval in milliseconds to periodically scan the configurationDir --> 
     <Set name="scanInterval">5</Set> 
    </New> 
    </Arg> 
</Call> 

您還需要創建一個WebAppContext項。將此放在一個叫做的test.xml文件,並將該文件放在/上下文目錄內:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> 
<Configure class="org.mortbay.jetty.webapp.WebAppContext"> 
    <Set name="contextPath">/test</Set> 
    <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test</Set> 
</Configure> 

注意<Set name="war">指的是實際的文件夾,不是WAR文件。該文件夾應包含應用程序的根目錄,例如/ WEB-INF文件夾和所有其他文件。

如果您希望即時識別JSP和其他頁面,則需要將webapps文件夾中的Web應用程序配置爲從分解的WAR運行,而不是WAR文件本身。之後,您需要將Eclipse指向這個位置,以便您可以直接從webapps/test文件夾修改和編譯文件。

總之,你不清楚你運行的是哪個Jetty版本。但是,儘管Jetty配置可能會因版本而異,但應該讓您開始。有關Jetty配置和故障排除的更多文檔,請參閱Jetty WebsiteWebtide website

一旦一切配置正確,您可以直接從該文件夾修改和編譯代碼,而無需重新啓動Jetty。

該規則的唯一例外情況是web.xml被修改的情況。如果你修改web.xml,你很可能需要重新啓動Jetty。

最後要說明的是,除非您先禁用了WAR文件的爆炸,否則請確保您不在webapps文件夾中部署任何名爲test.war的WAR文件。您最終可能會覆蓋您的代碼!

如果您需要其他指導,請參閱Jetty Documentation on ContextDeployers以及Deploying a Webapp to Jetty。祝你好運!

相關問題