52

java -jar myWarFile.war実行した WAR ファイルに含まれる webapp をホストする Jetty Web サーバーを起動する「実行可能な」war ファイル ( ) を作成しようとしています。

探しているものの作り方を説明したページを見つけました。

ただし、そのアドバイスに従って、実行可能な jar (戦争) を作成することになっていると思う方法は機能しません。

次のようなマニフェストで WAR ファイルを作成する Ant タスクがあります。

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 1.5.0_18-b02 (Sun Microsystems Inc.)
Main-Class: Start

WAR ファイルの内容は次のようになります。

> Start.class
> jsp
>   build.jsp 
> META-INF  
>   MANIFEST.MF
> WEB-INF
>   lib
>     jetty-6.1.22.jar
>     jetty-util.6.1.22.jar

WAR ファイルを実行しようとすると、次のエラーが表示されます。

Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/jetty/Handler
Caused by: java.lang.ClassNotFoundException: org.mortbay.jetty.Handler
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Start. Program will exit.

ここには 2 つのエラーがあるようです。1 つは JAR ファイルが見つからないように見えるエラーで、もう 1 つはStartクラスが見つからないエラーです。

最初のものを修正するために、Jetty JAR ファイルを WAR ファイルのベースに置き、再試行しましたが、同じエラーが発生しました。また、マニフェストWEB-INF/lib/<specific-JAR-files>の属性に を追加してみました。Class-Pathそれもうまくいきませんでした。

私が正しい/間違っていることと、この実行可能なWARファイルを起動して実行する方法について、誰かが洞察を持っていますか?

4

9 に答える 9

50

質問にあるリンクは、必要なもののほとんどを提供します。ただし、それに加えて実行する必要があることがいくつかあります。

Jetty の起動に必要なクラス ファイルは、パッケージ化するときに war ファイルのルートに配置する必要があります。Ant を利用して<war>、ファイルを作成する前にそれを行うことができます。war のマニフェスト ファイルにはMain-Class、サーバーを実行するための属性も必要です。

手順は次のとおりです。

Jetty サーバー クラスを作成します。

これは、提供されたリンクから適応されます。

package com.mycompany.myapp;

