Jetty 嵌入式服务器 - 更改部署 war 文件的路径

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

我正在使用 Jetty 9.2 在嵌入式 Jetty 服务器中 运行 一个 war 文件。我没有 'web.xml',没有 webapp 文件夹,只有我要部署的 war 文件。我正在 运行 编译 war 文件,此代码没有任何问题:

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");
}
}

'problem' 是 war 部署(解压)在预定义的位置(类似于 C:\Users\MyPCname\AppData\Local\Temp\jetty...),我想将其更改为某些内容不一样,比方说我项目的bin文件夹。

这可能吗?

jetty 文档中对此进行了很好的描述。请看一下 http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html.

public class OneWebApp
   {
     public static void main( String[] args ) throws Exception
      {
    // Create a basic jetty server object that will listen on port 8080.
    // Note that if you set this to port 0 then a randomly available port
    // will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);

    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(
            ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);

    // The WebAppContext is the entity that controls the environment in
    // which a web application lives and breathes. In this example the
    // context path is being set to "/" so it is suitable for serving root
    // context requests and then we see it setting the location of the war.
    // A whole host of other configurations are available, ranging from
    // configuring to support annotation scanning in the webapp (through
    // PlusConfiguration) to choosing where the webapp will unpack itself.
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    File warFile = new File(
            "C:\Users\MyPCname\AppData\Local\Temp\jetty\your.war");
    webapp.setWar(warFile.getAbsolutePath());
    webapp.addAliasCheck(new AllowSymLinkAliasChecker());

    // A WebAppContext is a ContextHandler as well so it needs to be set to
    // the server so it is aware of where to send the appropriate requests.
    server.setHandler(webapp);

    // Start things up! 
    server.start();

    // The use of server.join() the will make the current thread join and
    // wait until the server is done executing.
    // See            http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
    server.join();
  }
}

其实我的问题的答案很简单。 jetty 已经提供了所需的功能。我不得不在 'setWar' 方法上方添加这些行:

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

here你可以看到码头文档。