【问题标题】:Jetty embedded server - change the path where the war file will be deployedJetty 嵌入式服务器 - 更改将部署 war 文件的路径
【发布时间】:2015-07-02 10:40:30
【问题描述】:

我正在使用 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 文件夹。

这可能吗?

【问题讨论】:

    标签: java jetty embedded-jetty jetty-9


    【解决方案1】:

    实际上,我的问题的答案非常简单。 jetty 已经提供了所需的功能。我只需要在“setWar”方法上方添加这些行:

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

    here你可以看码头文档。

    【讨论】:

      【解决方案2】:

      这在码头文档中有很好的描述。请看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();
        }
      }
      

      【讨论】:

      • 您好@Eduard,很抱歉延迟回复。不幸的是,我已经尝试了上面的代码,但我没有看到任何区别。此外,我注意到我们正在定义我们将在其中查找 war 文件的路径,但我们没有设置将部署(解压缩)war 文件的路径,这正是我正在寻找的。​​span>
      • 上面代码的 cmets 声明了这一点:“配置可用,从配置到支持 webapp 中的注释扫描到 选择 webapp 将自己解包的位置”。所以这就是我想要实现的功能......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 2018-07-25
      • 2012-08-10
      • 1970-01-01
      • 2018-03-15
      • 2017-04-07
      相关资源
      最近更新 更多