使用jetty-maven-plugin运行Jetty并在jetty运行时完成构建 [英] Run Jetty with jetty-maven-plugin and finish the build when jetty is running

查看:135
本文介绍了使用jetty-maven-plugin运行Jetty并在jetty运行时完成构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用jetty-maven-plugin运行Jetty并在jetty运行时完成构建.

I want to Run Jetty with jetty-maven-plugin and finish the build when jetty is running.

我创建了一个 pom.xml ,该文件可以启动码头并部署战争文件, 在码头开始之后,我希望Maven在保持码头运行的同时完成构建,以便我可以启动另一个Maven构建,以便在我刚刚运行码头的服务器上运行测试.

I created a pom.xml that starts jetty and deploying a war file, after the jetty starts I want maven to finish the build while leaving the jetty running, So that I could start another maven build to run tests on the server I just ran jetty on.

然后,我将创建另一个停止码头服务器的Maven构建.

Then I will create another maven build that just stops the jetty server.

问题是我没有设法启动码头并在那之后停止了Maven的建造,有人知道怎么做吗?

Problem is that I didn't managed to start jetty and make the maven build stop after that, Does anyone knows how to do that?

p.s 我为此使用了"run-forked",但是它仍然等待停止信号,因此构建卡住了.

p.s I used "run-forked" for that, but it still waited for a stop signal so the build was stuck.

这是跳船开始的个人资料:

This is the jetty-start profile:

 <profile>
           <id>start-jetty</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <configuration>
                            <war>${unpacked.war.directory}</war>
                            <contextXml>${unpacked.war.directory}/WEB-INF/jetty-web.xml</contextXml>
                            <webApp>
                                <contextPath>/qabin</contextPath>
                            </webApp>
                            <systemProperties>
                                <systemProperty>
                                    <name>mercy.td.sa_config_dir</name>
                                    <value>${tests.runtime}</value>
                                </systemProperty>
                                <systemProperty>
                                    <name>jetty.port</name>
                                    <value>${jetty.start.port}</value>
                                </systemProperty>
                            </systemProperties>
                            <stopPort>${jetty.stop.port}</stopPort>
                            <stopKey>STOP</stopKey>
                        </configuration>
                        <executions>
                            <execution>
                                <id>start-jetty</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run-forked</goal>
                                </goals>
                                <configuration>
                                    <scanIntervalSeconds>0</scanIntervalSeconds>
                                    <daemon>true</daemon>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

推荐答案

很明显,Maven是一个构建工具,而不是命令执行器工具. 在集成测试执行阶段,启动/停止Jetty应该是同一构建的一部分.此外,您还将在两个Maven构建之间创建依赖关系(实际上并不是有效构建),如果停止构建由于某种原因而失败并且使启动的码头继续运行,则这可能是CI环境中的一个问题.因此,在CI服务器上消耗资源的时间是不确定的.

It should be clear that Maven is a build tool, not a commands executor tool. Starting/stopping Jetty should be part of the same build within an integration tests execution phase. Moreover, you are also creating dependencies between two maven builds (which are not effectively builds indeed), which may be a problem as part of your CI environment if ever the stop build fails - for whatever reason - and leave the started jetty up and running and as such consume resources on your CI server for undefined time.

作为同一Maven构建的一部分,可以实现以下简单的开始/测试/停止流程:

