diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java index 60473bcbb..d52aff13b 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -124,6 +124,12 @@ public abstract class AbstractCompilerMojo implements Mojo { */ private static final String DEFAULT_EXECUTABLE = "javac"; + /** + * The quote character for filenames in shell scripts. + * Shall not be used with {@link javax.tools.JavaFileManager}. + */ + static final char QUOTE = '"'; + // ---------------------------------------------------------------------- // Configurables // ---------------------------------------------------------------------- @@ -1775,7 +1781,11 @@ private void writeDebugFile(final ToolExecutor executor, final Options configura final var commandLine = new StringBuilder("For trying to compile from the command-line, use:"); Path dir = basedir; if (dir != null) { // Should never be null, but it has been observed with some Maven versions. - dir = Path.of(System.getProperty("user.dir")).relativize(dir); + try { + dir = Path.of(System.getProperty("user.dir")).relativize(dir); + } catch (IllegalArgumentException e) { + // Ignore, keep the absolute path. + } String chdir = dir.toString(); if (!chdir.isEmpty()) { boolean isWindows = (File.separatorChar == '\\'); @@ -1823,14 +1833,14 @@ private void writeDebugFile(final ToolExecutor executor, final Options configura String moduleName = root.getKey(); writeOption(out, SourcePathType.valueOf(moduleName), root.getValue()); } - out.write("-d \""); + out.write("-d " + QUOTE); out.write(relativize(sources.outputForRelease).toString()); - out.write('"'); + out.write(QUOTE); out.newLine(); for (final Path file : sources.files) { - out.write('"'); + out.write(QUOTE); out.write(relativize(file).toString()); - out.write('"'); + out.write(QUOTE); out.newLine(); } } @@ -1841,6 +1851,7 @@ private void writeDebugFile(final ToolExecutor executor, final Options configura /** * Writes the paths for the given Java compiler option. + * Used for the {@code *.args} debug file, because files will be written between quotes. * * @param out where to write * @param type the type of path to write as a compiler option @@ -1850,11 +1861,17 @@ private void writeDebugFile(final ToolExecutor executor, final Options configura private void writeOption(BufferedWriter out, PathType type, Collection files) throws IOException { if (!files.isEmpty()) { files = files.stream().map(this::relativize).toList(); - String separator = ""; - for (String element : type.option(files)) { - out.write(separator); - out.write(element); - separator = " "; + String[] options = type.option(files); + for (int i = 0; i < options.length; i++) { + String element = options[i]; + if (i == 0) { + out.write(element); + } else { + out.write(' '); + out.write(QUOTE); + out.write(element); + out.write(QUOTE); + } } out.newLine(); } diff --git a/src/main/java/org/apache/maven/plugin/compiler/Options.java b/src/main/java/org/apache/maven/plugin/compiler/Options.java index 25d580156..74e664e57 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/Options.java +++ b/src/main/java/org/apache/maven/plugin/compiler/Options.java @@ -404,11 +404,11 @@ void format(final StringBuilder commandLine, final Appendable out) throws IOExce } boolean needsQuote = option.indexOf(' ') >= 0; if (needsQuote) { - out.append('"'); + out.append(AbstractCompilerMojo.QUOTE); } out.append(option); if (needsQuote) { - out.append('"'); + out.append(AbstractCompilerMojo.QUOTE); } hasOptions = true; } diff --git a/src/main/java/org/apache/maven/plugin/compiler/SourcePathType.java b/src/main/java/org/apache/maven/plugin/compiler/SourcePathType.java index 8231d72a8..2a05cbee8 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/SourcePathType.java +++ b/src/main/java/org/apache/maven/plugin/compiler/SourcePathType.java @@ -102,7 +102,7 @@ public Optional option() { */ @Override public String[] option(Iterable paths) { - var joiner = new StringJoiner(File.pathSeparator, (moduleName != null) ? moduleName + "=\"" : "\"", "\""); + var joiner = new StringJoiner(File.pathSeparator, (moduleName != null) ? moduleName + '=' : "", ""); paths.forEach((path) -> joiner.add(path.toString())); return new String[] {option().get(), joiner.toString()}; } diff --git a/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java b/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java index 1f62ae4d8..88f8b04df 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java +++ b/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java @@ -120,7 +120,7 @@ void copyTo(final StandardJavaFileManager target) throws IOException { /** * Sets a module path by asking the file manager to parse an option formatted by this method. - * Invoked when a module path cannot be specified through the API + * Invoked when a module path cannot be specified through the standard API. * This is the workaround described in class Javadoc. * * @param fileManager the file manager on which an attempt to set the location has been made and failed