Skip to content

Commit 6be7a12

Browse files
authored
[MNG-8720] Fix for symlinked project directory (#2289)
Applied proposed fix and a bit more. Checked with provided reproducer at https://github.com/hho/MNG-8720 Thanks! --- https://issues.apache.org/jira/browse/MNG-8720
1 parent 4ac3b14 commit 6be7a12

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/model/rootlocator/DefaultRootLocator.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
*/
1919
package org.apache.maven.impl.model.rootlocator;
2020

21+
import java.io.IOException;
22+
import java.nio.file.Files;
2123
import java.nio.file.Path;
2224
import java.nio.file.Paths;
2325
import java.util.List;
24-
import java.util.Objects;
2526
import java.util.Optional;
2627
import java.util.ServiceLoader;
2728

@@ -47,7 +48,7 @@ public DefaultRootLocator() {
4748
}
4849

4950
@Override
50-
public Path findRoot(Path basedir) {
51+
public Path findRoot(@Nonnull Path basedir) {
5152
requireNonNull(basedir, getNoRootMessage());
5253
Path rootDirectory = basedir;
5354
while (rootDirectory != null && !isRootDirectory(rootDirectory)) {
@@ -64,8 +65,14 @@ public Path findMandatoryRoot(@Nonnull Path basedir) {
6465
rootDirectory = rdf.orElseThrow(() -> new IllegalStateException(getNoRootMessage()));
6566
logger.warn(getNoRootMessage());
6667
} else {
67-
if (rdf.isPresent() && !Objects.equals(rootDirectory, rdf.get())) {
68-
logger.warn("Project root directory and multiModuleProjectDirectory are not aligned");
68+
if (rdf.isPresent()) {
69+
try {
70+
if (!Files.isSameFile(rootDirectory, rdf.orElseThrow())) {
71+
logger.warn("Project root directory and multiModuleProjectDirectory are not aligned");
72+
}
73+
} catch (IOException e) {
74+
throw new IllegalStateException("findMandatoryRoot failed", e);
75+
}
6976
}
7077
}
7178
return rootDirectory;
@@ -84,8 +91,16 @@ protected boolean isRootDirectory(Path dir) {
8491
protected Optional<Path> getRootDirectoryFallback() {
8592
String mmpd = System.getProperty("maven.multiModuleProjectDirectory");
8693
if (mmpd != null) {
87-
return Optional.of(Paths.get(mmpd));
94+
return Optional.of(getCanonicalPath(Paths.get(mmpd)));
8895
}
8996
return Optional.empty();
9097
}
98+
99+
protected Path getCanonicalPath(Path path) {
100+
try {
101+
return path.toRealPath();
102+
} catch (IOException e) {
103+
return getCanonicalPath(path.getParent()).resolve(path.getFileName());
104+
}
105+
}
91106
}

0 commit comments

Comments
 (0)