2013-02-28 125 views
6

我试图在嵌入式Jetty服务器上部署Web应用程序。我的应用程序在下面的代码在Windows环境中本地运行良好,但是当我将它部署为Linux服务器上的JAR文件时,它看起来像我的web.xml文件没有被拾取。在构建JAR之前,是否需要在Descriptor或ResourceBase字段中更改某些内容?嵌入式Jetty服务器类路径问题

static void startJetty() { 
     try { 
      Server server = new Server(9090); 
      WebAppContext context = new WebAppContext(); 
      context.setDescriptor("/WEB-INF/web.xml");      
      context.setResourceBase("../DemoWithMultiChannels/src/"); 
      context.setContextPath("/");    
      context.setParentLoaderPriority(true); 
      server.setHandler(context); 

      System.out.println("Starting Server!");    
      server.start(); 

回答

2

部署嵌入码头如下:

主类

public static void main(String[] args) throws Exception { 
    Server server = new Server(8085);   

    WebAppContext webContext = new WebAppContext(); 
    webContext.setDescriptor("WEB-INF/web.xml"); 
    webContext.setResourceBase("src/sim/ai/server/start");  
    webContext.setServer(server); 
    webContext.setParentLoaderPriority(true); 
    server.setHandler(webContext); 

    server.start(); 
    server.join(); 
} 

的web.xml
<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
    <display-name>sim.ai.server.start</display-name> 
    <servlet> 
     <servlet-name>Jersey REST Service</servlet-name> 
     <servlet-class> 
     com.sun.jersey.spi.container.servlet.ServletContainer 
     </servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>sim.ai.server.start</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey REST Service</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

创建在同一文件夹中的jar文件一个WEB_INF夹;复制web.xmlWEB_INF,如:

sim/light.jar 
sim/WEB-INF/web.xml 
6

我有同样的问题,只是找到了解决办法:
这是工作正常,当我运行“Java的罐子......”从终端,但是当我催生它从另一个项目,web.xml没有拿起。

原因是web.xml文件的路径是错误的,它是相对于原来的项目,我最终做的是:

context.setDescriptor(Launch.class.getResource("/WEB-INF/web.xml").toString()); 

如果你不使用的资源你刚才读的常规文件你的src内文件夹,里面没有将.jar

0
+1

链接似乎被打破。 – 2014-03-21 19:46:53

+1

链接已修复 – jreznot 2014-03-24 16:50:13

4

这里是如何做到这一点。

首先,在你的pom.xml,声明在Web应用程序文件夹是:

<build> 
    <resources> 
     <resource> 
      <directory>src/main</directory> 
     </resource> 
    </resources> 

这里是我的src/main目录树:

├── java 
│   └── com 
│    └── myco 
│     └── myapp 
│      └── worker 
│       ├── App.java 
|     ... 
├── resources 
│   ├── log4j.properties 
│   └── version.properties 
└── webapp 
    ├── index.html 
    ├── index.jsp 
    ├── lib 
    │   ├── inc_meta.jsp 
    │   └── inc_navigation.jsp 
    ├── query.html 
    ├── scripts 
    │   ├── angular.min.js 
    │   └── bootstrap.min.css 
    ├── showresults.jsp 
    ├── status.jsp 
    └── WEB-INF 
     └── web.xml 

添加的Maven插件阴影在你的pom.xml文件:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>2.3</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <finalName>uber-${artifactId}-${version}/finalName>        
      </configuration>           
     </plugin> 

然后开始码头是这样的:

public static void startJetty() throws Exception { 
    logger.info("starting Jetty..."); 

    Server server = new Server(8080); 
    WebAppContext webAppContext = new WebAppContext(); 
    webAppContext.setContextPath("/"); 

    /* Important: Use getResource */ 
    String webxmlLocation = App.class.getResource("/webapp/WEB-INF/web.xml").toString(); 
    webAppContext.setDescriptor(webxmlLocation); 

    /* Important: Use getResource */ 
    String resLocation = App.class.getResource("/webapp").toString(); 
    webAppContext.setResourceBase(resLocation); 

    webAppContext.setParentLoaderPriority(true); 

    server.setHandler(webAppContext); 

    server.start(); 
    server.join(); 
} 

重要的部分是使用<YourApp>.class.getResource(<your location>),它将提供jar中文件的路径。错误的方法是这样做:webContext.setDescriptor("WEB-INF/web.xml");它给出了文件系统上的路径。

然后创建包

$mvn clean package 

产生尤伯杯jar文件,并包含被宣布为资源的webapp目录。

任何地方或在生产服务器上移动的罐子,这样运行:

$ java -jar myjettyembededwithwebxmlandhtmljspfile.jar