Skip to content

Commit 8a3dd9f

Browse files
committed
Use relative paths when possible in the javac.args file that is generated.
apache#193
1 parent 93cf90c commit 8a3dd9f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,25 +1787,47 @@ private void writeDebugFile(
17871787
try (BufferedWriter out = Files.newBufferedWriter(path)) {
17881788
compilerConfiguration.format(commandLine, out);
17891789
for (Map.Entry<PathType, List<Path>> entry : dependencies.entrySet()) {
1790+
List<Path> files = entry.getValue();
1791+
files = files.stream().map(this::relativize).toList();
17901792
String separator = "";
1791-
for (String element : entry.getKey().option(entry.getValue())) {
1793+
for (String element : entry.getKey().option(files)) {
17921794
out.write(separator);
17931795
out.write(element);
17941796
separator = " ";
17951797
}
17961798
out.newLine();
17971799
}
17981800
out.write("-d \"");
1799-
out.write(getOutputDirectory().toString());
1801+
out.write(relativize(getOutputDirectory()).toString());
18001802
out.write('"');
18011803
out.newLine();
18021804
for (SourceFile sf : sourceFiles) {
18031805
out.write('"');
1804-
out.write(sf.file.toString());
1806+
out.write(relativize(sf.file).toString());
18051807
out.write('"');
18061808
out.newLine();
18071809
}
18081810
}
18091811
tipForCommandLineCompilation = commandLine.append(" @").append(path).toString();
18101812
}
1813+
1814+
/**
1815+
* Makes the given file relative to the base directory if the path is inside the project directory tree.
1816+
* The check for the project directory tree (starting from the root of all sub-projects) is for avoiding
1817+
* to relativize the paths to JAR files in the Maven local repository for example.
1818+
*
1819+
* @param file the path to make relative to the base directory
1820+
* @return the given path, potentially relative to the base directory
1821+
*/
1822+
private Path relativize(Path file) {
1823+
Path root = project.getRootDirectory();
1824+
if (root != null && file.startsWith(root)) {
1825+
try {
1826+
file = basedir.relativize(file);
1827+
} catch (IllegalArgumentException e) {
1828+
// Ignore, keep the absolute path.
1829+
}
1830+
}
1831+
return file;
1832+
}
18111833
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ final class SourceDirectory {
5959
static final String CLASS_FILE_SUFFIX = ".class";
6060

6161
/**
62-
* The root directory of all source files.
62+
* The root directory of all source files. Whether the path is relative or absolute depends on the paths given to
63+
* the {@link #fromProject fromProject(…)} or {@link #fromPluginConfiguration fromPluginConfiguration(…)} methods.
64+
* This class preserves the relative/absolute characteristic of the user-specified directories in order to behave
65+
* as intended by users in operations such as {@linkplain Path#relativize relativization}, especially in regard of
66+
* symbolic links. In practice, this path is often an absolute path.
6367
*/
6468
final Path root;
6569

0 commit comments

Comments
 (0)