How to configure Jetty to reload a WebAppContext w

2019-05-20 21:52发布

问题:

I'm developing a web application and I run Jetty as the development and testing environment when I develop under Eclipse.

When I make changes to Java classes, Eclipse automatically compiles them to the build directory, but Jetty won't see the changes until I stop and start the server. I know that Jetty supports "hot deployment" using ContextDeployer that will refresh updated application contexts, but it relies on a context file in a context directory being updated - which is not very useful in my case.

Is there a way to set up Jetty so that it will reload the web app when any of the classes it uses is updated?

My current jetty.xml looks something like this:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure id="Server" class="org.eclipse.jetty.server.Server">
        <Set name="ThreadPool"><!-- bla bla --></Set>
        <Call name="addConnector"><!-- bla bla --></Call>
        <Set name="handler">
          <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <Set name="handlers">
             <Array type="org.eclipse.jetty.server.Handler">
               <Item>
                 <New id="webapp" class="org.eclipse.jetty.webapp.WebAppContext">
                   <Set name="displayName">My Web App</Set>
                   <Set name="resourceBase">src/main/webapp</Set>
                   <Set name="descriptor">src/main/webapp/WEB-INF/web.xml</Set>
                   <Set name="contextPath">/mywebapp</Set>
                 </New>
               </Item>
               <Item>
                 <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
               </Item>
             </Array>
            </Set>
          </New>
        </Set>
    </Configure>

回答1:

We have not found a way of doing this (aside from implementing our own version of the org.eclipse.jetty.deploy.providers.WebAppProvider).

We have configured jetty to hot deploy webapps from the webapps folder (property monitoredDirName of the WebappDeployer).

Then to hot deploy, I recreate my link in this folder to the src/main/webapp folder of my Eclipse project. The linked must be suffixed .war.

Not really automatic but good enough and avoids a Jetty restart.

If you go the route of re-implementing a WebappDeployer, I would not monitor the changes in .class files - they change too much when compiled by Eclipse, particularly in the case of automatic builds. I would implement a 'Tomcat like' solution by monitoring changes to the web.xml file. Then a dummy change saved to this file from Eclipse would trigger a redeployment.



回答2:

It is also possible to configure your jetty app with maven and starting periodical builds with Jenkins (even every couple of seconds, depending on the maschine you are working on)