user8681
user8681

Reputation:

Jetty monitor WAR file and update when it changes

I have an embedded Jetty running with several different contexts, one of which is a WAR file. I want Jetty to redeploy the war file when it changes (probably because it was rebuilt by another process).

My current configuration:

    ContextHandlerCollection handler = new ContextHandlerCollection();
    WebAppContext webAppContext = new WebAppContext("../../webapp/ROOT.war", "/");
    handler.setHandlers(new Handler[]{
            new WebAppContext("src/main/webapp", "/api"),
            webAppContext
    });

    Server server = new Server(8080);
    server.setHandler(handler);

How do I change it to watch and redeploy the war file (../../webapp/ROOT.war)?

Upvotes: 0

Views: 968

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

Don't use a WebAppContext directly.

Use the DeploymentManager to find and deploy your webapps.

    ContextHandlerCollection contexts = new ContextHandlerCollection();
    server.setHandler(contexts);

    DeploymentManager deployer = new DeploymentManager();
    deployer.setContexts(contexts);
    deployer.setContextAttribute(
            "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*/servlet-api-[^/]*\\.jar$");

    WebAppProvider webapp_provider = new WebAppProvider();
    // The directory to monitor for WAR + XML files
    webapp_provider.setMonitoredDirName("/opt/jetty/webapps");
    webapp_provider.setScanInterval(1); // how often to scan
    webapp_provider.setExtractWars(true);
    webapp_provider.setTempDir(new File("/opt/jetty/work"));

    deployer.addAppProvider(webapp_provider);
    server.addBean(deployer);

Upvotes: 2

Related Questions