如何从jetty-maven-plugin中获取端口号?

3
在集成测试中使用的 jetty-maven-plugin 7.x 可以在运行时动态查找可用端口。我该如何保存找到的端口号并在Java集成测试中使用它?也许 jetty-maven-plugin 可以将其保存到系统变量中?
1个回答

4

以下是其工作原理:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <portNames>
                    <portName>jetty.port</portName>
                    <portName>jetty.port.stop</portName>
                </portNames>
            </configuration>
            <executions>
                <execution>
                    <id>reserve-port</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>reserve-network-port</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

然后可以使用${jetty.port}来配置Jetty插件。


3
作为补充说明:在你的Java测试中使用${jetty.port},你可以通过使用systemPropertyValues将其传递给Surefire插件:http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html - sleske
请注意,使用jetty-maven-plugin的“run”目标将导致构建助手执行两次,这可能会破坏您的测试,因为它将保留新端口并再次过滤文件。切换到“start”以避免此问题。 - massfords

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接