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



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

我的问题是,我希望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>

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>

在码头发动战争的代码:

handler.setWar("myApp.war");

。。。我还尝试过:

URL res = Server.class.getResource("myApp.war");
handler.setWar(res.toExternalForm());

。。。和:

URL res = Thread.currentThread().getContextClassLoader().getSystemResource("myApp.war");
handler.setWar(res.toExternalForm());

但什么都不管用!

使用最后一个示例代码启动Jetty,服务器似乎可以正确启动,但没有任何请求工作。配置明显错误。

我知道有一些变通办法可以让.war本身执行,但我想让.jar中的">.war"起作用。

知道战争应该如何配置吗?

我在2009-10年为一个特定的应用程序玩了很多。

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

我发现了许多解决方案:

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

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

  3. 使war文件成为可执行的war文件,例如Jenkins使用的相同类型的技巧。要做到这一点,您可以用war文件覆盖jetty服务器类和主类。然后将jetty指向jar文件本身作为war文件。Jetty不在乎文件名是否以.war结尾

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

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

我试过像你一样在jar中使用war文件,但jetty似乎为我提供了一个目录列表,其中war文件是唯一的条目,我觉得这很奇怪。。。不确定我是否做错了什么。。。

到目前为止,我发现在jar文件中的一个目录中分解war文件似乎对我有效。我以后可能会发现一些奇怪的事情,但在我发现之前,我会坚持下去。

最新更新