首页 文章

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

提问于
浏览
2

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

“问题”是战争在预定义的位置部署(解压缩)(比如C:\ Users \ MyPCname \ AppData \ Local \ Temp \ jetty ...),我想把它改成不同的东西,让我们说到我的项目的bin文件夹 .

这可能吗?

2 回答

  • 2

    实际上我的问题的答案变得非常简单 . jetty已经提供了所需的功能 . 我不得不在'setWar'方法上面添加这些行:

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

    here你可以看到码头文件 .

  • 1

    这在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();
      }
    }
    

相关问题