使用命令行运行 Jetty War 文件

47

是否可以仅通过命令行来使用指定的war文件和上下文路径运行jetty

类似于以下命令:

java -jar $jettyHome/start.jar -Dwar.location=myApp.war -DcontextPath=/myApp OPTIONS=default,plus,jsp

1
滚草徽章,最终成为这个问题的著名问题! - gbegley
5个回答

50

使用 jetty runner

 java -jar jetty-runner.jar my.war

使用Maven,您可以通过将以下内容添加到pom.xml文件来进行安装:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.mortbay.jetty</groupId>
                                <artifactId>jetty-runner</artifactId>
                                <version>7.5.4.v20111024</version>
                                <destFileName>jetty-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

运行:

mvn package

并且使用方式如下:

java -jar target/dependency/jetty-runner.jar target/*.war

http://www.eclipse.org/jetty/documentation/current/runner.html

http://central.maven.org/maven2/org/eclipse/jetty/jetty-runner/


同样地,对于Tomcat,有一个名为webapp-runner的工具:https://github.com/jsimone/webapp-runner - ryenus
不确定为什么,但这会导致 java.lang.NoClassDefFoundError: org/apache/tomcat/util/IntrospectionUtils 错误。这是否相关? - zygimantus

7
我已经编写了一个小型的命令行应用程序/Maven原型,它的工作方式就像我最初想象的那样。这个引导程序可以通过传递WAR包路径和端口来启动您选择的servlet容器(Jetty、Tomcat、GlassFish)。
使用Maven,您可以创建和打包自己的简单应用程序实例:
mvn archetype:generate \
    -DarchetypeGroupId=org.duelengine \
    -DarchetypeArtifactId=war-bootstrap-archetype \
    -DarchetypeVersion=0.2.1

然后你可以像这样启动它:
java -jar bootstrap.jar -war myapp.war -p 8080 -c /myapp --jetty

这是实用程序和原型的源代码: https://bitbucket.org/mckamey/war-bootstrap

Bitbucket的URL已经失效或者权限被移除。 - Payam

4

从命令行安装Maven:

sudo apt install maven

在存放pom.xml的文件夹中,通过命令行运行war包:

mvn jetty:run-war

4
使用 jetty-runner-minimal
$ git clone https://github.com/kissaten/jetty-runner-minimal
$ cd jetty-runner-minimal
$ mvn package

$ wget https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war
$ java -jar target/dependency/jetty-runner.jar sample.war

2

如果您有适当的起始配置(jetty.xml)设置,那么这是可能的。

直接使用Jetty不会附带一个能够实现此功能的jetty.xml文件,但是您可以很容易地编写一个。

这意味着您需要进行以下操作之一:

  1. Have a command line that was more like

    java -jar $jettyHome/start.jar -Dwar.location=myApp.war -DcontextPath=/myApp jetty-myapp.xml
    

    or

    java -jar $jettyHome/start.jar -Dwar.location=myApp.war -DcontextPath=/myApp etc/jetty.xml etc/jetty-plus.xml jetty-deploy-app.xml
    
  2. Override the etc/jetty.xml yourself and put the info you want in there.

Jetty启动非常简单,实际上只需要生成一个XML文件以达到所需的效果。该XML文件可以从系统属性中读取值,因此您可以使用各种“-D”选项。


“将您想要的信息放入[etc/jetty.xml]”是否意味着我可以通过命令行传递系统属性来配置etc/jetty.xml,然后能够使用单个命令运行任何WAR文件,还是配置针对特定的WAR文件? - gbegley
是的,这应该是可行的。我实际上还没有尝试过,所以可能会有一些我没有考虑到的障碍,但是Jetty的XML配置格式允许您做几乎任何需要的事情,所以我看不出为什么它不会起作用。如果我有时间,我会尝试自己去尝试一下。 - Tim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接