6

組み込みの Jetty サーバーに Web アプリケーションをデプロイしようとしています。私のアプリケーションは、以下のコードを使用して Windows 環境でローカルに正常に実行されますが、Linux サーバーに JAR ファイルとしてデプロイすると、web.xml ファイルが取得されないように見えます。JAR をビルドする前に、以下の Descriptor または ResourceBase フィールドで変更する必要があるものはありますか?

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(); 
4

4 に答える 4

4

方法は次のとおりです。

まず、pom.xml で、webapp フォルダーの場所を宣言します。

 <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

pom.xml ファイルに Maven Shade プラグインを追加します。

        <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>

次に、次のように Jetty を起動します。

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

uber-jar ファイルが生成され、リソースとして宣言された webapp ディレクトリが含まれます。

jar を任意の場所または運用サーバーに移動し、次のように実行します。

$ java -jar myjettyembededwithwebxmlandhtmljspfile.jar
于 2014-09-08T11:34:54.293 に答える
2

組み込みの Jetty を次のようにデプロイします。

メインクラス

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>   

WEB_INFjar ファイルと同じフォルダーにフォルダーを作成します。次のように にコピーweb.xmlしますWEB_INF

sim/light.jar
sim/WEB-INF/web.xml
于 2013-03-07T10:16:05.033 に答える
0

WebAppContext と WebXmlConfiguration を、クラスパスでリソースを見つけることができる特定の子孫に置き換えました。

サーバー: https://github.com/jreznot/diy-remote/blob/master/server/src/org/strangeway/diyremote/server/RemoteServer.java

WebAppContex: https://github.com/jreznot/diy-remote/blob/master/server/src/org/strangeway/diyremote/server/sys/ClasspathWebAppContext.java

WebXmlConfiguration: https://github.com/jreznot/diy-remote/blob/master/server/src/org/strangeway/diyremote/server/sys/ClasspathWebXmlConfiguration.java

それは私にとってはうまくいきます

于 2013-09-30T17:05:33.320 に答える