Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this comment in together with the if statement.
The comment says that dir should never be null, but it was observed, so I would assume the if handles the one path and an else path the rare, but observed, path if it is. But I don't see an else to handle this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basedir is a field declared in this class as below:

@Parameter(defaultValue = "${project.basedir}", required = true, readonly = true)
protected Path basedir;

A value should always be injected by Maven core. But during the development of this plugin, sometime when upgrading from a Maven version to the next one, or when using a snapshot version compiled from master, the value was not supplied anymore by Maven core. For example, a similar issue can be reproduced with apache/maven#11441 (I don't know if it was the same cause).

Even if a null basedir may be an indication of a serious problem that we want to be aware of, in this particular context, basedir is used only for making a path relative when creating a debug file. This code is executed only if the build failed (or was executed with --verbose), so we probably don't want the original cause to be hidden by a NullPointerException while writing debug information.

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 == '\\');
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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
Expand All @@ -1850,11 +1861,17 @@ private void writeDebugFile(final ToolExecutor executor, final Options configura
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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/maven/plugin/compiler/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Optional<String> option() {
*/
@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 + '=' : "", "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the quotes only got removed, but not replaced. Is this correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it needs to be consistent with apache/maven#11435.

paths.forEach((path) -> joiner.add(path.toString()));
return new String[] {option().get(), joiner.toString()};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
Expand Down