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

问题描述 投票:5回答:2

我正在尝试生成一个包含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>

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>

在Jetty中设置战争的代码:

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

知道如何配置.war吗?

java maven jetty embedded-jetty
2个回答
4
投票

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

我发现的问题是,当你在jar中嵌入一个war时,URI最终会成为那些无法使用试图访问war文件的默认jar: uri处理程序的URI。

我找到了许多解决方案:

  1. 告诉jetty将war文件解压缩/爆炸到临时目录中
  2. 实现自己的URI处理程序(这是一个有趣的练习,但如果你需要自己支持代码,我不建议这样做)
  3. 使war文件成为可执行的war文件,例如詹金斯使用的相同类型的技巧。为此,您可以使用war文件覆盖jetty服务器类和主类。然后你只需将jetty指向jar文件本身作为war文件。 Jetty并不关心文件名不会以.war结尾

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

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


0
投票

我已经尝试过像jar中的war文件,但jetty似乎为我提供了一个目录列表,其中war文件是唯一的条目,我觉得很奇怪...不确定我是否在那里做错了...

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

© www.soinside.com 2019 - 2024. All rights reserved.