Skip to content

Commit

Permalink
Allow Quarkus to pick a random debug port
Browse files Browse the repository at this point in the history
By passing a zero or negative value, Quarkus
will launch the dev-mode JVM process
using a random debugging port

Fixes: #33363
  • Loading branch information
geoand committed May 22, 2023
1 parent 0f99bf0 commit 70e8b6a
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<R extends QuarkusDevModeLauncher, B extends Builder<R, B>> {

Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 70e8b6a

Please sign in to comment.