简体   繁体   中英

spring-boot-maven-plugin - start JMX at random port

Small question regarding how to start jmxPort randomly, using the spring-boot-maven-plugin please.

Currently, I am running integration test with the spring-boot-maven-plugin, and it needs the jmxPort.

Hence, I am initializing it this way:

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

However, the port 0 is not working.

How to start it randomly please?

Thank you

You can use for example org.codehaus.mojo.build-helper-maven-plugin

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

You will add one more step to your plugin config. This step will generate variable with random port number which can be then used with the spring-boot-maven-plugin .

Your configuration will look similar to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM