From 70e8b6a689f4cd3e8515b17e05a1c18cc35db201 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 22 May 2023 13:44:27 +0300 Subject: [PATCH] Allow Quarkus to pick a random debug port By passing a zero or negative value, Quarkus will launch the dev-mode JVM process using a random debugging port Fixes: #33363 --- .../deployment/dev/QuarkusDevModeLauncher.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java index ff6c918d803ee..32c5ee31a66dd 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.ObjectOutputStream; import java.net.InetAddress; +import java.net.ServerSocket; import java.net.Socket; import java.net.URI; import java.net.UnknownHostException; @@ -40,7 +41,7 @@ public abstract class QuarkusDevModeLauncher { final Pattern validDebug = Pattern.compile("^(true|false|client|[0-9]+)$"); - final Pattern validPort = Pattern.compile("^[0-9]+$"); + final Pattern validPort = Pattern.compile("^-?[0-9]+$"); public class Builder> { @@ -369,7 +370,7 @@ protected void prepare() throws Exception { } } if (port <= 0) { - throw new Exception("The specified debug port must be greater than 0"); + port = getRandomPort(); } if (debug != null && debug.equalsIgnoreCase("client")) { @@ -479,6 +480,12 @@ protected void prepare() throws Exception { } } + private int getRandomPort() throws IOException { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } + } + private InetAddress getInetAddress(String host) throws UnknownHostException { if ("localhost".equals(host)) { return InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });