Search code examples
javajettyembedded-jettyjetty-9

Jetty embedded server - change the path where the war file will be deployed


I am using Jetty 9.2 to run a war file inside the embedded Jetty server. I have no 'web.xml', no webapp folder, just the war file i want to deploy. I am running the war file without any problems with this code:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class DeployWar {

public static void main(String[] args) {

    Server server = new Server(9090);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");

    webapp.setWar("test.war");
    server.setHandler(webapp);

    try {
        server.start();
        System.out.println("Press any key to stop the server...");
        System.in.read(); System.in.read();
        server.stop();
    } catch (Exception ex) {
        System.out.println("error");
    }

    System.out.println("Server stopped");
}
}

The 'problem' is that the war is deployed(unpacked) in a predefined location (something like C:\Users\MyPCname\AppData\Local\Temp\jetty...) and i want to change it to something different, let's say to the bin folder of my project.

Is this possible?


Solution

  • Actually the answer to my problem turned out to be pretty simple. The desired functionality is already provided by jetty. I had to just add these lines above the 'setWar' method:

    File webappsFolder = new File("jettyWebapps/");
    webappsFolder.mkdirs();
    webapp.setTempDirectory(webappsFolder);
    

    here you can see the jetty documentation.