resolve path

This commit is contained in:
2026-07-06 22:01:41 +02:00
parent 31adae0a4b
commit 4f8c35adef
2 changed files with 70 additions and 0 deletions

View File

@@ -66,6 +66,23 @@ public class SiblingDependencyResolver {
for (String gradlePath : gradlePaths) {
String relativePath = gradlePath.replace(':', '/');
Path resolvedSibling = rootDir.resolve(relativePath).normalize();
// Check for explicit projectDir mapping in settings.gradle or settings.gradle.kts
Path settingsFile = rootDir.resolve("settings.gradle");
if (!Files.exists(settingsFile)) {
settingsFile = rootDir.resolve("settings.gradle.kts");
}
if (Files.exists(settingsFile)) {
String settingsContent = Files.readString(settingsFile);
String regex = "project\\(['\"]:" + gradlePath + "['\"]\\)\\.projectDir\\s*=\\s*(?:file\\(['\"](.*?)['\"]\\)|new File\\(.*?,\\s*['\"](.*?)['\"]\\))";
Matcher settingsMatcher = Pattern.compile(regex).matcher(settingsContent);
if (settingsMatcher.find()) {
String customPath = settingsMatcher.group(1) != null ? settingsMatcher.group(1) : settingsMatcher.group(2);
resolvedSibling = rootDir.resolve(customPath).normalize();
log.info("Found custom projectDir in settings for {}: {}", gradlePath, resolvedSibling);
}
}
if (Files.exists(resolvedSibling)) {
log.info("Discovered local Gradle sibling dependency: {}", resolvedSibling);
siblings.add(resolvedSibling);
@@ -156,6 +173,19 @@ public class SiblingDependencyResolver {
// Also add parent dir if it has a pom.xml
Path parentDir = projectDir.getParent();
// Check for explicit <relativePath> in parent block
Matcher parentRelativePathMatcher = Pattern.compile("<parent>.*?<relativePath>(.*?)</relativePath>.*?</parent>", Pattern.DOTALL).matcher(content);
if (parentRelativePathMatcher.find()) {
String relPathStr = parentRelativePathMatcher.group(1).trim();
Path relPath = projectDir.resolve(relPathStr).normalize();
if (Files.isRegularFile(relPath) && relPath.getFileName().toString().equals("pom.xml")) {
parentDir = relPath.getParent();
} else if (Files.isDirectory(relPath)) {
parentDir = relPath;
}
}
if (parentDir != null && Files.exists(parentDir.resolve("pom.xml")) && !parentDir.equals(projectDir)) {
log.info("Discovered local Maven parent sibling: {}", parentDir);
siblings.add(parentDir);

View File

@@ -0,0 +1,40 @@
package click.kamil.springstatemachineexporter.analysis.resolver;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class RelativePathTest {
@Test
public void testRelativePathDirectory(@TempDir Path tempDir) throws Exception {
Path rootDir = tempDir.resolve("root");
Path childDir = tempDir.resolve("child");
Files.createDirectories(rootDir);
Files.createDirectories(childDir);
Files.writeString(rootDir.resolve("pom.xml"), "<project><groupId>com.acme</groupId><artifactId>root</artifactId></project>");
Files.writeString(childDir.resolve("pom.xml"), "<project><parent><relativePath>../root</relativePath></parent></project>");
SiblingDependencyResolver resolver = new SiblingDependencyResolver();
Set<Path> siblings = resolver.resolveSiblings(childDir);
assertTrue(siblings.contains(rootDir), "Should resolve parent directory from <relativePath>");
}
@Test
public void testRelativePathFile(@TempDir Path tempDir) throws Exception {
Path rootDir = tempDir.resolve("root");
Path childDir = tempDir.resolve("child");
Files.createDirectories(rootDir);
Files.createDirectories(childDir);
Files.writeString(rootDir.resolve("pom.xml"), "<project><groupId>com.acme</groupId><artifactId>root</artifactId></project>");
Files.writeString(childDir.resolve("pom.xml"), "<project><parent><relativePath>../root/pom.xml</relativePath></parent></project>");
SiblingDependencyResolver resolver = new SiblingDependencyResolver();
Set<Path> siblings = resolver.resolveSiblings(childDir);
assertTrue(siblings.contains(rootDir), "Should resolve parent directory from <relativePath> file");
}
}