2012-10-01 77 views
7

我想要做的是構建一個包含我的項目的可執行文件JAR。我已經包括它的依賴就在旁邊,也JAR文件,所以我的目錄列表看起來是這樣的:爲JAR文件啓動嵌入式jetty服務器

~/Projects/Java/web-app/out:

web-app.jar

dependency1.jar

dependency2.jar

dependency3.jar

我知道和相信我的問題沒有從依賴出現,我的應用程序正常運行,直到我開始嵌入Jetty的那一刻。

我用來啓動碼頭的代碼是這樣的:

public class ServerExecutionGoal implements ExecutionGoal {  

    private final static Logger logger = Logger.getLogger(ServerExecutionGoal.class);  

    private WebAppContext getWebAppContext() throws IOException {  
     WebAppContext context = new WebAppContext();  
     System.out.println("context = " + context);  
     context.setResourceBase(new PathMatchingResourcePatternResolver().getResource("classpath:/webapp").getURL().toExternalForm());  
     context.setContextPath("/");  
     context.setLogger(new StdErrLog());  
     return context;  
    }  

    @Override  
    public void execute(Map<String, Object> stringObjectMap) throws ExecutionTargetFailureException {  
     logger.info("Instantiating target server ...");  
     final Server server = new Server(8089);  
     final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();  
     try {  
      handlerCollection.setHandlers(new Handler[]{new RequestLogHandler(), getWebAppContext()});  
     } catch (IOException e) {  
      throw new ExecutionTargetFailureException("Could not create web application context", e);  
     }  
     server.setHandler(handlerCollection);  
     try {  
      logger.info("Starting server on port 8089 ...");  
      server.start();  
      server.join();  
      logger.info("Server started. Waiting for requests.");  
     } catch (Exception e) {  
      throw new ExecutionTargetFailureException("Failed to properly start the web server", e);  
     }  
    }  

    public static void main(String[] args) throws ExecutionTargetFailureException {  
     new ServerExecutionGoal().execute(null);  
    }  

}  

我可以驗證「Web應用程序」文件夾被我的JAR內正確地搬遷到/webapp,並通過我的IDE中運行代碼時( IntelliJ IDEA 11)context.setResourceBase(new PathMatchingResourcePatternResolver().getResource("classpath:/webapp").getURL().toExternalForm())有效地映射到相關資源。此外,我可以證明它可以解決類似於:

jar:file:~/Projects/Java/web-app/out/web-app.jar!/webapp 

並且可以訪問(我讀過它)。

然而,當我開始我的應用程序的主要方法,和Jetty開始,在http://localhost:8089我得到如下:

HTTP ERROR: 503

Problem accessing /. Reason:

Service Unavailable 

Powered by Jetty://

我要去哪裏錯了?

我知道通過設置「resourceBase」爲「。」地址「http://localhost:8089」將充當到位置「~/Projects/Java/web-app/out/」的接口,在那裏我可以看到所有JAR文件的列表,包括「web-app.jar」,點擊我提供的下載文件。

我已經看到了下面的問題,答不適用:

  1. Embedded jetty application not working from jar:我得到NullPointerException異常,因爲Start.class.getProtectionDomain().getCodeSource()解析爲null
  2. Embedded Jetty WebAppContext FilePermission issue:這不僅沒有回答,但情況顯然不適用,因爲我沒有權限問題(我可以得到一個文件列表應該證明這一點)。
  3. embedding jetty server problems:也沒有答案,也不適用,因爲我沒有任何依賴性問題(正如我在前面的評論中指出的那樣)。

我認爲我應該以某種方式使碼頭訪問/webapp文件夾,它位於我src/main/resources/目錄下,並捆綁到我的Web應用程序。我應該放棄一個捆綁的Web應用程序,並將爆炸的上下文路徑部署到Jetty可訪問的地方(這完全不可取,因爲它會給我和我的客戶帶來許多問題)。

+0

你可以用一個WAR文件使用context.setWar(...)。這隻會將一個文件添加到您的發行版中。 –

+0

我也面臨類似的問題。當服務器從eclipse啓動時,我得到了正確的請求響應。但是從cmd的jar文件運行時,我得到了404。任何幫助表示讚賞。 –

回答

0

我不確定,如果Jetty的WebAppContext可以使用PathMatchingResourcePatternResolver

在我的一個項目中,我通過編寫我自己的javax.servlet.Filter解決了這個問題,它通過Class#getResourceAsStream加載請求的資源。 Class#getResourceAsStream從jar文件內部以及資源路徑(例如maven)中讀取資源。

希望這個技巧是對你有幫助,歡呼聲 蒂洛

相關問題