[MNG-7795] IllegalArgumentException: 'other' has different root during plugin validation (#1133)

Checks the paths before relativizing them.
Normalize and relative before adding to result
Rename local vars
Apply to ExecutionEventLogger

---

https://issues.apache.org/jira/browse/MNG-7795

Co-authored-by: Andreas Dangel <adangel@apache.org>
This commit is contained in:
Guillaume Nodet 2023-06-01 19:05:48 +02:00 committed by GitHub
parent b430e7d826
commit b2953c52d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 13 deletions

View File

@ -22,6 +22,8 @@ import javax.inject.Named;
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;
@ -261,16 +263,12 @@ public final class DefaultPluginValidationManager extends AbstractEventSpy imple
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());
@ -285,8 +283,12 @@ public final class DefaultPluginValidationManager extends AbstractEventSpy imple
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;
}

View File

@ -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 class ExecutionEventLogger extends AbstractExecutionListener {
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 ]----------

View File

@ -80,6 +80,7 @@ class ExecutionEventLoggerTest {
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 @@ class ExecutionEventLoggerTest {
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);