Maven deploy multiple wars to embedded server for integration tests

笑着哭i 提交于 2019-12-30 06:12:25

问题


I have had no issue running a maven war project on an embedded server for its own integration tests, but now I need to run multiple wars and test from a different project.

I would like to setup the following scenario...

I have two Maven war projects in my local workspace called War1 and War2. I would like to have a 3rd Maven project, WarIntegration, that contains only integration tests and does the following:

  1. Packages War1
  2. Packages War2
  3. Starts an embedded server
  4. Deploys both wars to same embedded server
  5. Runs integration tests contained within WarIntegration (which will make http calls to War1 and War2)
  6. Stops embedded server

Is this possible? What plugin setup will achieve this? What kind of project should WarIntergration be (packaging)? Should War1 and War2 be modules in WarIntegration or dependencies? Can all of the configuration be aded to the WarIntegration project or would it have to be spread across the projects?

This is similar to this question, except we must use an embedded server that is started and stopped by the project (probably when we run verify) and we need a separate project for integration tests: I have a multi-module Maven 2 POM that has two WARs, how can I configure it to deploy both wars prior to running tests?


回答1:


I was able to achieve this using the cargo-maven2-plugin.

Here are the relevant pieces of the pom for anyone who is interested...

...
<groupId>com.test</groupId>
<artifactId>webapp-integration</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
...
<dependencies>
        ...
        <dependency>
            <artifactId>webapp1</artifactId>
            <groupId>com.test</groupId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>webapp2</groupId>
            <artifactId>com.test</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.2.2</version>
                <configuration>
                    <container>
                        <containerId>jetty6x</containerId>
                        <type>embedded</type>
                    </container>
                    <configuration>
                        <type>standalone</type>
                        <properties>
                            <cargo.servlet.port>8085</cargo.servlet.port>
                        </properties>
                        <deployables>
                            <deployable>
                                <artifactId>webapp1</artifactId>
                                <groupId>com.test</groupId>
                                <type>war</type>
                                <pingURL>http://localhost:8085/testapp/</pingURL>
                                <properties>
                                    <context>testapp</context>
                                </properties>
                            </deployable>
                            <deployable>
                                <artifactId>webapp2</artifactId>
                                <groupId>com.test</groupId>
                                <type>war</type>
                                <pingURL>http://localhost:8085/testapp2/</pingURL>
                                <properties>
                                    <context>testapp2</context>
                                </properties>
                            </deployable>
                        </deployables>
                    </configuration>
                </configuration>
                <executions>
                    <execution>
                        <id>start-server</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-server</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.12</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.12</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <groups>com.test.integration.IntegrationTestMarker</groups>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/*.class</include>
                            </includes>
                            <skipTests>false</skipTests>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



回答2:


Watch out, the DEPLOYABLES element is a child of plugin/configuration, NOT plugin/configuration/configuration.

The example above should be :

<plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.2.2</version>
            <configuration>
                <container>...</container>
                <configuration>
                    <type>standalone</type>
                    <properties>
                        <cargo.servlet.port>8085</cargo.servlet.port>
                    </properties>
                </configuration>
                <deployables>
                     <deployable>
                            <artifactId>webapp1</artifactId>
                            <groupId>com.test</groupId>
                            <type>war</type>
                            <pingURL>http://localhost:8085/testapp/</pingURL>
                            <properties>
                                <context>testapp</context>
                            </properties>
                      </deployable>
                      <deployable>
                            <artifactId>webapp2</artifactId>
                            <groupId>com.test</groupId>
                            <type>war</type>
                            <pingURL>http://localhost:8085/testapp2/</pingURL>
                            <properties>
                                <context>testapp2</context>
                            </properties>
                      </deployable>
                 </deployables>
             </configuration>
        </plugin>
 </plugins>

Hope that helps !



来源:https://stackoverflow.com/questions/11142287/maven-deploy-multiple-wars-to-embedded-server-for-integration-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!