|
| 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.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import java.util.stream.Stream; |
| 35 | + |
| 36 | +import org.springframework.util.ObjectUtils; |
| 37 | +import org.springframework.util.StringUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * Helper class to build the -cp (classpath) argument of a java process. |
| 41 | + * |
| 42 | + * @author Stephane Nicoll |
| 43 | + * @author Dmytro Nosan |
| 44 | + */ |
| 45 | +class ClasspathBuilder { |
| 46 | + |
| 47 | + private final List<URL> urls; |
| 48 | + |
| 49 | + protected ClasspathBuilder(List<URL> urls) { |
| 50 | + this.urls = urls; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 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 '@' |
| 59 | + */ |
| 60 | + static ClasspathBuilder forURLs(List<URL> urls) { |
| 61 | + return new ClasspathBuilder(new ArrayList<>(urls)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 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 '@' |
| 70 | + */ |
| 71 | + static ClasspathBuilder forURLs(URL... urls) { |
| 72 | + return new ClasspathBuilder(Arrays.asList(urls)); |
| 73 | + } |
| 74 | + |
| 75 | + Classpath build() { |
| 76 | + if (ObjectUtils.isEmpty(this.urls)) { |
| 77 | + return new Classpath("", Collections.emptyList()); |
| 78 | + } |
| 79 | + if (this.urls.size() == 1) { |
| 80 | + Path file = toFile(this.urls.get(0)); |
| 81 | + return new Classpath(file.toString(), List.of(file)); |
| 82 | + } |
| 83 | + List<Path> files = this.urls.stream().map(ClasspathBuilder::toFile).toList(); |
| 84 | + String argument = files.stream().map(Object::toString).collect(Collectors.joining(File.pathSeparator)); |
| 85 | + if (needsClasspathArgFile()) { |
| 86 | + argument = createArgFile(argument); |
| 87 | + } |
| 88 | + return new Classpath(argument, files); |
| 89 | + } |
| 90 | + |
| 91 | + protected boolean needsClasspathArgFile() { |
| 92 | + String os = System.getProperty("os.name"); |
| 93 | + if (!StringUtils.hasText(os)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + // Windows limits the maximum command length, so we use an argfile |
| 97 | + return os.toLowerCase(Locale.ROOT).contains("win"); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Create a temporary file with the given {@code} classpath. Return a suitable |
| 102 | + * argument to load the file, that is the full path prefixed by {@code @}. |
| 103 | + * @param classpath the classpath to use |
| 104 | + * @return a suitable argument for the classpath using a file |
| 105 | + */ |
| 106 | + private String createArgFile(String classpath) { |
| 107 | + try { |
| 108 | + return "@" + writeClasspathToFile(classpath); |
| 109 | + } |
| 110 | + catch (IOException ex) { |
| 111 | + return classpath; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private Path writeClasspathToFile(CharSequence classpath) throws IOException { |
| 116 | + Path tempFile = Files.createTempFile("spring-boot-", ".argfile"); |
| 117 | + tempFile.toFile().deleteOnExit(); |
| 118 | + Files.writeString(tempFile, "\"" + escape(classpath) + "\"", getCharset()); |
| 119 | + return tempFile; |
| 120 | + } |
| 121 | + |
| 122 | + private static Charset getCharset() { |
| 123 | + String nativeEncoding = System.getProperty("native.encoding"); |
| 124 | + if (nativeEncoding == null) { |
| 125 | + return Charset.defaultCharset(); |
| 126 | + } |
| 127 | + try { |
| 128 | + return Charset.forName(nativeEncoding); |
| 129 | + } |
| 130 | + catch (UnsupportedCharsetException ex) { |
| 131 | + return Charset.defaultCharset(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static String escape(CharSequence content) { |
| 136 | + return content.toString().replace("\\", "\\\\"); |
| 137 | + } |
| 138 | + |
| 139 | + private static Path toFile(URL url) { |
| 140 | + try { |
| 141 | + return Paths.get(url.toURI()); |
| 142 | + } |
| 143 | + catch (URISyntaxException ex) { |
| 144 | + throw new IllegalArgumentException(ex); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + static final class Classpath { |
| 149 | + |
| 150 | + private final String argument; |
| 151 | + |
| 152 | + private final List<Path> elements; |
| 153 | + |
| 154 | + private Classpath(String argument, List<Path> elements) { |
| 155 | + this.argument = argument; |
| 156 | + this.elements = elements; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Return the {@code -cp} argument value. |
| 161 | + * @return the argument to use |
| 162 | + */ |
| 163 | + String argument() { |
| 164 | + return this.argument; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Return the classpath elements. |
| 169 | + * @return the JAR files to use |
| 170 | + */ |
| 171 | + Stream<Path> elements() { |
| 172 | + return this.elements.stream(); |
| 173 | + } |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments