关于java:Embedded Jetty:如何使用Jetty启动的.jar中包含的.war?

Embedded Jetty : how to use a .war that is included in the .jar from which Jetty starts?

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

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

我已经能够使用:

创建包含.war的.jar。

在POM.xml中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<plugin>
    maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <finalName>myApp</finalName>
        false</appendAssemblyId>
       
            <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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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>

在Jetty中引发战争的代码:

1
handler.setWar("myApp.war");

...我也尝试过:

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

...和:

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

但是没有任何效果!

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

我知道有一些变通办法可以使.war本身成为可执行文件,但我想使" .jar内的.war"起作用。

是否知道应如何配置.war?


在2009-10年度,我为一个特定的应用程序经常玩这个游戏。

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

我发现了许多解决方案:

  • 告诉码头将战争文件提取/分解到一个临时目录中

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

  • 使war文件成为可执行的war文件,例如Jenkins(Jenkins)使用的是同类型的把戏。为此,您将warty文件覆盖码头服务器类和主类。然后,您只需将码头指向jar文件本身作为war文件即可。 Jetty不在乎文件名不以.war

    结尾

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

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


    我曾尝试过像您一样在罐子里放一个战争文件,但码头似乎为我提供了一个目录,其中唯一列出了战争文件,我觉得很奇怪...不确定我是否在这里做错了什么...

    我发现,到目前为止,将jar文件内的目录中的war文件展开似乎对我来说是可行的。以后我可能会发现一些奇怪的东西,但是直到我这样做之前,我都会坚持下去。