2014-02-11 107 views
8

我有maven配置文件集測試在哪裏在預集成測試maven啓動兩個碼頭服務器,然後開始測試。我偶然發現的問題是在服務器中,測試開始時它們並未完全加載。看起來問題是通過在測試中添加5秒的睡眠時間來解決的,但我希望將它添加到maven中並從測試中移除。無論如何,我可以做到這一點?請查看下面的代碼示例以獲取更多信息是否有任何Maven睡眠功能?

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.7</version> 
      <executions> 
       <execution> 
        <id>copy</id> 
        <phase>compile</phase> 
        <goals> 
         <goal>copy</goal> 
        </goals> 
        <configuration> 
         <artifactItems> 
          <artifactItem> 
           <groupId>com.work.projectname</groupId> 
           <artifactId>projectname-web</artifactId> 
           <version>${project.version}</version> 
           <type>war</type> 
           <destFileName>projectname-${project.version}.war</destFileName> 
          </artifactItem> 
          <artifactItem> 
           <groupId>com.work.projectname</groupId> 
           <artifactId>projectname-stubs-web</artifactId> 
           <version>${project.version}</version> 
           <type>war</type> 
           <destFileName>projectname-stubs-${project.version}.war</destFileName> 
          </artifactItem> 
         </artifactItems> 
         <outputDirectory>${project.build.directory}</outputDirectory> 
         <overWriteReleases>false</overWriteReleases> 
         <overWriteSnapshots>true</overWriteSnapshots> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.16</version> 
      <configuration> 
       <skipTests>true</skipTests> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.mortbay.jetty</groupId> 
      <artifactId>jetty-maven-plugin</artifactId> 
      <version>${jetty-server.version}</version> 
      <executions> 
       <execution> 
        <id>start-projectname</id> 
        <phase>pre-integration-test</phase> 
        <goals> 
         <goal>deploy-war</goal> 
        </goals> 
        <configuration> 
         <daemon>true</daemon> 
         <reload>manual</reload> 
         <war>${project.build.directory}/projectname-${project.version}.war</war> 
         <webAppXml>${basedir}/src/jetty/jetty-loginService.xml</webAppXml> 
         <webAppConfig> 
          <contextPath>/projectname</contextPath> 
          <parentLoaderPriority>true</parentLoaderPriority> 
          <tempDirectory>${project.build.directory}/tmp-projectname</tempDirectory> 
         </webAppConfig> 
         <stopPort>8006</stopPort> 
         <stopKey>STOP</stopKey> 
        </configuration> 
       </execution> 
       <execution> 
        <id>start-projectname-stubs</id> 
        <phase>pre-integration-test</phase> 
        <goals> 
         <goal>deploy-war</goal> 
        </goals> 
        <configuration> 
         <daemon>true</daemon> 
         <reload>manual</reload> 
         <war>${project.build.directory}/projectname-stubs-${project.version}.war</war> 
         <connectors> 
          <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> 
           <port>${stub-jetty-server.port}</port> 
           <maxIdleTime>300000</maxIdleTime> 
          </connector> 
         </connectors> 
         <stopPort>8008</stopPort> 
         <stopKey>STOP</stopKey> 
        </configuration> 
       </execution> 
       <execution> 
        <id>stop-projectname</id> 
        <phase>post-integration-test</phase> 
        <goals> 
         <goal>stop</goal> 
        </goals> 
        <configuration> 
         <stopPort>8006</stopPort> 
         <stopKey>STOP</stopKey> 
        </configuration> 
       </execution> 
       <execution> 
        <id>stop-projectname-stubs</id> 
        <phase>post-integration-test</phase> 
        <goals> 
         <goal>stop</goal> 
        </goals> 
        <configuration> 
         <stopPort>8008</stopPort> 
         <stopKey>STOP</stopKey> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-failsafe-plugin</artifactId> 
      <version>2.16</version> 
      <configuration> 
       <parallel>classes</parallel> 
       <threadCountClasses>33</threadCountClasses> 
       <includes> 
        <include>**/*Test.java</include> 
       </includes> 
      </configuration> 
      <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> 
    </plugins> 
</build> 

回答

13

您可以在maven antrun插件的幫助下完成此操作。類似這樣的:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <configuration> 
     <tasks> 
      <sleep seconds="5" /> 
     </tasks> 
    </configuration> 
    <executions> 
     <execution> 
      <id>sleep-for-a-while</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

編輯:萬一有人會抓到這個。基於jl的評論,任務確實已被棄用,而基於使用目標的是同樣的事情。整齊地重組,但具有相同的功能。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.8</version> 
    <executions> 
     <execution> 
      <id>sleep-for-a-while</id> 
      <phase>pre-integration-test</phase> 
      <configuration> 
       <target> 
        <sleep seconds="5" /> 
       </target> 
      </configuration> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
+0

謝謝你,你會親切地解釋這是怎麼之前互爲作用的測試推出。我已經嘗試將階段設置爲預集成測試,但這不適用於我。正在使用的Maven目標是乾淨的驗證 – Keynash

+0

我已經添加了一個示例。它以什麼方式不適合你?我希望你使用預集成測試作爲階段(注意,你有預集成測試,但可能是一個錯字?)。 – DanielBarbarian

+1

@Keynash:將它放在jetty-maven-plugin的定義之後的某處。在達到集成測試階段之前,這會使構建速度降低5秒,因此Jetty有足夠的時間啓動。 – carlspring

0

我創建了一個maven-sleep-plugin一次。不知道它現在有多少功能,你可以嘗試讓我們知道。

用法(檢查出的源和建築後):

<plugin> 
    <groupId>org.jboss.maven.plugins</groupId> 
    <artifactId>maven-sleep-plugin</artifactId> 
    <version>1.0</version> 
    <executions> 
     <execution> 
      <id>sleep-5-seconds</id> 
      <phase>pre-integration-test</phase> 
      <goals> 
       <goal>sleep</goal> 
      </goals> 
      <configuration> 
       <delay>5</delay> 
      </configuration> 
     </execution> 
    </executions> 
</plugin>