【问题标题】:How to run embedded Tomcat 9 inside Maven 3 for integration testing purposes?如何在 Maven 3 中运行嵌入式 Tomcat 9 以进行集成测试?
【发布时间】:2020-05-12 10:32:11
【问题描述】:

我正在尝试在 Maven 3 中运行嵌入式 Tomcat 9 以进行集成测试。我被其他 SO 答案引导到cargo-maven2-plugin

因此,尝试按照此处的说明进行操作:

https://codehaus-cargo.github.io/cargo/Static+deployment+of+WAR.html

我在一个简单的 POM 中有这个片段:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.7.6</version>
            <configuration>
                <container>
                    <containerId>tomcat9x</containerId>
                    <type>embedded</type>
                </container>
                <deployables>
                    <deployable>
                        <type>war</type>
                        <properties>
                            <file>path/to/myapp.war</file>
                        </properties>
                    </deployable>
                </deployables>
            </configuration>
        </plugin>
    </plugins>
</build>

我尝试使用 mvn org.codehaus.cargo:cargo-maven2-plugin:run 执行该操作

它失败并出现错误:

[INFO] [en2.ContainerRunMojo] 已解决的容器工件 org.codehaus.cargo:cargo-core-container-tomcat:jar:1.7.6 用于容器 tomcat9x [警告] 定义的可部署具有相同的 groupId 和 artifactId 作为您项目的主要工件,但类型不同。 您已经定义了一个 [war] 类型,而项目的包装是 [pom]。 这可能是一个错误,因此插件将尝试 在项目的依赖项中找到这个可部署的。

我怎样才能做到这一点?我只想在 Maven 中的嵌入式 tomcat9 中启动给定的 WAR。

【问题讨论】:

    标签: maven tomcat9 cargo maven-cargo


    【解决方案1】:

    在尝试了许多排列之后,这终于对我有用了:

            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.7.9</version>
                <configuration>
                    <container>
                        <systemProperties>
                            <myvar1>${myEnvVar}</myvar1>
                            <myvar2>... stuff ...</myvar2>
                        </systemProperties>
                        <containerId>tomcat9x</containerId>
                        <zipUrlInstaller>
                            <url>https://repo.maven.apache.org/maven2/org/apache/tomcat/tomcat/9.0.29/tomcat-9.0.29.zip</url>
                        </zipUrlInstaller>
                    </container>
                    <deployables>
                        <deployable>
                            <groupId>org.codehaus.cargo</groupId>
                            <artifactId>simple-war</artifactId>
                            <type>war</type>
                            <location>path/to/myapp.war</location>
                            <properties>
                                <context>myapp</context>
                            </properties>
                        </deployable>
                    </deployables>
                    <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>
                </configuration>
            </plugin>
    

    使用failsafe 插件自动运行startstop 之间的集成测试:

                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.21.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                            <configuration>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 1970-01-01
      • 2014-12-12
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2011-09-20
      • 2014-11-25
      相关资源
      最近更新 更多