import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public final class EmbeddedJettyServer
{
    public static void main(String[] args) throws Exception
    {
        int port = Integer.parseInt(System.getProperty("port", "8080"));
        Server server = new Server(port);

        ProtectionDomain domain = EmbeddedJettyServer.class.getProtectionDomain();
        URL location = domain.getCodeSource().getLocation();

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
        webapp.setServer(server);
        webapp.setWar(location.toExternalForm());

        // (Optional) Set the directory the war will extract to.
        // If not set, java.io.tmpdir will be used, which can cause problems
        // if the temp directory gets cleaned periodically.
        // Your build scripts should remove this directory between deployments
        webapp.setTempDirectory(new File("/path/to/webapp-directory"));

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

ここで設定できるすべての内容を確認するには、Jetty API ドキュメントをご覧ください。

Ant で戦争を構築する:

これは、ステージング ディレクトリを使用して必要なクラス ファイルを war のルートに展開し、war の実行時にアクセスできるようにします。

<target name="war" description="--> Creates self-executing war">
  <property name="staging.dir" location="${basedir}/staging"/>
  <property name="webapp.dir" location="${basedir}/src/webapp"/>

  <mkdir dir="${staging.dir}"/>

  <!-- assumes you have all of your war content (excluding classes and libraries) already structured in a directory called src/webapp -->
  <!-- e.g. -->
  <!-- src/webapp/index.html -->
  <!-- src/webapp/WEB-INF/web.xml -->
  <!-- src/webapp/WEB-INF/classes/my.properties -->
  <!-- etc ... -->
  <copy todir="${staging.dir}">
    <fileset dir="${webapp.dir}" includes="**/*"/>
  </copy>

  <unjar dest="${staging.dir}">
    <!-- you'll have to locate these jars or appropriate versions; note that these include JSP support -->
    <!-- you might find some of them in the downloaded Jetty .tgz -->
    <fileset dir="path/to/jetty/jars">
      <include name="ant-1.6.5.jar"/>
      <include name="core-3.1.1.jar"/>
      <include name="jetty-6.1.24.jar"/>
      <include name="jsp-2.1-glassfish-2.1.v20091210.jar"/><!-- your JSP implementation may vary -->
      <include name="jsp-api-2.1-glassfish-2.1.v20091210.jar"/><!-- your JSP implementation may vary -->
      <include name="servlet-api-2.5-20081211.jar"/><!-- your Servlet API implementation may vary -->
    </fileset>
    <patternset><!-- to exclude some of the stuff we don't really need -->
      <exclude name="META-INF/**/*"/>
      <exclude name="images/**/*"/>
      <exclude name=".options"/>
      <exclude name="about.html"/>
      <exclude name="jdtCompilerAdapter.jar"/>
      <exclude name="plugin*"/>
    </patternset>
  </unjar>

  <!-- copy in the class file built from the above EmbeddedJettyServer.java -->
  <copy todir="${staging.dir}">
    <fileset dir="path/to/classes/dir" includes="com/mycompany/myapp/EmbeddedJettyServer.class"/>
  </copy>

  <war destfile="myapp.war" webxml="${webapp.dir}/WEB-INF/web.xml">
    <fileset dir="${staging.dir}" includes="**/*"/>
    <classes dir="path/to/classes/dir"/><!-- your application classes -->
    <lib dir="path/to/lib/dir"/><!-- application dependency jars -->
    <manifest>
      <!-- add the Main-Class attribute that will execute our server class -->
      <attribute name="Main-Class" value="com.mycompany.myapp.EmbeddedJettyServer"/>
    </manifest>
  </war>

  <delete dir="${staging.dir}"/>
</target>

戦争を実行する:

上記のすべてが適切に設定されている場合、次のことができるはずです。

java -jar myapp.war

// or if you want to configure the port (since we are using the System property in the code)

java -Dport=8443 -jar myapp.war
于 2010-07-09T19:24:02.013 に答える
45

これは、@RobHruska の回答の Maven への適応です。これは、メイン クラスのファイルをコピーし、Jetty JAR ファイルを WAR ファイルにマージするだけで、新しいことは何もありません。私のように Maven を初めて使用する場合の作業を簡素化するためです。

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>move-main-class</id>
            <phase>compile</phase>
            <configuration>
                <tasks>
                    <copy todir="${project.build.directory}/${project.build.finalName}">
                        <fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/">
                            <include name="main/*.class" />
                        </fileset>
                    </copy>

                    <unjar dest="${project.build.directory}/${project.build.finalName}">
                        <!-- you'll have to locate these jars or appropriate versions; note that these include JSP support -->
                        <!-- you might find some of them in the downloaded Jetty .tgz -->
                        <fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/lib/">
                            <include name="ant-1.6.5.jar"/>
                            <!--<include name="core-3.1.1.jar"/>-->
                            <include name="jetty*"/>
                            <include name="servlet-api*"/>
                        </fileset>

                        <patternset><!-- to exclude some of the stuff we don't really need -->
                            <exclude name="META-INF/**/*"/>
                            <exclude name="images/**/*"/>
                            <exclude name=".options"/>
                            <exclude name="about.html"/>
                            <exclude name="jdtCompilerAdapter.jar"/>
                            <exclude name="plugin*"/>
                        </patternset>
                    </unjar>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin> 
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <archiveClasses>true</archiveClasses>
        <archive>
            <manifest>
                <mainClass>main.Main</mainClass> 
            </manifest>
        </archive>
    </configuration>
</plugin>
于 2012-05-01T14:38:21.237 に答える
14

jetty-console-maven-pluginを使用してこれを理解しました。

mvn packageを実行するたびに、java-jarで使用できる別のwarが作成されますwhateverpackage-runnable.war

        <plugin>
            <groupId>org.simplericity.jettyconsole</groupId>
            <artifactId>jetty-console-maven-plugin</artifactId>
            <version>1.45</version>
            <executions>
                <execution>
                    <goals>
                        <goal>createconsole</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <additionalDependencies>
                    <additionalDependency>
                        <artifactId>jetty-console-requestlog-plugin</artifactId>
                    </additionalDependency>
                    <additionalDependency>
                        <artifactId>jetty-console-gzip-plugin</artifactId>
                    </additionalDependency>
                    <additionalDependency>
                        <artifactId>jetty-console-ajp-plugin</artifactId>
                    </additionalDependency>
                    <additionalDependency>
                        <artifactId>jetty-console-startstop-plugin</artifactId>
                    </additionalDependency>
                </additionalDependencies>
            </configuration>
        </plugin>

また、init.dスクリプトとすべてを生成します。

于 2012-03-21T05:38:27.937 に答える
7

Hudson は、このユース ケースを直接サポートする Winstone サーブレット コンテナーを使用して、まさにこの問題を解決します。 http://winstone.sourceforge.net/#embedding

おそらくこれはあなたのために働くでしょうか?

于 2010-03-17T12:30:46.417 に答える
6

これはちょっと古いですが、Jetty 8 の別の代替手段は、単に Jetty jar を依存関係として pom に含め、pom に以下を追加することです (war をアンパッケージして再パッケージ化する ant スクリプトに対して):

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>JettyStandaloneMain</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- The main class needs to be in the root of the war in order to be 
            runnable -->
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>move-main-class</id>
                    <phase>compile</phase>
                    <configuration>
                        <tasks>
                            <move todir="${project.build.directory}/${project.build.finalName}">
                                <fileset dir="${project.build.directory}/classes/">
                                    <include name="JettyStandaloneMain.class" />
                                </fileset>
                            </move>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
于 2011-07-01T14:53:57.350 に答える
3

「mavenなし」では、「mvn jetty:run」ではなく、それ自体で実行できるjarが必要であると思います--mavenをまったく使用したくないというわけではありません。

多くのオプションを見つけたので、これを理解するのに長い時間がかかりました.どれも簡単ではありませんでした. 最終的に、この maven プラグインを simplericity から見つけました。それは素晴らしく機能します。

于 2011-11-02T02:04:56.617 に答える
1

これは私の例の ANT 抽出物です。アイデアは、Jetty の依存関係をアンパッケージしてから、通常の JAR ファイルのようにローカルに含めることです。

<!-- Hack: Java doesn't support jars within jars/wars -->
<unjar src="${lib.dir}/container/jetty.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/jetty-util.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/servlet-api.jar" dest="${build.dir}/unjar"/>
<unjar src="${lib.dir}/container/jsp-api.jar" dest="${build.dir}/unjar"/>

<!-- Build war file as normal, just including the compiled and unjar'ed files -->
<war destfile="${war.file}" webxml="${config.dir}/web.xml">
    <fileset dir="${build.dir}/classes"/>
    <fileset dir="${build.dir}/unjar"/>
    <fileset dir="${resources.dir}" excludes="*.swp"/>
    <lib dir="${lib.dir}/runtime"/>
    <manifest>
        <attribute name="Main-Class" value="Start"/>
    </manifest>
</war>

ノート:

WEB-INF/libディレクトリは、Web アプリケーションの依存関係用です。この場合、起動時に通常の Jetty JAR ファイルのように機能するように、WAR ファイルをパッケージ化しています。

于 2010-03-22T20:43:28.680 に答える
0

以前も同様のことをしたことがありますが、アプリを「java -jar xxx.war」として起動していますか? あなたは2つの瓶しか持っていませんが、それでは十分ではないと思います. また、Jetty 7.0.0M1 (最新バージョン) を使用してみてください。jetty-server と jetty-webapp を 2 つの依存関係 (org.eclipse.jetty からのもの) として追加すると、次の jar が lib ディレクトリに作成されます。参考までに、org.mortbay.jetty.Handler は jetty-server*.jar にありました。

  • jetty-continuation-7.0.0.M1.jar
  • jetty-http-7.0.0.M1.jar
  • jetty-io-7.0.0.M1.jar
  • jetty-security-7.0.0.M1.jar
  • jetty-server-7.0.0.M1.jar
  • jetty-servlet-7.0.0.M1.jar
  • jetty-util-7.0.0.M1.jar
  • jetty-webapp-7.0.0.M1.jar
  • jetty-xml-7.0.0.M1.jar
于 2010-03-17T12:27:19.700 に答える
0
  • .war ファイルのルート内に .jar を配置しても何も起こりません
  • 内部に .jar を入れWEB-INF/libても、JVM が Jetty ファイルを見つけて .war の起動を開始するのに役立ちません。それらをそこに置くには「遅すぎます」。
  • マニフェスト Class-Path に .jar を配置すると、.jar に含まれるファイルではなく、外部の .jar ファイルに対してのみ機能します。

じゃあ何をすればいいの?

  • ビルド スクリプトを使用して、必要なすべての .jar ファイルを .war ファイルに単純にマージします。これには少し余分な作業が必要です。また、コンパイルされたコードが .war 内の提供可能なファイルの一部であるという点で、少し醜いです。
  • 「java -cp jetty.jar:... ...」を使用して、依存する .jar を JVM のクラスパスに追加します。
于 2010-03-17T01:23:31.370 に答える