我想用Jetty maven插件运行Jetty,并在Jetty运行时完成构建。
我创建了一个pom.xml,它启动了jetty并部署了一个war文件,在jetty启动后,我希望maven在让jetty运行的同时完成构建,这样我就可以启动另一个maven构建,在我刚刚运行jetty的服务器上运行测试。
然后,我将创建另一个maven构建,它只是停止jetty服务器。
问题是,我没能启动jetty并在那之后停止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>
应该清楚的是,Maven是一个构建工具,而不是命令执行器工具。在集成测试执行阶段,启动/停止Jetty应该是同一构建的一部分。此外,您还创建了两个maven构建之间的依赖关系(实际上并不是有效的构建),如果停止构建失败(无论出于何种原因),并使启动的jetty保持运行,从而在未定义的时间内消耗CI服务器上的资源,那么这可能会成为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项目中开始,在另一个mavin模块中停止,您可以应用以下方法:有一个聚合器/多模块maven项目:一个模块将提供启动,另一个模块提供停止,其他模块将使用正在运行的jetty。然而,maven reactor可能不会按照您希望的顺序调用它们,然后您应该确保停止模块依赖于启动模块(将其作为依赖项),并且任何需要运行模块的模块也将启动模块作为依赖项。此外,停止模块还应该依赖于测试模块,以便它只在最后执行。这就行了。因此,总结一下:
- 码头问题(聚合器项目)
- 启动码头模块
- 使用jetty模块(将启动jetty模块作为依赖项)
- 停止jetty模块(具有启动jetty模块和使用jetty模块作为依赖项)
更新2:工作方法下方(在Windows机器上测试)这是聚合器项目的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>
<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启动模块的pom文件,它是嵌套在jetty问题下的文件夹
<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插件执行后台进程以执行mvnjetty:run在我的示例代码中,部署的应用程序simple提供了一个打印Helloworld的index.html页面。
以下是jetty使用模块的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启动模块,以便reactor maven构建将在jetty启动后执行它(因此,我们确信执行此构建时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());
}
}
最后,这里是码头停止模块的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插件,并通过antrun插件停止它,该插件将在后台执行mvnjetty:stop目标。还要注意这个模块的依赖性:它需要依赖于jetty启动和jetty使用,这样maven reactor构建将在最后执行它。
进一步注意:jetty启动和jetty停止模块上的jetty配置显然需要共享停止键和停止端口。例如,服务器端口在pom文件中进行了harcode编码(jetty启动模块和jetty终止模块也需要相同),但您也可以将其移到父模块中的属性中。此外,antrun插件在Windows模式下执行后台进程。如果您在Linux上运行的是&后缀也应该起作用。我还建议将它保存在一个多模块项目中,这样您就可以确保依赖关系耦合在一起。
虽然我不会建议这个答案顶部所描述的方法,但让它发挥作用既有挑战性,也很有趣,所以谢谢你的乐趣。希望你也能成功。