Search code examples
javamavenjettyoverlays

Starting Jetty before test phase in a Maven multi-module project


I have a WAR Module in a multi-module Maven project (let's say foo-web), which realises a web-service. Then I have a foo-cli, which implements a web service client and tests it in a couple of unit tests.

In order to make this working, I start Jetty before the test phase this way:

<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>foo-web</artifactId>
  <version>${project.version}</version>
  <type>war</type>
  <scope>test</scope>
</dependency>         
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
   <configuration>

     <scanIntervalSeconds>10</scanIntervalSeconds>
     <useTestScope>true</useTestScope>
     <connectors>
       <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
         <port>8080</port>
         <maxIdleTime>60000</maxIdleTime>
       </connector>
     </connectors>
   </configuration>

<executions>
    <execution>
      <id>start-jetty</id>
      <phase>process-test-classes</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <scanIntervalSeconds>0</scanIntervalSeconds>
        <daemon>true</daemon>
      </configuration>
    </execution>
   </executions>          
</plugin>

This works perfectly well while I run 'mvn test' from within the foo-cli module (it even stops automatically, with no need to specify anything else). However, when I attempt to go to the upper level (foo) and issue 'mvn test' from there, i.e., I try to run all the tests for all the modules in the project, it fails with '404 - not found'. From the output, I can see that the overlay (the war dependency) seems to be totally ignored.

Thanks in advance for any help.


Solution

  • You should try moving your integration test to the top level project. This way it will run after the WAR artifact has been built.

    Have you had a look at the Maven Failsafe Plugin? It's designed for the sort of thing you're doing, which is actually an integration test and not a unit test. The page offers some good advice on why you might want to use the integration-test phase for your integration testing.

    Namely, it describes why you might want to do start-jetty during pre-integration-test and so on, so that you can tear it all down appropriately.