Skip to content

Commit 1271c85

Browse files
committed
Use relative paths in compiler error and warning messages, for shorter and more readable output.
Give a hint when a change of directory is needed for using the relative paths.
1 parent 5d4d7f3 commit 1271c85

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.io.BufferedReader;
3131
import java.io.BufferedWriter;
32+
import java.io.File;
3233
import java.io.IOException;
3334
import java.io.InputStream;
3435
import java.io.StreamTokenizer;
@@ -1600,10 +1601,18 @@ private void writeDebugFile(final ToolExecutor executor, final Options configura
16001601
logger.warn("The <debugFileName> parameter should not be empty.");
16011602
return;
16021603
}
1603-
final var commandLine = new StringBuilder("For trying to compile from the command-line, use:")
1604-
.append(System.lineSeparator())
1605-
.append(" ")
1606-
.append(executable != null ? executable : compilerId);
1604+
final var commandLine = new StringBuilder("For trying to compile from the command-line, use:");
1605+
final var chdir =
1606+
Path.of(System.getProperty("user.dir")).relativize(basedir).toString();
1607+
if (!chdir.isEmpty()) {
1608+
boolean isWindows = (File.separatorChar == '\\');
1609+
commandLine
1610+
.append(System.lineSeparator())
1611+
.append(" ")
1612+
.append(isWindows ? "chdir " : "cd ")
1613+
.append(chdir);
1614+
}
1615+
commandLine.append(System.lineSeparator()).append(" ").append(executable != null ? executable : compilerId);
16071616
try (BufferedWriter out = Files.newBufferedWriter(path)) {
16081617
configuration.format(commandLine, out);
16091618
for (Map.Entry<PathType, List<Path>> entry : executor.dependencies.entrySet()) {

src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.tools.DiagnosticListener;
2323
import javax.tools.JavaFileObject;
2424

25+
import java.nio.file.Path;
2526
import java.util.Arrays;
2627
import java.util.LinkedHashMap;
2728
import java.util.Locale;
@@ -53,6 +54,11 @@ final class DiagnosticLogger implements DiagnosticListener<JavaFileObject> {
5354
*/
5455
private final Locale locale;
5556

57+
/**
58+
* The base directory with which to relativize the paths to source files.
59+
*/
60+
private final Path directory;
61+
5662
/**
5763
* Number of errors or warnings.
5864
*/
@@ -74,14 +80,31 @@ final class DiagnosticLogger implements DiagnosticListener<JavaFileObject> {
7480
* @param logger the logger where to send diagnostics
7581
* @param messageBuilderFactory the factory for creating message builders
7682
* @param locale the locale for compiler message
83+
* @param directory the base directory with which to relativize the paths to source files
7784
*/
78-
DiagnosticLogger(Log logger, MessageBuilderFactory messageBuilderFactory, Locale locale) {
85+
DiagnosticLogger(Log logger, MessageBuilderFactory messageBuilderFactory, Locale locale, Path directory) {
7986
this.logger = logger;
8087
this.messageBuilderFactory = messageBuilderFactory;
8188
this.locale = locale;
89+
this.directory = directory;
8290
codeCount = new LinkedHashMap<>();
8391
}
8492

93+
/**
94+
* Makes the given file relative to the base directory.
95+
*
96+
* @param file the path to make relative to the base directory
97+
* @return the given path, potentially relative to the base directory
98+
*/
99+
private String relativize(String file) {
100+
try {
101+
return directory.relativize(Path.of(file)).toString();
102+
} catch (IllegalArgumentException e) {
103+
// Ignore, keep the absolute path.
104+
return file;
105+
}
106+
}
107+
85108
/**
86109
* Invoked when the compiler emitted a warning.
87110
*
@@ -95,6 +118,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
95118
}
96119
MessageBuilder record = messageBuilderFactory.builder();
97120
record.a(message);
121+
JavaFileObject source = diagnostic.getSource();
98122
Diagnostic.Kind kind = diagnostic.getKind();
99123
String style;
100124
switch (kind) {
@@ -107,11 +131,13 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
107131
break;
108132
default:
109133
style = ".info:-bold,f:blue";
134+
if (diagnostic.getLineNumber() == Diagnostic.NOPOS) {
135+
source = null; // Some messages are generic, e.g. "Recompile with -Xlint:deprecation".
136+
}
110137
break;
111138
}
112-
JavaFileObject source = diagnostic.getSource();
113139
if (source != null) {
114-
record.newline().a(" at ").a(source.getName());
140+
record.newline().a(" at ").a(relativize(source.getName()));
115141
long line = diagnostic.getLineNumber();
116142
long column = diagnostic.getColumnNumber();
117143
if (line != Diagnostic.NOPOS || column != Diagnostic.NOPOS) {

src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ protected ToolExecutor(final AbstractCompilerMojo mojo, DiagnosticListener<? sup
204204

205205
logger = mojo.logger;
206206
if (listener == null) {
207-
listener = new DiagnosticLogger(logger, mojo.messageBuilderFactory, LOCALE);
207+
listener =
208+
new DiagnosticLogger(logger, mojo.messageBuilderFactory, LOCALE, mojo.project.getRootDirectory());
208209
}
209210
this.listener = listener;
210211
encoding = mojo.charset();

0 commit comments

Comments
 (0)