From 1cff5947ac7281e40f992f11549588ad004a36b6 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 16 Oct 2024 16:22:26 +0200 Subject: [PATCH] Revert "Make rootDirectory really mandatory" (#1800) This reverts commit 608a99fb08cf7fabcc73c0691ff0b35bd5f326ed. --- https://issues.apache.org/jira/browse/MNG-8302 --- .../java/org/apache/maven/api/Session.java | 1 + .../impl/model/DefaultModelBuilder.java | 25 ++++++++++++++++--- .../model/rootlocator/DefaultRootLocator.java | 6 ++--- .../internal/impl/standalone/ApiRunner.java | 5 ++-- .../AbstractCoreMavenComponentTestCase.java | 3 +-- .../project/AbstractMavenProjectTestCase.java | 2 +- .../LegacyRepositorySystemTest.java | 2 +- .../DefaultMavenExecutionRequest.java | 7 ------ .../AbstractCoreMavenComponentTestCase.java | 4 +-- .../org/apache/maven/MavenTestHelper.java | 2 +- .../apache/maven/internal/impl/TestApi.java | 2 +- .../project/AbstractMavenProjectTestCase.java | 2 +- 12 files changed, 35 insertions(+), 26 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java index 48b5ca0cc7..150cd7ea71 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/Session.java @@ -158,6 +158,7 @@ public interface Session { * Gets the root directory of the session, which is the root directory for the top directory project. * * @return the root directory, never {@code null} + * @throws IllegalStateException if the root directory could not be found * @see #getTopDirectory() * @see Project#getRootDirectory() * @see Project#isRootProject() diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java index bb1a7e23b7..9a13ca9627 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java @@ -105,6 +105,7 @@ import org.apache.maven.api.services.model.PluginManagementInjector; import org.apache.maven.api.services.model.ProfileActivationContext; import org.apache.maven.api.services.model.ProfileInjector; import org.apache.maven.api.services.model.ProfileSelector; +import org.apache.maven.api.services.model.RootLocator; import org.apache.maven.api.services.xml.XmlReaderException; import org.apache.maven.api.services.xml.XmlReaderRequest; import org.apache.maven.api.spi.ModelParserException; @@ -631,7 +632,12 @@ public class DefaultModelBuilder implements ModelBuilder { top = top.toAbsolutePath().normalize(); // Obtain the root directory, resolving it if necessary - Path rootDirectory = session.getRootDirectory(); + Path rootDirectory; + try { + rootDirectory = session.getRootDirectory(); + } catch (IllegalStateException e) { + rootDirectory = session.getService(RootLocator.class).findMandatoryRoot(top); + } // Locate and normalize the root POM if it exists, fallback to top otherwise Path root = modelProcessor.locateExistingPom(rootDirectory); @@ -1232,11 +1238,19 @@ public class DefaultModelBuilder implements ModelBuilder { Model doReadFileModel() throws ModelBuilderException { ModelSource modelSource = request.getSource(); Model model; - Path rootDirectory = request.getSession().getRootDirectory(); + Path rootDirectory; setSource(modelSource.getLocation()); logger.debug("Reading file model from " + modelSource.getLocation()); try { boolean strict = request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM; + try { + rootDirectory = request.getSession().getRootDirectory(); + } catch (IllegalStateException ignore) { + rootDirectory = modelSource.getPath(); + while (rootDirectory != null && !Files.isDirectory(rootDirectory)) { + rootDirectory = rootDirectory.getParent(); + } + } try (InputStream is = modelSource.openStream()) { model = modelProcessor.read(XmlReaderRequest.builder() .strict(strict) @@ -1647,7 +1661,12 @@ public class DefaultModelBuilder implements ModelBuilder { return null; } - Path rootDirectory = request.getSession().getRootDirectory(); + Path rootDirectory; + try { + rootDirectory = request.getSession().getRootDirectory(); + } catch (IllegalStateException e) { + rootDirectory = null; + } if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM && rootDirectory != null) { Path sourcePath = importSource.getPath(); if (sourcePath != null && sourcePath.startsWith(rootDirectory)) { diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/rootlocator/DefaultRootLocator.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/rootlocator/DefaultRootLocator.java index f43c050a94..19d514b84d 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/rootlocator/DefaultRootLocator.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/rootlocator/DefaultRootLocator.java @@ -53,10 +53,10 @@ public class DefaultRootLocator implements RootLocator { while (rootDirectory != null && !isRootDirectory(rootDirectory)) { rootDirectory = rootDirectory.getParent(); } - Optional rdf = getMultiModuleProjectDirectory(); + Optional rdf = getRootDirectoryFallback(); if (rootDirectory == null) { + rootDirectory = rdf.orElseThrow(() -> new IllegalStateException(getNoRootMessage())); logger.warn(getNoRootMessage()); - rootDirectory = rdf.orElseGet(() -> Paths.get("").toAbsolutePath()); } else { if (rdf.isPresent() && !Objects.equals(rootDirectory, rdf.get())) { logger.warn("Project root directory and multiModuleProjectDirectory are not aligned"); @@ -75,7 +75,7 @@ public class DefaultRootLocator implements RootLocator { return false; } - protected Optional getMultiModuleProjectDirectory() { + protected Optional getRootDirectoryFallback() { String mmpd = System.getProperty("maven.multiModuleProjectDirectory"); if (mmpd != null) { return Optional.of(Paths.get(mmpd)); diff --git a/maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/ApiRunner.java b/maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/ApiRunner.java index e9eba6f5ef..8b55406214 100644 --- a/maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/ApiRunner.java +++ b/maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/ApiRunner.java @@ -52,7 +52,6 @@ import org.apache.maven.api.services.PackagingRegistry; import org.apache.maven.api.services.RepositoryFactory; import org.apache.maven.api.services.SettingsBuilder; import org.apache.maven.api.services.TypeRegistry; -import org.apache.maven.api.services.model.RootLocator; import org.apache.maven.api.settings.Settings; import org.apache.maven.api.spi.TypeProvider; import org.apache.maven.di.Injector; @@ -156,12 +155,12 @@ public class ApiRunner { @Override public Path getTopDirectory() { - return Paths.get(""); + return null; } @Override public Path getRootDirectory() { - return getService(RootLocator.class).findMandatoryRoot(getTopDirectory()); + throw new IllegalStateException(); } @Override diff --git a/maven-compat/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java b/maven-compat/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java index 0df571dc0e..0201b2fd0a 100644 --- a/maven-compat/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java +++ b/maven-compat/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java @@ -90,7 +90,7 @@ public abstract class AbstractCoreMavenComponentTestCase { } protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception { - MavenExecutionRequest request = new DefaultMavenExecutionRequest(true) + MavenExecutionRequest request = new DefaultMavenExecutionRequest() .setPom(pom) .setProjectPresent(true) .setShowErrors(true) @@ -102,7 +102,6 @@ public abstract class AbstractCoreMavenComponentTestCase { if (pom != null) { request.setMultiModuleProjectDirectory(pom.getParentFile()); - request.setRootDirectory(pom.getParentFile().toPath()); } return request; diff --git a/maven-compat/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java b/maven-compat/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java index 0d7c5f21a9..fffdbb85c6 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java +++ b/maven-compat/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java @@ -161,7 +161,7 @@ public abstract class AbstractMavenProjectTestCase { session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo)); request.setRepositorySession(session); - DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(true); + DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); MavenSession msession = new MavenSession(getContainer(), session, mavenExecutionRequest, new DefaultMavenExecutionResult()); DefaultSession iSession = new DefaultSession( diff --git a/maven-compat/src/test/java/org/apache/maven/repository/LegacyRepositorySystemTest.java b/maven-compat/src/test/java/org/apache/maven/repository/LegacyRepositorySystemTest.java index b1dc38a054..5feff15253 100644 --- a/maven-compat/src/test/java/org/apache/maven/repository/LegacyRepositorySystemTest.java +++ b/maven-compat/src/test/java/org/apache/maven/repository/LegacyRepositorySystemTest.java @@ -123,7 +123,7 @@ class LegacyRepositorySystemTest { new LocalRepository(request.getLocalRepository().getBasedir()); session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo)); LegacySupport legacySupport = container.lookup(LegacySupport.class); - DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(true); + DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); MavenSession mavenSession = new MavenSession(container, session, mavenExecutionRequest, new DefaultMavenExecutionResult()); legacySupport.setSession(mavenSession); diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java index 1d715fcfcd..aa9748ca51 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java @@ -20,7 +20,6 @@ package org.apache.maven.execution; import java.io.File; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -173,12 +172,6 @@ public class DefaultMavenExecutionRequest implements MavenExecutionRequest { public DefaultMavenExecutionRequest() {} - public DefaultMavenExecutionRequest(boolean withDefaultRoot) { - if (withDefaultRoot) { - setRootDirectory(Paths.get("").toAbsolutePath()); - } - } - public static MavenExecutionRequest copy(MavenExecutionRequest original) { DefaultMavenExecutionRequest copy = new DefaultMavenExecutionRequest(); copy.setLocalRepository(original.getLocalRepository()); diff --git a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java index e8f8433b47..8484b60200 100644 --- a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java @@ -21,7 +21,6 @@ package org.apache.maven; import javax.inject.Inject; import java.io.File; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -91,8 +90,7 @@ public abstract class AbstractCoreMavenComponentTestCase { protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception { MavenExecutionRequest request = new DefaultMavenExecutionRequest() - .setRootDirectory( - pom != null ? pom.toPath().getParent() : Paths.get("").toAbsolutePath()) + .setRootDirectory(pom != null ? pom.toPath().getParent() : null) .setPom(pom) .setProjectPresent(true) .setShowErrors(true) diff --git a/maven-core/src/test/java/org/apache/maven/MavenTestHelper.java b/maven-core/src/test/java/org/apache/maven/MavenTestHelper.java index 8020cf7e12..358ab86ebe 100644 --- a/maven-core/src/test/java/org/apache/maven/MavenTestHelper.java +++ b/maven-core/src/test/java/org/apache/maven/MavenTestHelper.java @@ -32,7 +32,7 @@ public class MavenTestHelper { public static DefaultRepositorySystemSession createSession( MavenRepositorySystem repositorySystem, PlexusContainer container) { DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false); - DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest(true); + DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest(); MavenSession mavenSession = new MavenSession(repoSession, request, new DefaultMavenExecutionResult()); DefaultSession session = new DefaultSession(mavenSession, null, null, repositorySystem, new DefaultLookup(container), null); diff --git a/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java b/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java index 7f9177177e..a176e3e520 100644 --- a/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java +++ b/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java @@ -119,7 +119,7 @@ class TestApi { .get() .withLocalRepositoryBaseDirectories(new File("target").toPath()) .build(); - DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(true); + DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(); DefaultMavenExecutionResult meres = new DefaultMavenExecutionResult(); MavenSession ms = new MavenSession(rss, mer, meres); DefaultSession session = new DefaultSession( diff --git a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java index 35cec20d4a..652479a879 100644 --- a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java @@ -164,7 +164,7 @@ public abstract class AbstractMavenProjectTestCase { new DefaultSessionFactory(repoSystem, repositorySystem, new DefaultLookup(container), null); MavenSession session = new MavenSession( - getContainer(), repoSession, new DefaultMavenExecutionRequest(true), new DefaultMavenExecutionResult()); + getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult()); session.setSession(defaultSessionFactory.newSession(session)); DefaultSession s = new DefaultSession(session, null, null, null, null, null);