关于java: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文件,此代码没有任何问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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文件夹说。

这可能吗?


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

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

在这里您可以看到码头文件。


在码头文件中对此进行了很好的描述。请查看http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
  }
}