Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

java - Possible to run two webapps at once when developing with Maven/Eclipse?

Here's the problem: we build webapps for clients. We also have an "admin" webapp that modifies some client data structures. Because of the nature of the data, both webapps have to run in the same JVM.

This is no problem in production; you just put two webapps in the same app server.

We've recently switched to a Mavenish way of laying out webapps, though, and Maven wants one webapp per project. In Eclipse it's a problem, because if you run the different webapps independently, they'll be in separate JVMs.

We're trying to use the jetty-maven-plugin to do webapp testing, but could switch to something else if it would solve this problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you can :) It's done by identifying one WAR module as the primary, copying all the other WARs into the primary's target dir, making a jetty.xml and telling Maven Jetty Plugin to use the jetty.xml. Here's how to copy the other WARs using Maven dependency plugin:

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

You'll need to have the com.foo:bar dependency defined in the POM as well. Here's the contents of jetty.xml:

<?xml version="1.0"?>

<!-- =========================================================== -->
<!-- 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>

And here's how to tell Maven Jetty plugin to use the new jetty.xml:

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
    <jettyConfig>${basedir}/jetty.xml</jettyConfig>
</configuration>

Now kick off jetty:run-war goal from Eclipse and you should see all WARs deploying in one Maven Jetty plugin instance. I run this from command line and it works there, YMMV with Eclipse.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...