Skip to content

Commit 598a515

Browse files
committed
Polish ClasspathBuilder
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 29e498b commit 598a515

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ClasspathBuilder.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,30 @@ protected ClasspathBuilder(List<URL> urls) {
5151
}
5252

5353
/**
54-
* Builds a classpath string or an argument file representing the classpath, depending
55-
* on the operating system.
56-
* @param urls an array of {@link URL} representing the elements of the classpath
57-
* @return the classpath; on Windows, the path to an argument file is returned,
58-
* prefixed with '@'
54+
* Creates a ClasspathBuilder instance using the specified list of URLs.
55+
* @param urls a list of {@link URL} objects representing the elements of the
56+
* classpath
57+
* @return a new instance of {@code ClasspathBuilder}
5958
*/
6059
static ClasspathBuilder forURLs(List<URL> urls) {
6160
return new ClasspathBuilder(new ArrayList<>(urls));
6261
}
6362

6463
/**
65-
* Builds a classpath string or an argument file representing the classpath, depending
66-
* on the operating system.
67-
* @param urls an array of {@link URL} representing the elements of the classpath
68-
* @return the classpath; on Windows, the path to an argument file is returned,
69-
* prefixed with '@'
64+
* Creates a ClasspathBuilder instance using the specified array of URLs.
65+
* @param urls an array of {@link URL} objects representing the elements of the
66+
* classpath
67+
* @return a new instance of {@code ClasspathBuilder}
7068
*/
7169
static ClasspathBuilder forURLs(URL... urls) {
7270
return new ClasspathBuilder(Arrays.asList(urls));
7371
}
7472

73+
/**
74+
* Builds {@link Classpath} that containing a classpath argument and its corresponding
75+
* classpath elements.
76+
* @return a {@code Classpath}
77+
*/
7578
Classpath build() {
7679
if (ObjectUtils.isEmpty(this.urls)) {
7780
return new Classpath("", Collections.emptyList());
@@ -88,6 +91,13 @@ Classpath build() {
8891
return new Classpath(argument, files);
8992
}
9093

94+
/**
95+
* Determines if an argument file should be used for the classpath based on the
96+
* operating system. On Windows, argument files are used due to the command length
97+
* limitation.
98+
* @return {@code true} if an argument file is required for the classpath,
99+
* {@code false} otherwise
100+
*/
91101
protected boolean needsClasspathArgFile() {
92102
String os = System.getProperty("os.name");
93103
if (!StringUtils.hasText(os)) {
@@ -145,6 +155,9 @@ private static Path toFile(URL url) {
145155
}
146156
}
147157

158+
/**
159+
* Classpath consisting of a {@code -cp} argument and its associated elements.
160+
*/
148161
static final class Classpath {
149162

150163
private final String argument;
@@ -157,7 +170,8 @@ private Classpath(String argument, List<Path> elements) {
157170
}
158171

159172
/**
160-
* Return the {@code -cp} argument value.
173+
* Return the {@code -cp} argument value; on Windows, the path to an argument file
174+
* is returned, prefixed with '@'.
161175
* @return the argument to use
162176
*/
163177
String argument() {

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/CommandLineBuilderTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.net.URL;
2525
import java.nio.file.Path;
2626
import java.nio.file.Paths;
27+
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.List;
2930
import java.util.Map;
31+
import java.util.stream.Collectors;
3032

3133
import org.junit.jupiter.api.Test;
3234
import org.junit.jupiter.api.condition.DisabledOnOs;
@@ -119,11 +121,15 @@ void buildWithClassPathOnWindows(@TempDir Path tempDir) throws Exception {
119121

120122
@Test
121123
void buildAndRunWithLongClassPath() throws IOException, InterruptedException {
122-
URL[] urls = Arrays.stream(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
124+
List<URL> urls = Arrays.stream(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
123125
.map(this::toURL)
124-
.toArray(URL[]::new);
126+
.collect(Collectors.toCollection(ArrayList::new));
127+
// Simulates [CreateProcess error=206, The filename or extension is too long]
128+
for (int i = 0; i < 5; i++) {
129+
urls.addAll(new ArrayList<>(urls));
130+
}
125131
List<String> command = CommandLineBuilder.forMainClass(ClassWithMainMethod.class.getName())
126-
.withClasspath(urls)
132+
.withClasspath(urls.toArray(new URL[0]))
127133
.build();
128134
ProcessBuilder pb = new JavaExecutable().processBuilder(command.toArray(new String[0]));
129135
Process process = pb.start();

0 commit comments

Comments
 (0)