在 Maven 多模块项目的测试阶段之前启动 Jetty



我在一个多模块Maven项目(假设foo-web)中有一个WAR模块,它实现了Web服务。然后我有一个foo-cli,它实现了一个Web服务客户端并在几个单元测试中对其进行了测试。

为了使它工作,我在测试阶段之前以这种方式启动 Jetty:

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

当我从 foo-cli 模块中运行"mvn test"时,这非常有效(它甚至会自动停止,无需指定任何其他内容)。但是,当我尝试进入上层 (foo) 并从那里发出"mvn test"时,即我尝试为项目中的所有模块运行所有测试,它失败并显示"404 - 未找到"。从输出中,我可以看到覆盖(战争依赖)似乎被完全忽略了。

提前感谢任何帮助。

您应该尝试将集成测试移动到顶级项目。这样,它将在构建 WAR 工件后运行。

你看过Maven故障安全插件吗?它是为你正在做的事情而设计的,这实际上是一个集成测试,而不是一个单元测试。该页面提供了一些很好的建议,说明为什么您可能希望使用integration-test阶段进行集成测试。

也就是说,它描述了为什么你可能想要在pre-integration-test期间做start-jetty等等,以便你可以适当地将其全部拆除。

最新更新