5

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

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

可执行 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 吗?

4

2 回答 2

4

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

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

我找到了许多解决方案:

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

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

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

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

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

于 2012-11-17T20:46:51.623 回答
0

我已经像你一样尝试在 jar 中使用 war 文件,但码头似乎为我提供了一个目录列表,其中包含 war 文件作为唯一的条目,我觉得这很奇怪......不确定我是否在那里做错了什么......

我发现到目前为止,在 jar 文件内的目录中分解 war 文件似乎对我有用。我以后可能会发现一些奇怪的东西,但在我这样做之前,我会坚持下去。

于 2017-12-19T06:39:20.873 回答