Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven bootstrap: resolve workspace for layouts with directory breaks #11804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class BootstrapMavenContext {
private Boolean currentProjectExists;
private DefaultServiceLocator serviceLocator;
private String alternatePomName;
private Path rootProjectDir;

public static BootstrapMavenContextConfig<?> config() {
return new BootstrapMavenContextConfig<>();
Expand All @@ -139,6 +140,7 @@ public BootstrapMavenContext(BootstrapMavenContextConfig<?> config)
this.remoteRepos = config.remoteRepos;
this.remoteRepoManager = config.remoteRepoManager;
this.cliOptions = config.cliOptions;
this.rootProjectDir = config.rootProjectDir;
if (config.currentProject != null) {
this.currentProject = config.currentProject;
this.currentPom = currentProject.getRawModel().getPomFile().toPath();
Expand Down Expand Up @@ -771,6 +773,10 @@ public Path getCurrentProjectBaseDir() {
}

public Path getRootProjectBaseDir() {
return rootProjectDir == null ? rootProjectDir = resolveRootProjectDir() : rootProjectDir;
}

private Path resolveRootProjectDir() {
final String rootBaseDir = System.getenv(MAVEN_PROJECTBASEDIR);
if (rootBaseDir == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class BootstrapMavenContextConfig<T extends BootstrapMavenContextConfig<?
protected File userSettings;
protected boolean artifactTransferLogging = true;
protected BootstrapMavenOptions cliOptions;
protected Path rootProjectDir;

/**
* Local repository location
Expand Down Expand Up @@ -183,6 +184,18 @@ public Path getPomForDirOrNull(Path basedir) {
return BootstrapMavenContext.getPomForDirOrNull(basedir, altPom == null ? null : Paths.get(altPom));
}

/**
* Root project directory.
*
* @param rootProjectDir root project directory
* @return this instance
*/
@SuppressWarnings("unchecked")
public T setRootProjectDir(Path rootProjectDir) {
this.rootProjectDir = rootProjectDir;
return (T) this;
}

private BootstrapMavenOptions getInitializedCliOptions() {
return cliOptions == null ? cliOptions = BootstrapMavenOptions.newInstance() : cliOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private static class WorkspaceLoader {
private final Map<Path, Model> cachedModels = new HashMap<>();
private final Path currentProjectPom;
private Path workspaceRootPom;
// indicates whetehr the workspace root pom has been resolved or provided by the caller
private boolean workspaceRootResolved;

private WorkspaceLoader(Path currentProjectPom) throws BootstrapMavenException {
this.currentProjectPom = isPom(currentProjectPom) ? currentProjectPom
Expand Down Expand Up @@ -73,10 +75,10 @@ void setWorkspaceRootPom(Path rootPom) {
}

private Path getWorkspaceRootPom() throws BootstrapMavenException {
return workspaceRootPom == null ? workspaceRootPom = resolveWorkspaceRootPom() : workspaceRootPom;
return workspaceRootPom == null ? workspaceRootPom = resolveWorkspaceRootPom(false) : workspaceRootPom;
}

private Path resolveWorkspaceRootPom() throws BootstrapMavenException {
private Path resolveWorkspaceRootPom(boolean stopAtCached) throws BootstrapMavenException {
Path rootPom = null;
Path projectPom = currentProjectPom;
Model model = model(projectPom);
Expand Down Expand Up @@ -105,11 +107,15 @@ private Path resolveWorkspaceRootPom() throws BootstrapMavenException {
} else {
// if the parent is not at the top of the FS tree, it might have already been parsed
model = null;
for (Map.Entry<Path, Model> entry : cachedModels.entrySet()) {
// we are looking for the root dir of the workspace
if (rootPom.getNameCount() > entry.getKey().getNameCount()) {
rootPom = entry.getValue().getPomFile().toPath();
if (!stopAtCached) {
for (Map.Entry<Path, Model> entry : cachedModels.entrySet()) {
// we are looking for the root dir of the workspace
if (rootPom.getNameCount() > entry.getKey().getNameCount()) {
rootPom = entry.getValue().getPomFile().toPath();
}
}
// it is supposed to be the root pom
workspaceRootResolved = true;
}
}
}
Expand All @@ -118,10 +124,22 @@ private Path resolveWorkspaceRootPom() throws BootstrapMavenException {
}

LocalProject load() throws BootstrapMavenException {
load(null, getWorkspaceRootPom());
final Path rootPom = getWorkspaceRootPom();
load(null, rootPom);
if (workspace.getCurrentProject() == null) {
if (!currentProjectPom.equals(getWorkspaceRootPom())) {
load(null, currentProjectPom);
if (!currentProjectPom.equals(rootPom)) {
// if the root pom wasn't resolved but provided we are going to try to navigate
// to the very top pom that hasn't already been loaded
if (!workspaceRootResolved) {
final Path resolvedRootPom = resolveWorkspaceRootPom(true);
if (!rootPom.equals(resolvedRootPom)) {
load(null, resolvedRootPom);
}
}
// if the project still wasn't found, we load it directly
if (workspace.getCurrentProject() == null) {
load(null, currentProjectPom);
}
}
if (workspace.getCurrentProject() == null) {
throw new BootstrapMavenException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.fail;

import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalProject;
import io.quarkus.bootstrap.resolver.maven.workspace.LocalWorkspace;
import io.quarkus.bootstrap.util.IoUtils;
Expand Down Expand Up @@ -116,6 +117,27 @@ public static void cleanup() {
IoUtils.recursiveDelete(workDir);
}

@Test
public void loadWorkspaceWithDirBreaks() throws Exception {
final URL projectUrl = Thread.currentThread().getContextClassLoader().getResource("workspace-with-dir-breaks/root");
assertNotNull(projectUrl);
final Path rootProjectDir = Paths.get(projectUrl.toURI());
assertTrue(Files.exists(rootProjectDir));
final Path nestedProjectDir = rootProjectDir.resolve("module1/break/nested-project/module1");
assertTrue(Files.exists(nestedProjectDir));

final LocalWorkspace ws = new BootstrapMavenContext(BootstrapMavenContext.config()
.setRootProjectDir(rootProjectDir)
.setCurrentProject(nestedProjectDir.toString()))
.getWorkspace();

assertNotNull(ws.getProject("org.acme", "nested-project-module1"));
assertNotNull(ws.getProject("org.acme", "nested-project-parent"));
assertNotNull(ws.getProject("org.acme", "root-module1"));
assertNotNull(ws.getProject("org.acme", "root"));
assertEquals(4, ws.getProjects().size());
}

@Test
public void loadWorkspaceFromRootDirWithParentInChildDir() throws Exception {
final URL projectUrl = Thread.currentThread().getContextClassLoader().getResource("workspace-parent-is-not-root-dir");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.acme</groupId>
<artifactId>nested-project-parent</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>

<artifactId>nested-project-module1</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.acme</groupId>
<artifactId>root-module1</artifactId>
<version>1.0</version>
<relativePath/>
</parent>

<artifactId>nested-project-parent</artifactId>
<packaging>pom</packaging>

<modules>
<module>module1</module>
</modules>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.acme</groupId>
<artifactId>root</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>

<artifactId>root-module1</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>org.acme</groupId>
<artifactId>root</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<modules>
<module>module1</module>
</modules>

</project>