spring-boot-maven-plugin -启动JMX随机端口



关于如何随机启动jmxPort,请使用spring-boot-maven-plugin .

目前,我正在使用spring-boot-maven-plugin运行集成测试,它需要jmxPort。

因此,我这样初始化它:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<wait>1000</wait>
<maxAttempts>180</maxAttempts>
<jmxPort>0</jmxPort>
<environmentVariables>
<SPRING_PROFILES_ACTIVE>integration</SPRING_PROFILES_ACTIVE>
</environmentVariables>
<jvmArguments>
-Dspring.application.admin.enabled=true
</jvmArguments>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

但是,端口0不工作。

如何随机启动?

谢谢

您可以使用例如org.codehaus.mojo.build-helper-maven-plugin

https://www.mojohaus.org/build-helper-maven-plugin/

您将在插件配置中添加更多步骤。此步骤将生成具有随机端口号的变量,然后可以与spring-boot-maven-plugin一起使用。

你的配置看起来像这样:

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>random.jmx.port</portName>
</portNames>
<randomPort>true</randomPort>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<wait>1000</wait>
<maxAttempts>180</maxAttempts>
<jmxPort>${random.jmx.port}</jmxPort>
<environmentVariables>
<SPRING_PROFILES_ACTIVE>integration</SPRING_PROFILES_ACTIVE>
</environmentVariables>
<jvmArguments>
-Dspring.application.admin.enabled=true
</jvmArguments>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

最新更新