嵌入式Jetty:如何使用Jetty启动的.jar中包含的.war?

ele*_*ype 5 java jetty embedded-jetty maven

我正在尝试生成一个包含main()的.jar ,它将启动Jetty.

我的问题是我希望Jetty加载的.war 包含在同一个.jar中.

我已经能够创建包含.war的.jar:

在POM.xml中:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <finalName>myApp</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <mainClass>com.myApp.Server</mainClass>
            </manifest>
        </archive>
        <descriptors>
            <descriptor>src/main/resources/executable-jar-assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-my-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

executable-jar-assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

<id>jar-with-dependencies-and-war</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>false</useProjectArtifact>
        <unpack>true</unpack>
        <scope>runtime</scope>
    </dependencySet>
</dependencySets>
<fileSets>
    <fileSet>
        <directory>target/classes/</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>target/</directory>
        <includes>
            <include>
                myApp.war
            </include>
        </includes>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>
Run Code Online (Sandbox Code Playgroud)

在Jetty中设置战争的代码:

handler.setWar("myApp.war");
Run Code Online (Sandbox Code Playgroud)

......我也尝试过:

URL res = Server.class.getResource("myApp.war");
handler.setWar(res.toExternalForm());
Run Code Online (Sandbox Code Playgroud)

......和:

URL res = Thread.currentThread().getContextClassLoader().getSystemResource("myApp.war");
handler.setWar(res.toExternalForm());
Run Code Online (Sandbox Code Playgroud)

但没有任何作用!

使用最后一个示例代码启动Jetty,服务器似乎正确启动但没有请求工作.配置显然是错误的.

我知道有一些变通方法可以使.war本身可执行,但我想在.jar中创建" .war ".

知道如何配置.war吗?

Ste*_*lly 4

我在 2009 年 10 月的一个特定应用程序中经常使用它。

我发现的问题是,当您在 jar 中嵌入 war 时,URI 最终可能无法与尝试jar:访问 war 文件的默认 uri 处理程序一起使用。

我发现了很多解决方案:

  1. 告诉jetty将war文件提取/分解到临时目录中

  2. 实现您自己的 URI 处理程序(这是一个有趣的练习,但如果您需要自己支持代码,我不建议这样做)

  3. 使 war 文件成为可执行的 war 文件,例如 Jenkins 使用的相同类型的技巧。为此,您需要使用 war 文件覆盖 jetty 服务器类和主类。然后你只需将jetty指向jar文件本身作为war文件。Jetty 不关心文件名是否以以下结尾.war

这三个人都在 Jetty 7 上为我工作。我怀疑情况会保持不变。

最后我选择了选项 3。它是最简单的,不会在用户系统上留下临时文件,并且启动速度最快。

  • -1 表示缺乏细节。这几乎比没有帮助更糟糕。 (3认同)