Skip to content

Commit 082c585

Browse files
committed
Polish
1 parent 8aea6d5 commit 082c585

File tree

7 files changed

+254
-319
lines changed

7 files changed

+254
-319
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,31 @@ protected final void compileSourceFiles(URL[] classPath, File sourcesDirectory,
145145
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
146146
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
147147
JavaCompilerPluginConfiguration compilerConfiguration = new JavaCompilerPluginConfiguration(this.project);
148-
List<String> options = new ArrayList<>();
149-
options.add("-cp");
150-
options.add(ClasspathBuilder.forURLs(classPath).build().toString());
151-
options.add("-d");
152-
options.add(outputDirectory.toPath().toAbsolutePath().toString());
148+
List<String> args = new ArrayList<>();
149+
args.addAll(ClassPath.of(classPath).args(false));
150+
args.add("-d");
151+
args.add(outputDirectory.toPath().toAbsolutePath().toString());
153152
String releaseVersion = compilerConfiguration.getReleaseVersion();
154153
if (releaseVersion != null) {
155-
options.add("--release");
156-
options.add(releaseVersion);
154+
args.add("--release");
155+
args.add(releaseVersion);
157156
}
158157
else {
159158
String source = compilerConfiguration.getSourceMajorVersion();
160159
if (source != null) {
161-
options.add("--source");
162-
options.add(source);
160+
args.add("--source");
161+
args.add(source);
163162
}
164163
String target = compilerConfiguration.getTargetMajorVersion();
165164
if (target != null) {
166-
options.add("--target");
167-
options.add(target);
165+
args.add("--target");
166+
args.add(target);
168167
}
169168
}
170-
options.addAll(new RunArguments(this.compilerArguments).getArgs());
169+
args.addAll(new RunArguments(this.compilerArguments).getArgs());
171170
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromPaths(sourceFiles);
172171
Errors errors = new Errors();
173-
CompilationTask task = compiler.getTask(null, fileManager, errors, options, null, compilationUnits);
172+
CompilationTask task = compiler.getTask(null, fileManager, errors, args, null, compilationUnits);
174173
boolean result = task.call();
175174
if (!result || errors.hasReportedErrors()) {
176175
throw new IllegalStateException("Unable to compile generated source" + errors);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.apache.maven.toolchain.ToolchainManager;
4040

4141
import org.springframework.boot.loader.tools.FileUtils;
42-
import org.springframework.boot.maven.ClasspathBuilder.Classpath;
4342
import org.springframework.util.Assert;
4443
import org.springframework.util.ObjectUtils;
4544

@@ -346,12 +345,11 @@ private void addActiveProfileArgument(RunArguments arguments) {
346345

347346
private void addClasspath(List<String> args) throws MojoExecutionException {
348347
try {
349-
Classpath classpath = ClasspathBuilder.forURLs(getClassPathUrls()).build();
348+
ClassPath classpath = ClassPath.of(getClassPathUrls());
350349
if (getLog().isDebugEnabled()) {
351350
getLog().debug("Classpath for forked process: " + classpath);
352351
}
353-
args.add("-cp");
354-
args.add(classpath.argument());
352+
args.addAll(classpath.args(true));
355353
}
356354
catch (Exception ex) {
357355
throw new MojoExecutionException("Could not build classpath", ex);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.maven;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.net.URISyntaxException;
22+
import java.net.URL;
23+
import java.nio.charset.Charset;
24+
import java.nio.charset.UnsupportedCharsetException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.util.Arrays;
29+
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.Locale;
32+
import java.util.function.UnaryOperator;
33+
import java.util.stream.Collector;
34+
import java.util.stream.Collectors;
35+
36+
import org.springframework.util.StringUtils;
37+
38+
/**
39+
* Encapsulates a class path and allows argument parameters to be created. On Windows an
40+
* argument file is used whenever possible since the maximum command line length is
41+
* limited.
42+
*
43+
* @author Stephane Nicoll
44+
* @author Dmytro Nosan
45+
* @author Phillip Webb
46+
*/
47+
final class ClassPath {
48+
49+
private static final Collector<CharSequence, ?, String> JOIN_BY_PATH_SEPARATOR = Collectors
50+
.joining(File.pathSeparator);
51+
52+
private final boolean preferArgFile;
53+
54+
private final String path;
55+
56+
private ClassPath(boolean preferArgFile, String path) {
57+
this.preferArgFile = preferArgFile;
58+
this.path = path;
59+
}
60+
61+
/**
62+
* Return the args to append to a java command line call (including {@code -cp}).
63+
* @param allowArgFile if an arg file can be used
64+
* @return the command line arguments
65+
*/
66+
List<String> args(boolean allowArgFile) {
67+
return (!this.path.isEmpty()) ? List.of("-cp", classPathArg(allowArgFile)) : Collections.emptyList();
68+
}
69+
70+
private String classPathArg(boolean allowArgFile) {
71+
if (this.preferArgFile && allowArgFile) {
72+
try {
73+
return "@" + createArgFile();
74+
}
75+
catch (IOException ex) {
76+
return this.path;
77+
}
78+
}
79+
return this.path;
80+
}
81+
82+
private Path createArgFile() throws IOException {
83+
Path argFile = Files.createTempFile("spring-boot-", ".argfile");
84+
argFile.toFile().deleteOnExit();
85+
Files.writeString(argFile, "\"" + this.path.replace("\\", "\\\\") + "\"", charset());
86+
return argFile;
87+
}
88+
89+
private Charset charset() {
90+
try {
91+
String nativeEncoding = System.getProperty("native.encoding");
92+
return (nativeEncoding != null) ? Charset.forName(nativeEncoding) : Charset.defaultCharset();
93+
}
94+
catch (UnsupportedCharsetException ex) {
95+
return Charset.defaultCharset();
96+
}
97+
}
98+
99+
/**
100+
* Factory method to create a {@link ClassPath} of the given URLs.
101+
* @param urls the class path URLs
102+
* @return a new {@link ClassPath} instance
103+
*/
104+
static ClassPath of(URL... urls) {
105+
return of(Arrays.asList(urls));
106+
}
107+
108+
/**
109+
* Factory method to create a {@link ClassPath} of the given URLs.
110+
* @param urls the class path URLs
111+
* @return a new {@link ClassPath} instance
112+
*/
113+
static ClassPath of(List<URL> urls) {
114+
return of(System::getProperty, urls);
115+
}
116+
117+
/**
118+
* Factory method to create a {@link ClassPath} of the given URLs.
119+
* @param getSystemProperty {@link UnaryOperator} allowing access to system properties
120+
* @param urls the class path URLs
121+
* @return a new {@link ClassPath} instance
122+
*/
123+
static ClassPath of(UnaryOperator<String> getSystemProperty, List<URL> urls) {
124+
boolean preferrArgFile = urls.size() > 1 && isWindows(getSystemProperty);
125+
return new ClassPath(preferrArgFile,
126+
urls.stream().map(ClassPath::toPathString).collect(JOIN_BY_PATH_SEPARATOR));
127+
}
128+
129+
private static boolean isWindows(UnaryOperator<String> getSystemProperty) {
130+
String os = getSystemProperty.apply("os.name");
131+
return StringUtils.hasText(os) && os.toLowerCase(Locale.ROOT).contains("win");
132+
}
133+
134+
private static String toPathString(URL url) {
135+
try {
136+
return Paths.get(url.toURI()).toString();
137+
}
138+
catch (URISyntaxException ex) {
139+
throw new IllegalArgumentException(ex);
140+
}
141+
}
142+
143+
}

0 commit comments

Comments
 (0)