使用jetty-maven-plugin运行Jetty,并在jetty运行时完成构建

时间:2015-11-26 09:41:09

标签: maven jetty pom.xml maven-plugin maven-jetty-plugin

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

我创建了一个 pom.xml ,它启动了jetty并部署了war文件, 在码头开始之后,我希望maven完成构建,同时让码头继续运行,这样我就可以启动另一个maven构建来在我刚刚运行码头的服务器上运行测试。

然后我将创建另一个只停止jetty服务器的maven构建。

问题是我没有设法启动码头并使之后的maven构建停止,有谁知道该怎么做?

P.S 我使用了“run-forked”,但它仍然等待停止信号,因此构建卡住了。

这是码头启动资料:

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

1 个答案:

答案 0 :(得分:2)

应该很清楚,Maven是一个构建工具,而不是命令执行器工具。 启动/停止Jetty应该是集成测试执行阶段中相同构建的一部分。此外,您还在两个maven构建之间创建依赖关系(实际上并不是有效构建),如果停止构建失败(无论出于何种原因),这可能是CI环境的一部分问题,并使启动的jetty保持运行状态并因此消耗CI服务器上未定义时间的资源。

一个简单的启动/测试/停止流程可以作为同一个Maven构建的一部分实现如下:

        <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插件配置为在测试阶段跳过集成测试,然后在集成测试fase之前启动jetty,执行集成测试(基于后缀),然后停止jetty。

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

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

  • jetty-question(聚合器项目)
    • 启动码头模块
    • use-jetty-module(具有start-jetty-module作为依赖)
    • stop-jetty-module(具有start-jetty-module和use-jetty-module作为依赖项)

更新2 :低于工作方法(在Windows机器上测试) 这是聚合器项目的pom文件,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(聚合器所需)。

这是jetty-start模块的pom文件,它是一个嵌套在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>

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

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

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

重要:如上所述,它需要依赖于jetty-start模块,以便反应堆maven构建将在jetty-start之后执行它(因此我们确保jetty在执行此构建时将运行)。 注意Junit和selenium的依赖关系,我用它们通过下面的junit集成测试有效地测试运行的jetty:

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());
    }
}

最后,这是jetty-stop模块的pom文件

    <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-start模块类似的配置。这个模块也配置了jetty插件,它通过antrun插件停止它,后者将在后台执行mvn jetty:stop目标。 还要注意这个模块的依赖关系:它需要依赖于jetty-start和jetty-use,以便maven reactor build在最后执行它。

进一步说明:码头启动和码头停靠模块上的码头配置显然需要共享停止键和停止端口。对于此示例,服务器端口在pom文件中进行了编码(对于jetty-start和jetty-stop模块也需要相同),但您也可以将其移动到父模块中的属性。 此外,antrun插件在Windows模式下执行后台进程。如果你在Linux上运行a&amp;后缀也应该成功。 我还建议将它保存在多模块项目中,以便确保依赖项耦合在一起。

虽然我不建议在本答案的顶部描述这种方法,但要让它发挥作用是具有挑战性和乐趣的,所以谢谢你的乐趣。希望你也能得到它。

相关问题