This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new 7e3e48f Quote filenames only when formatting a command for the shell
(#997)
7e3e48f is described below
commit 7e3e48f0964032ff721466e98a69701e1928a9af
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Thu Nov 20 16:15:29 2025 +0100
Quote filenames only when formatting a command for the shell (#997)
Quotes should not be added by `JavaPathType` but by this plugin instead,
because whether we want quotes depends on whether the filenames will
be used as arguments for a Java tools API or written in a shell command.
---
.../plugin/compiler/AbstractCompilerMojo.java | 37 ++++++++++++++++------
.../org/apache/maven/plugin/compiler/Options.java | 4 +--
.../maven/plugin/compiler/SourcePathType.java | 2 +-
.../plugin/compiler/WorkaroundForPatchModule.java | 2 +-
4 files changed, 31 insertions(+), 14 deletions(-)
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 60473bc..d52aff1 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 @@ public abstract class AbstractCompilerMojo implements
Mojo {
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 @@ public abstract class AbstractCompilerMojo implements
Mojo {
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 @@ public abstract class AbstractCompilerMojo implements
Mojo {
/**
* 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 @@ public abstract class AbstractCompilerMojo implements
Mojo {
private void writeOption(BufferedWriter out, PathType type,
Collection<Path> 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 25d5801..74e664e 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 @@ public final class Options {
}
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 8231d72..2a05cbe 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 @@ final class SourcePathType implements PathType {
*/
@Override
public String[] option(Iterable<? extends Path> 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 1f62ae4..88f8b04 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 @@ final class WorkaroundForPatchModule extends
ForwardingJavaFileManager<StandardJ
/**
* 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
<abbr>API</abbr>.
* 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