Maven running jetty on integration-test stage

馋奶兔 提交于 2019-12-13 13:35:47

问题


I use failsafe plugin.

So when I type mvn failsafe:integration-test it stars my integration tests (which is great).

But I want my jetty server starts on pre-integration stage then. What should I do?

(I don't want launch mvn verify since it involves whole cycle running, but mvn failsafe:integration-test- it seems it's supposed to work that way)

There are two plugins:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>                                                              <!-- for starting jetty for integration tests -->
    <version>2.16</version>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>verify</id>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <!--<jettyConfig>${project.basedir}/src/main/resources/config/jetty9.xml</jettyConfig>-->
        <stopKey>STOP</stopKey>
        <stopPort>9999</stopPort>
        <stopWait>5</stopWait>
        <scanIntervalSeconds>5</scanIntervalSeconds>
        <scanTargets>
            <scanTarget>${project.basedir}/src/main</scanTarget>
            <scanTarget>${project.basedir}/src/test</scanTarget>
        </scanTargets>
        <contextXml>${project.basedir}/src/test/resources/jetty-context.xml</contextXml>
        <webAppConfig>
            <contextPath>/${project.artifactId}-${project.version}</contextPath>
        </webAppConfig>
    </configuration>

    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>                                                         <!-- In the pre-integration-test phase the Jetty server will be started -->
            <goals>
                <goal>run-exploded</goal>
            </goals>
            <configuration>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>                                                        <!-- in the "post-integration-phase" it will be stopped -->
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

回答1:


I prefer starting jetty on the fly programmatically inside the test cases. Main reasons for this is:

  • the test becomes self contained and not dependent on maven configuration
  • it is possible to run the test as is in any IDE



回答2:


This is the jetty and maven-failsafe-plugin usage manual:

Maven Failsafe Plugin – Usage

It provides a sample configuration for integrating Jetty into the integration test life-cycle.

Jetty is started during the pre-integration-test phase and stopped during the vpost-integration-test phase.

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.16</version>
    <executions>
      <execution>
        <id>start-jetty</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <scanIntervalSeconds>0</scanIntervalSeconds>
          <daemon>true</daemon>
        </configuration>
      </execution>
      <execution>
        <id>stop-jetty</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>stop</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

However, it also specifically recommends that you use the verify phase:

The recommendation is that you do not directly invoke the pre-integration-test, integration-test or post-integration-test phases but that instead you run integration tests by specifying the verify phase. [...]

This allows you to set-up your integration test environment during the pre-integration-test phase, run your integration tests during the integration-test phase, cleanly tear-down your integration test environment during the post-integration-test phase before finally checking the integration test results and failing the build if necessary.



来源:https://stackoverflow.com/questions/25127982/maven-running-jetty-on-integration-test-stage

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