A simple start/test/stop flow could be implemented as following as part of the same Maven build:

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <phase>integration-test</phase>
                            <configuration>
                                <excludes>
                                    <exclude>none</exclude>
                                </excludes>
                                <includes>
                                    <include>**/*IntegrationTest.java</include>
                                </includes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>9.2.8.v20150217</version>
                    <configuration>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <stopKey>foo</stopKey>
                        <stopPort>9999</stopPort>
                        <stopWait>2</stopWait>
                        <webApp>
                            <contextPath>/examplecomponent</contextPath>
                        </webApp>
                        <httpConnector>
                            <port>7777</port>
                        </httpConnector>
                    </configuration>
                    <executions>
                        <execution>
                            <id>start-jetty</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                            <configuration>
                                <scanIntervalSeconds>0</scanIntervalSeconds>
                            </configuration>
                        </execution>
                        <execution>
                            <id>stop-jetty</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.eclipse.jetty</groupId>
                            <artifactId>jetty-util</artifactId>
                            <version>9.2.8.v20150217</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>

基本上,您可以将surefire插件配置为在测试阶段跳过集成测试,然后在进行集成测试之前先启动码头,执行集成测试(基于后缀),然后再停止码头.

Basically, you configure the surefire plugin to skip integration tests during the test phase, then start jetty before the integration test fase, execute integration tests (based on suffix) and then stop jetty afterwards.

我还建议移动它的配置文件,以使默认构建更快并独立于集成测试,以便它也可以在脱机时成功运行,然后在需要时激活该配置文件(即在CI构建中).

I would also suggest to move it a profile in order to make the default build faster and independent from integration tests, so that it can also run successfully when offline, then activate the profile when required (i.e. on the CI build).

已更新:如果您确实需要在Maven项目中开始并且在其他Maven模块中停止,则可以采用以下方法: 有一个聚合器/多模块Maven项目:一个模块将提供启动,另一个模块将提供停止,其他模块将使用正在运行的码头.但是,Maven反应器可能不会按您希望的顺序调用它们,然后应确保停止模块依赖于启动模块(将其作为依赖项),并且任何需要运行模块的模块也将启动模块作为依赖项.此外,停止模块还应该依赖于测试模块,以便仅在最后执行.这应该够了吧. 因此,总结一下:

Updated: if you really need to have a start in a maven project and a stop in an other maven module, you could apply the following approach: Have an aggregator/multimodule maven project: a module will provide the start, another module will provide the stop, other modules will use the running jetty. However, the maven reactor may not invoke them in the order you wish, you should then make sure the stop module depends on the start module (has it as dependency) and any module requiring the running module will also have the start module as dependency. Moreover, the stop module should also depend on testing module so that it will be executed only at the end. That should do the trick. Hence, to summarize:

  • jetty-question(聚合器项目)
    • start-jetty-module
    • use-jetty-module(具有start-jetty-module作为依赖项)
    • stop-jetty-module(具有start-jetty-module和use-jetty-module作为依赖项)
    • jetty-question (the aggregator project)
      • start-jetty-module
      • use-jetty-module (has start-jetty-module as dependency)
      • stop-jetty-module (has start-jetty-module and use-jetty-module as dependencies)

      已更新2 :下面的工作方法(在Windows计算机上进行了测试) 这是聚合器项目jetty-question的pom文件:

      Updated 2: Below the working approach (tested on Windows machine) Here is the pom file of the aggregator project, jetty-question:

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.stackoverflow</groupId>
        <artifactId>jetty-question</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <modules>
          <module>jetty-start</module>
          <module>jetty-stop</module>
          <module>jetty-use</module>
        </modules>
      </project>
      

      注意模块声明和包装为pom(聚合器必需).

      Note the modules declaration and the packaging as pom (required for aggregators).

      这是jetty-start模块的pom文件,该模块是嵌套在jetty-question下的文件夹

      Here is the pom file of the jetty-start module, which is a folder nested under jetty-question

          <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>com.stackoverflow</groupId>
              <artifactId>jetty-question</artifactId>
              <version>1.0.0-SNAPSHOT</version>
          </parent>
          <artifactId>jetty-start</artifactId>
          <packaging>war</packaging>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-antrun-plugin</artifactId>
                      <version>1.6</version>
                      <executions>
                          <execution>
                              <phase>verify</phase>
                              <configuration>
                                  <target>
                                      <exec executable="cmd.exe" spawn="true">
                                          <arg value="/c" />
                                          <arg value="mvn jetty:run" />
                                      </exec>
                                  </target>
                              </configuration>
                              <goals>
                                  <goal>run</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
                  <plugin>
                      <groupId>org.eclipse.jetty</groupId>
                      <artifactId>jetty-maven-plugin</artifactId>
                      <version>9.2.8.v20150217</version>
                      <configuration>
                          <scanIntervalSeconds>10</scanIntervalSeconds>
                          <stopKey>foo</stopKey>
                          <stopPort>9999</stopPort>
                          <stopWait>2</stopWait>
                          <webApp>
                              <contextPath>/jetty-start</contextPath>
                          </webApp>
                          <httpConnector>
                              <port>7777</port>
                          </httpConnector>
                      </configuration>
                      <dependencies>
                          <dependency>
                              <groupId>org.eclipse.jetty</groupId>
                              <artifactId>jetty-util</artifactId>
                              <version>9.2.8.v20150217</version>
                          </dependency>
                      </dependencies>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      注意:该模块正在配置码头插件,然后通过antrun插件执行后台进程以执行mvn jetty:run 在我的示例代码中,已部署的应用程序简单地提供了一个index.html页面打印Hello world.

      Note: the module is configuring the jetty plugin and then executing a background process via the antrun plugin to execute mvn jetty:run In my example code, the deployed application simple provided an index.html page printing Hello world.

      这是码头使用模块的pom文件:

      Here is the pom file of the jetty-use module:

          <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>com.stackoverflow</groupId>
              <artifactId>jetty-question</artifactId>
              <version>1.0.0-SNAPSHOT</version>
          </parent>
          <artifactId>jetty-use</artifactId>
      
          <dependencies>
              <dependency>
                  <groupId>com.stackoverflow</groupId>
                  <artifactId>jetty-start</artifactId>
                  <version>1.0.0-SNAPSHOT</version>
                  <type>war</type>
              </dependency>
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <scope>test</scope>
              </dependency>
              <dependency>
                  <groupId>org.seleniumhq.selenium</groupId>
                  <artifactId>selenium-java</artifactId>
                  <version>2.47.1</version>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      </project>
      

      重要:如上所述,它需要依赖码头启动模块,以便反应堆Maven构建将在码头启动之后执行它(因此,我们确定执行此构建时码头会运行). 注意Junit和硒的依赖关系,我使用它们通过下面的junit集成测试有效地测试了正在运行的码头:

      Important: as described above, it needs a dependencies on the jetty-start module so that the reactor maven build will execute it after the jetty-start (and as such we are sure jetty would be running when executing this build). Note the dependencies for Junit and selenium, I used them to effectively test the running jetty via the junit integration test below:

      public class AppIntegrationTest extends TestCase {
      
          public void testApp() throws Exception {
          // Create a new instance of the Firefox driver
          WebDriver driver = new HtmlUnitDriver();
      
          // Launch the Online Store Website
          driver.get("http://localhost:7777/jetty-start");
      
          WebElement element = driver.findElement(By.id("title"));
          Assert.assertNotNull(element);
          Assert.assertNotNull(element.getText());
          Assert.assertEquals("Hello World!", element.getText());
          }
      }
      

      最后,这是码头停车模块的pom文件

      Finally, here is the pom file of the jetty-stop module

          <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>com.stackoverflow</groupId>
              <artifactId>jetty-question</artifactId>
              <version>1.0.0-SNAPSHOT</version>
          </parent>
          <artifactId>jetty-stop</artifactId>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-antrun-plugin</artifactId>
                      <version>1.6</version>
                      <executions>
                          <execution>
                              <phase>verify</phase>
                              <configuration>
                                  <target>
                                      <exec executable="cmd.exe" spawn="true">
                                          <arg value="/c" />
                                          <arg value="mvn jetty:stop" />
                                      </exec>
                                  </target>
                              </configuration>
                              <goals>
                                  <goal>run</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
                  <plugin>
                      <groupId>org.eclipse.jetty</groupId>
                      <artifactId>jetty-maven-plugin</artifactId>
                      <version>9.2.8.v20150217</version>
                      <configuration>
                          <scanIntervalSeconds>10</scanIntervalSeconds>
                          <stopKey>foo</stopKey>
                          <stopPort>9999</stopPort>
                          <stopWait>2</stopWait>
                          <httpConnector>
                              <port>7777</port>
                          </httpConnector>
                      </configuration>
                      <dependencies>
                          <dependency>
                              <groupId>org.eclipse.jetty</groupId>
                              <artifactId>jetty-util</artifactId>
                              <version>9.2.8.v20150217</version>
                          </dependency>
                      </dependencies>
                  </plugin>
              </plugins>
          </build>
      
          <dependencies>
              <dependency>
                  <groupId>com.stackoverflow</groupId>
                  <artifactId>jetty-start</artifactId>
                  <version>1.0.0-SNAPSHOT</version>
                  <type>war</type>
              </dependency>
              <dependency>
                  <groupId>com.stackoverflow</groupId>
                  <artifactId>jetty-use</artifactId>
                  <version>1.0.0-SNAPSHOT</version>
              </dependency>
          </dependencies>
      </project>
      

      请注意与码头启动模块类似的配置.该模块还配置了jetty插件,并通过antrun插件将其停止,该插件将在后台执行mvn jetty:stop目标. 还要注意该模块的依赖关系:它需要同时依赖码头启动和码头使用,以便Maven反应堆的建造将在最后执行.

      Note the similar configuration to the jetty-start module. This module is also configuring the jetty plugin and it is stopping it via the antrun plugin which will execute in background the mvn jetty:stop goal. Also note the dependencies of this module: it needs to depend on both jetty-start and jetty-use so that the maven reactor build will execute it at the end.

      更多说明:码头启动和码头停止模块上的码头配置显然需要共享停止键和停止端口.在此示例中,服务器端口在pom文件中进行了编码(对于jetty-start和jetty-stop模块,该端口也必须相同),但是您也可以将其移动到父模块的属性中. 此外,antrun插件在Windows模式下执行后台进程.如果您在Linux上运行,则&后缀也可以解决问题. 我还建议将其保留在多模块项目中,以确保依赖关系耦合在一起.

      Further notes: the jetty configuration on the jetty-start and jetty-stop module need obviously to share the stop key and stop port. For this example, server port is harcoded in the pom file (which also needs to be the same for both jetty-start and jetty-stop modules), but you could also move it to a property in the parent module. Moreover, the antrun plugin executes a background process in Windows mode. If you are running on Linux a & suffix should also make the trick. I would also suggest to keep it in a multimodule project so that you can make sure that dependencies are coupled together.

      尽管我不建议像该答案顶部所述的那样使用该方法,但使其工作起来既充满挑战又充满乐趣,因此,感谢您的乐趣.希望您也能使用.

      Although I would not advice this approach as described at the top of this answer, it was challenging and fun to get it to work, so thank you for the fun. Hope you get it work too.

      这篇关于使用jetty-maven-plugin运行Jetty并在jetty运行时完成构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