From 57dfc26b40d74b8d175008be2ce21eb54a0a8d69 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Thu, 22 Feb 2024 23:01:39 +0100 Subject: [PATCH] Path.relativize() may throw exception if source and build directories are on different Windows drives Closes #364 --- .../plexus/compiler/AbstractCompiler.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/plexus-compiler-api/src/main/java/org/codehaus/plexus/compiler/AbstractCompiler.java b/plexus-compiler-api/src/main/java/org/codehaus/plexus/compiler/AbstractCompiler.java index 7a3a4a52..0acb4694 100644 --- a/plexus-compiler-api/src/main/java/org/codehaus/plexus/compiler/AbstractCompiler.java +++ b/plexus-compiler-api/src/main/java/org/codehaus/plexus/compiler/AbstractCompiler.java @@ -268,19 +268,31 @@ private static String getCanonicalPath(File origFile) throws CompilerException { protected void logCompiling(String[] sourceFiles, CompilerConfiguration config) { if (log.isInfoEnabled()) { - String to = (config.getWorkingDirectory() == null) - ? config.getOutputLocation() - : config.getWorkingDirectory() - .toPath() - .relativize(new File(config.getOutputLocation()).toPath()) - .toString(); log.info("Compiling " + (sourceFiles == null ? "" : (sourceFiles.length + " source file" + (sourceFiles.length == 1 ? " " : "s "))) + "with " + getCompilerId() + " [" + config.describe() + "]" + " to " - + to); + + getRelativeWorkingDirectory(config)); } } + + private static String getRelativeWorkingDirectory(CompilerConfiguration config) { + String to; + if (config.getWorkingDirectory() == null) { + to = config.getOutputLocation(); + } else { + try { + to = config.getWorkingDirectory() + .toPath() + .relativize(new File(config.getOutputLocation()).toPath()) + .toString(); + } catch (IllegalArgumentException e) { + // may happen on Windows if the working directory is on a different drive + to = config.getOutputLocation(); + } + } + return to; + } }