Skip to content

Commit 9ba851d

Browse files
committed
Merge pull request #44330 from nosan
* pr/44330: Polish ClasspathBuilder Closes gh-44330
2 parents b6abb42 + 616d4cb commit 9ba851d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ void buildWithClassPathOnWindows(@TempDir Path tempDir) throws Exception {
119119

120120
@Test
121121
void buildAndRunWithLongClassPath() throws IOException, InterruptedException {
122-
URL[] urls = Arrays.stream(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
123-
.map(this::toURL)
124-
.toArray(URL[]::new);
122+
StringBuilder classPath = new StringBuilder(ManagementFactory.getRuntimeMXBean().getClassPath());
123+
// Simulates [CreateProcess error=206, The filename or extension is too long]
124+
while (classPath.length() < 35000) {
125+
classPath.append(File.pathSeparator).append(classPath);
126+
}
127+
URL[] urls = Arrays.stream(classPath.toString().split(File.pathSeparator)).map(this::toURL).toArray(URL[]::new);
125128
List<String> command = CommandLineBuilder.forMainClass(ClassWithMainMethod.class.getName())
126129
.withClasspath(urls)
127130
.build();

0 commit comments

Comments
 (0)