嵌入式Jetty:如何使用包含在Jetty启动的.jar中的.war? [英] Embedded Jetty : how to use a .war that is included in the .jar from which Jetty starts?

查看:116
本文介绍了嵌入式Jetty:如何使用包含在Jetty启动的.jar中的.war?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to generate a .jar containing a main() that would start Jetty.

我的问题是我d。喜欢.war,Jetty加载 包含在同一个.jar 中。

My problem is that I'd like the .war that Jetty loads to be included in the same .jar.

我去过能够创建包含.war的.jar:

I've been able to create the .jar containing the .war with :

在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,服务器似乎正确启动但没有请求工作。配置显然是错误的。

Using that last example code to start Jetty, the server seems to start correctly but no requests work. The configuration is obviously wrong.

我知道有一些变通方法可以使.war本身可执行,但是我想在内部创建 .war .jar 工作。

I know there are some workarounds to make a .war itself executable, but I'd like to make the ".war inside the .jar" work.

知道如何配置.war吗?

Any idea how the .war should be configured?

推荐答案

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

I played with this a lot in 2009-10 for a specific app.

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

The problem I found is that when you have a war embedded within a jar, the URIs can end up being ones that do not work with the default jar: uri handler trying to access the war file.

我找到了很多解决方案:

There are a number of solutions that I found:


  1. 告诉码头提取/爆炸将war文件放入临时目录

  1. Tell jetty to extract/explode the war file into a temporary directory

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

Implement your own URI handler (that was an interesting exercise, but if you need to support the code yourself, I would not recommend doing that)

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

Make the war file an executable war file, e.g. the same type of trick used by Jenkins. To do this you overlay the jetty server classes and your main class with the war file. Then you just point jetty to the jar file itself as the war file. Jetty does not care that the filename does not end in .war

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

All three worked for me on Jetty 7. I suspect that the situation will remain the same.

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

In the end I opted for option 3. It works out as the simplest, does not leave temporary files on the users system, and is quickest to start up.

这篇关于嵌入式Jetty:如何使用包含在Jetty启动的.jar中的.war?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