Skip to content

Commit 139bd7a

Browse files
committed
Add useRandomPort
Closes gh-75
1 parent ec5645b commit 139bd7a

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

README.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ static CommonsExecWebServerFactoryBean authorizationServer() {
189189
When starting the remote debugger, it is important to remember that the classpath of the `CommonsExecWebServerFactoryBean` is independent of the project it runs in.
190190
This means, the classpath of the debugger will need to match the classpat of the `CommonsExecWebServerFactoryBean` rather than the project it exists in.
191191

192+
=== Server Port
193+
194+
By default `CommonsExecWebServerFactoryBean` starts the application on a random port by specifying the system property `server.port=0`.
195+
If you need to disable this behavior, you can opt out using the `useRandomPort` property as shown below:
196+
[source,java]
197+
----
198+
@Bean
199+
@OAuth2ClientProviderIssuerUri
200+
static CommonsExecWebServerFactoryBean authorizationServer() {
201+
// @formatter:off
202+
return CommonsExecWebServerFactoryBean.builder()
203+
// ...
204+
.useRandomPort(false);
205+
// @formatter:on
206+
}
207+
----
208+
192209
[[dynamicproperty]]
193210
== @DynamicProperty
194211

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/server/exec/CommonsExecWebServerFactoryBean.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public class CommonsExecWebServerFactoryBean
6464

6565
private final DebugSettings debugSettings = new DebugSettings();
6666

67+
private boolean useRandomPort = true;
68+
6769
CommonsExecWebServerFactoryBean() {
6870
Class<?> jarDetector = ClassUtils.resolveClassName(this.mainClass, null);
6971
this.classpath.entries(new ResourceClasspathEntry(
@@ -113,6 +115,18 @@ public CommonsExecWebServerFactoryBean setAdditionalBeanClassNames(String... add
113115
StringUtils.arrayToCommaDelimitedString(additionalBeanClassNames)));
114116
}
115117

118+
/**
119+
* Sets if the Spring Boot application should be started on a random port. If true,
120+
* specifies the system property server.port=0 to start on a random port, else starts
121+
* on the port following standard Spring Boot rules.
122+
* @param useRandomPort true (default) if should start on random port, else false.
123+
* @return the {@link CommonsExecWebServerFactoryBean} for customization.
124+
*/
125+
public CommonsExecWebServerFactoryBean useRandomPort(boolean useRandomPort) {
126+
this.useRandomPort = useRandomPort;
127+
return this;
128+
}
129+
116130
public CommonsExecWebServerFactoryBean systemProperties(Consumer<Map<String, String>> systemProperties) {
117131
systemProperties.accept(this.systemProperties);
118132
return this;
@@ -150,7 +164,7 @@ private CommonsExecWebServer build() {
150164
private String[] createSystemPropertyArgs() {
151165
Map<String, String> systemPropertyArgs = new HashMap<>(this.systemProperties);
152166
systemPropertyArgs.put("PORTFILE", this.applicationPortFile.getAbsolutePath());
153-
if (!systemPropertyArgs.containsKey("server.port")) {
167+
if (this.useRandomPort) {
154168
systemPropertyArgs.put("server.port", "0");
155169
}
156170
return systemPropertyArgs.entrySet().stream().map((e) -> "-D" + e.getKey() + "=" + e.getValue() + "")

spring-boot-testjars/src/test/java/org/springframework/experimental/boot/server/exec/CommonsExecWebServerFactoryBeanTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,9 @@ void doesNotAddJarLauncherDetectorLauncherDetectorWhenMainClassDefined() throws
105105
}
106106

107107
@Test
108-
void serverPortWhenSpecifiedThenNotOverridden() throws Exception {
109-
String expectedPort = "1234";
110-
String portSystemProperty = "server.port";
111-
CommonsExecWebServer server = CommonsExecWebServerFactoryBean.builder()
112-
.systemProperties((props) -> props.put(portSystemProperty, expectedPort)).getObject();
113-
assertThat(server.getCommandLine().getArguments()).contains("-D" + portSystemProperty + "=" + expectedPort);
108+
void useRandomPortWhenFalseThenServerPortNot0() throws Exception {
109+
CommonsExecWebServer server = CommonsExecWebServerFactoryBean.builder().useRandomPort(false).getObject();
110+
assertThat(server.getCommandLine().getArguments()).doesNotContain("-Dserver.port=0");
114111
}
115112

116113
// gh-53

0 commit comments

Comments
 (0)