Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Quarkus to pick a random debug port #33521

Merged
merged 1 commit into from
May 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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