使用Maven/Eclipse进行开发时,可以同时运行两个webapp?

ccl*_*eve 4 java web-applications maven maven-jetty-plugin

问题在于:我们为客户构建webapps.我们还有一个"管理员"webapp,可以修改一些客户端数据结构.由于数据的性质,两个webapp都必须在同一个JVM中运行.

这在生产中没有问题; 你只需将两个webapp放在同一个app服务器上.

我们最近改用Mavenish的方式来布局webapps,Maven想要每个项目使用一个webapp.在Eclipse中,这是一个问题,因为如果您独立运行不同的Web应用程序,它们将位于不同的JVM中.

我们正在尝试使用jetty-maven-plugin进行webapp测试,但如果可以解决这个问题,可以切换到别的东西.

Tim*_*mer 5

是的,你可以:)通过将一个WAR模块识别为主模块,将所有其他WAR复制到主目标目录,制作jetty.xml并告诉Maven Jetty插件使用jetty.xml来完成.以下是使用Maven依赖插件复制其他WAR的方法:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>copy</id>
        <phase>package</phase>
        <goals>
            <goal>copy</goal>
        </goals>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>com.foo</groupId>
                    <artifactId>bar</artifactId>
                    <version>${project.version}</version>
                    <type>war</type>
                    <overWrite>true</overWrite>
                    <outputDirectory>target/</outputDirectory>
                </artifactItem>
            </artifactItems>
        </configuration>
    </execution>
</executions>
Run Code Online (Sandbox Code Playgroud)

您还需要在POM中定义com.foo:bar依赖项.这是jetty.xml的内容:

<?xml version="1.0"?>
Run Code Online (Sandbox Code Playgroud)

<!-- =========================================================== -->
<!-- Set handler Collection Structure                            -->
<!-- =========================================================== -->
<Set name="handler">
    <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
            <Array type="org.eclipse.jetty.server.Handler">
                <Item>
                    <New class="org.eclipse.jetty.server.handler.ContextHandlerCollection"
                         id="Contexts">
                        <Set name="handlers">
                            <Array type="org.eclipse.jetty.server.Handler">
                                <Item>
                                    <New id="FooWebHandler"
                                         class="org.eclipse.jetty.webapp.WebAppContext"/>
                                </Item>
                            </Array>
                        </Set>
                    </New>
                </Item>
            </Array>
        </Set>
    </New>
</Set>

<Ref id="FooWebHandler">
    <Set name="contextPath">/foo</Set>
    <Set name="war">
        target/bar-${project.version}.war
    </Set>
</Ref>
Run Code Online (Sandbox Code Playgroud)

以下是如何告诉Maven Jetty插件使用新的jetty.xml:

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
    <jettyConfig>${basedir}/jetty.xml</jettyConfig>
</configuration>
Run Code Online (Sandbox Code Playgroud)

现在启动jetty:来自Eclipse的run-war目标,你应该看到所有WAR部署在一个Maven Jetty插件实例中.我从命令行运行它,它在那里工作,YMMV与Eclipse.


For*_*e_7 5

您可以直接通过jetty-maven-plugin执行此操作,而无需修改jetty.xml:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <configuration>
        <scanIntervalSeconds>5</scanIntervalSeconds>
        <webApp>
            <contextPath>/</contextPath>
        </webApp>
       <contextHandlers>
          <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
            <war>${project.basedir}/../secondProject.war</war>
            <contextPath>/cc</contextPath>
          </contextHandler>
        </contextHandlers>  
    </configuration>
    <executions>
        ...
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

您根本无需修改jetty.xml文件即可成功使用此文件,也无需复制war文件.(虽然您可能希望在此构建中构建第二次战争,请参阅maven-invoker-plugin)

文档:http://www.eclipse.org/jetty/documentation/9.2.2.v20140723/jetty-maven-plugin.html#running-more-than-one-webapp