From 41bb24b8254dbe3ad75120f3f197a2d1aaffe6f6 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 26 May 2023 10:42:41 +0200 Subject: [PATCH 1/2] [MNG-7795] IllegalArgumentException: 'other' has different root during plugin validation Checks the paths before relativizing them. --- .../DefaultPluginValidationManager.java | 24 ++++++++++--------- .../maven/cli/event/ExecutionEventLogger.java | 9 +++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginValidationManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginValidationManager.java index 655535c581ae..818509c1b31a 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginValidationManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultPluginValidationManager.java @@ -22,6 +22,8 @@ import javax.inject.Singleton; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -251,16 +253,12 @@ private String pluginDeclaration(MavenSession mavenSession, MojoDescriptor mojoD if (location.contains("://")) { stringBuilder.append(" (").append(location).append(")"); } else { - File rootBasedir = mavenSession.getTopLevelProject().getBasedir(); - File locationFile = new File(location); - if (location.startsWith(rootBasedir.getPath())) { - stringBuilder - .append(" (") - .append(rootBasedir.toPath().relativize(locationFile.toPath())) - .append(")"); - } else { - stringBuilder.append(" (").append(location).append(")"); + Path topDirectory = mavenSession.getTopDirectory(); + Path locationPath = Paths.get(location).toAbsolutePath().normalize(); + if (locationPath.startsWith(topDirectory)) { + locationPath = topDirectory.relativize(locationPath); } + stringBuilder.append(" (").append(locationPath).append(")"); } } stringBuilder.append(" @ line ").append(inputLocation.getLineNumber()); @@ -275,8 +273,12 @@ private String pluginOccurrence(MavenSession mavenSession) { String result = prj.getGroupId() + ":" + prj.getArtifactId() + ":" + prj.getVersion(); File currentPom = prj.getFile(); if (currentPom != null) { - File rootBasedir = mavenSession.getTopLevelProject().getBasedir(); - result += " (" + rootBasedir.toPath().relativize(currentPom.toPath()) + ")"; + Path topDirectory = mavenSession.getTopDirectory(); + Path current = currentPom.toPath().toAbsolutePath().normalize(); + if (current.startsWith(topDirectory)) { + current = topDirectory.relativize(current); + } + result += " (" + current + ")"; } return result; } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java b/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java index ca5554339fb2..ae591850be57 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/event/ExecutionEventLogger.java @@ -19,6 +19,7 @@ package org.apache.maven.cli.event; import java.io.File; +import java.nio.file.Path; import java.util.List; import java.util.Objects; @@ -336,8 +337,12 @@ public void projectStarted(ExecutionEvent event) { File currentPom = project.getFile(); if (currentPom != null) { MavenSession session = event.getSession(); - File rootBasedir = session.getTopLevelProject().getBasedir(); - logger.info(" from " + rootBasedir.toPath().relativize(currentPom.toPath())); + Path topDirectory = session.getTopDirectory(); + Path current = currentPom.toPath().toAbsolutePath().normalize(); + if (current.startsWith(topDirectory)) { + current = topDirectory.relativize(current); + } + logger.info(" from " + current); } // ----------[ packaging ]---------- From a1e1f596ed2a978e94596eca8f34925840ea7988 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 31 May 2023 09:48:52 +0200 Subject: [PATCH 2/2] Fix test --- .../org/apache/maven/cli/event/ExecutionEventLoggerTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java index 7e830f45c97c..59b8dc282a0f 100644 --- a/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/cli/event/ExecutionEventLoggerTest.java @@ -80,6 +80,7 @@ void testProjectStarted() { when(rootProject.getBasedir()).thenReturn(basedir); MavenSession session = mock(MavenSession.class); when(session.getTopLevelProject()).thenReturn(rootProject); + when(session.getTopDirectory()).thenReturn(basedir.toPath()); when(event.getSession()).thenReturn(session); // execute @@ -112,6 +113,7 @@ void testProjectStartedOverflow() { MavenSession session = mock(MavenSession.class); when(session.getTopLevelProject()).thenReturn(project); when(event.getSession()).thenReturn(session); + when(session.getTopDirectory()).thenReturn(basedir.toPath()); // execute executionEventLogger.projectStarted(event);