Make rootDirectory really mandatory

This commit is contained in:
Guillaume Nodet 2024-10-08 18:20:59 +02:00
parent 836621bc7c
commit 608a99fb08
13 changed files with 27 additions and 36 deletions

View File

@ -158,7 +158,6 @@ public interface Session {
* Gets the root directory of the session, which is the root directory for the top directory project. * Gets the root directory of the session, which is the root directory for the top directory project.
* *
* @return the root directory, never {@code null} * @return the root directory, never {@code null}
* @throws IllegalStateException if the root directory could not be found
* @see #getTopDirectory() * @see #getTopDirectory()
* @see Project#getRootDirectory() * @see Project#getRootDirectory()
* @see Project#isRootProject() * @see Project#isRootProject()

View File

@ -26,7 +26,7 @@ import org.apache.maven.api.annotations.Nonnull;
/** /**
* Interface used to locate the root directory for a given project. * Interface used to locate the root directory for a given project.
* *
* The root locator is usually looked up from the plexus container. * The root locator is usually looked up from the DI container.
* One notable exception is the computation of the early {@code session.rootDirectory} * One notable exception is the computation of the early {@code session.rootDirectory}
* property which happens very early. The implementation used in this case * property which happens very early. The implementation used in this case
* will be discovered using the JDK service mechanism. * will be discovered using the JDK service mechanism.

View File

@ -106,7 +106,6 @@ import org.apache.maven.api.services.model.PluginManagementInjector;
import org.apache.maven.api.services.model.ProfileActivationContext; import org.apache.maven.api.services.model.ProfileActivationContext;
import org.apache.maven.api.services.model.ProfileInjector; import org.apache.maven.api.services.model.ProfileInjector;
import org.apache.maven.api.services.model.ProfileSelector; 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.XmlReaderException;
import org.apache.maven.api.services.xml.XmlReaderRequest; import org.apache.maven.api.services.xml.XmlReaderRequest;
import org.apache.maven.api.spi.ModelParserException; import org.apache.maven.api.spi.ModelParserException;
@ -633,12 +632,7 @@ public class DefaultModelBuilder implements ModelBuilder {
top = top.toAbsolutePath().normalize(); top = top.toAbsolutePath().normalize();
// Obtain the root directory, resolving it if necessary // Obtain the root directory, resolving it if necessary
Path rootDirectory; Path rootDirectory = session.getRootDirectory();
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 // Locate and normalize the root POM if it exists, fallback to top otherwise
Path root = modelProcessor.locateExistingPom(rootDirectory); Path root = modelProcessor.locateExistingPom(rootDirectory);
@ -1177,19 +1171,11 @@ public class DefaultModelBuilder implements ModelBuilder {
Model doReadFileModel() throws ModelBuilderException { Model doReadFileModel() throws ModelBuilderException {
ModelSource modelSource = request.getSource(); ModelSource modelSource = request.getSource();
Model model; Model model;
Path rootDirectory; Path rootDirectory = request.getSession().getRootDirectory();
setSource(modelSource.getLocation()); setSource(modelSource.getLocation());
logger.debug("Reading file model from " + modelSource.getLocation()); logger.debug("Reading file model from " + modelSource.getLocation());
try { try {
boolean strict = request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM; 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()) { try (InputStream is = modelSource.openStream()) {
model = modelProcessor.read(XmlReaderRequest.builder() model = modelProcessor.read(XmlReaderRequest.builder()
.strict(strict) .strict(strict)
@ -1592,12 +1578,7 @@ public class DefaultModelBuilder implements ModelBuilder {
return null; return null;
} }
Path rootDirectory; Path rootDirectory = request.getSession().getRootDirectory();
try {
rootDirectory = request.getSession().getRootDirectory();
} catch (IllegalStateException e) {
rootDirectory = null;
}
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM && rootDirectory != null) { if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM && rootDirectory != null) {
Path sourcePath = importSource.getPath(); Path sourcePath = importSource.getPath();
if (sourcePath != null && sourcePath.startsWith(rootDirectory)) { if (sourcePath != null && sourcePath.startsWith(rootDirectory)) {

View File

@ -53,10 +53,10 @@ public class DefaultRootLocator implements RootLocator {
while (rootDirectory != null && !isRootDirectory(rootDirectory)) { while (rootDirectory != null && !isRootDirectory(rootDirectory)) {
rootDirectory = rootDirectory.getParent(); rootDirectory = rootDirectory.getParent();
} }
Optional<Path> rdf = getRootDirectoryFallback(); Optional<Path> rdf = getMultiModuleProjectDirectory();
if (rootDirectory == null) { if (rootDirectory == null) {
rootDirectory = rdf.orElseThrow(() -> new IllegalStateException(getNoRootMessage()));
logger.warn(getNoRootMessage()); logger.warn(getNoRootMessage());
rootDirectory = rdf.orElseGet(() -> Paths.get("").toAbsolutePath());
} else { } else {
if (rdf.isPresent() && !Objects.equals(rootDirectory, rdf.get())) { if (rdf.isPresent() && !Objects.equals(rootDirectory, rdf.get())) {
logger.warn("Project root directory and multiModuleProjectDirectory are not aligned"); logger.warn("Project root directory and multiModuleProjectDirectory are not aligned");
@ -75,7 +75,7 @@ public class DefaultRootLocator implements RootLocator {
return false; return false;
} }
protected Optional<Path> getRootDirectoryFallback() { protected Optional<Path> getMultiModuleProjectDirectory() {
String mmpd = System.getProperty("maven.multiModuleProjectDirectory"); String mmpd = System.getProperty("maven.multiModuleProjectDirectory");
if (mmpd != null) { if (mmpd != null) {
return Optional.of(Paths.get(mmpd)); return Optional.of(Paths.get(mmpd));

View File

@ -52,6 +52,7 @@ import org.apache.maven.api.services.PackagingRegistry;
import org.apache.maven.api.services.RepositoryFactory; import org.apache.maven.api.services.RepositoryFactory;
import org.apache.maven.api.services.SettingsBuilder; import org.apache.maven.api.services.SettingsBuilder;
import org.apache.maven.api.services.TypeRegistry; 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.settings.Settings;
import org.apache.maven.api.spi.TypeProvider; import org.apache.maven.api.spi.TypeProvider;
import org.apache.maven.di.Injector; import org.apache.maven.di.Injector;
@ -153,12 +154,12 @@ public class ApiRunner {
@Override @Override
public Path getTopDirectory() { public Path getTopDirectory() {
return null; return Paths.get("");
} }
@Override @Override
public Path getRootDirectory() { public Path getRootDirectory() {
throw new IllegalStateException(); return getService(RootLocator.class).findMandatoryRoot(getTopDirectory());
} }
@Override @Override

View File

@ -90,7 +90,7 @@ public abstract class AbstractCoreMavenComponentTestCase {
} }
protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception { protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
MavenExecutionRequest request = new DefaultMavenExecutionRequest() MavenExecutionRequest request = new DefaultMavenExecutionRequest(true)
.setPom(pom) .setPom(pom)
.setProjectPresent(true) .setProjectPresent(true)
.setShowErrors(true) .setShowErrors(true)
@ -102,6 +102,7 @@ public abstract class AbstractCoreMavenComponentTestCase {
if (pom != null) { if (pom != null) {
request.setMultiModuleProjectDirectory(pom.getParentFile()); request.setMultiModuleProjectDirectory(pom.getParentFile());
request.setRootDirectory(pom.getParentFile().toPath());
} }
return request; return request;

View File

@ -161,7 +161,7 @@ public abstract class AbstractMavenProjectTestCase {
session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo)); session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
request.setRepositorySession(session); request.setRepositorySession(session);
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(true);
MavenSession msession = MavenSession msession =
new MavenSession(getContainer(), session, mavenExecutionRequest, new DefaultMavenExecutionResult()); new MavenSession(getContainer(), session, mavenExecutionRequest, new DefaultMavenExecutionResult());
DefaultSession iSession = new DefaultSession( DefaultSession iSession = new DefaultSession(

View File

@ -123,7 +123,7 @@ class LegacyRepositorySystemTest {
new LocalRepository(request.getLocalRepository().getBasedir()); new LocalRepository(request.getLocalRepository().getBasedir());
session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo)); session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
LegacySupport legacySupport = container.lookup(LegacySupport.class); LegacySupport legacySupport = container.lookup(LegacySupport.class);
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(true);
MavenSession mavenSession = MavenSession mavenSession =
new MavenSession(container, session, mavenExecutionRequest, new DefaultMavenExecutionResult()); new MavenSession(container, session, mavenExecutionRequest, new DefaultMavenExecutionResult());
legacySupport.setSession(mavenSession); legacySupport.setSession(mavenSession);

View File

@ -20,6 +20,7 @@ package org.apache.maven.execution;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -172,6 +173,12 @@ public class DefaultMavenExecutionRequest implements MavenExecutionRequest {
public DefaultMavenExecutionRequest() {} public DefaultMavenExecutionRequest() {}
public DefaultMavenExecutionRequest(boolean withDefaultRoot) {
if (withDefaultRoot) {
setRootDirectory(Paths.get("").toAbsolutePath());
}
}
public static MavenExecutionRequest copy(MavenExecutionRequest original) { public static MavenExecutionRequest copy(MavenExecutionRequest original) {
DefaultMavenExecutionRequest copy = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest copy = new DefaultMavenExecutionRequest();
copy.setLocalRepository(original.getLocalRepository()); copy.setLocalRepository(original.getLocalRepository());

View File

@ -21,6 +21,7 @@ package org.apache.maven;
import javax.inject.Inject; import javax.inject.Inject;
import java.io.File; import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -90,7 +91,8 @@ public abstract class AbstractCoreMavenComponentTestCase {
protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception { protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
MavenExecutionRequest request = new DefaultMavenExecutionRequest() MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setRootDirectory(pom != null ? pom.toPath().getParent() : null) .setRootDirectory(
pom != null ? pom.toPath().getParent() : Paths.get("").toAbsolutePath())
.setPom(pom) .setPom(pom)
.setProjectPresent(true) .setProjectPresent(true)
.setShowErrors(true) .setShowErrors(true)

View File

@ -32,7 +32,7 @@ public class MavenTestHelper {
public static DefaultRepositorySystemSession createSession( public static DefaultRepositorySystemSession createSession(
MavenRepositorySystem repositorySystem, PlexusContainer container) { MavenRepositorySystem repositorySystem, PlexusContainer container) {
DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false); DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest(true);
MavenSession mavenSession = new MavenSession(repoSession, request, new DefaultMavenExecutionResult()); MavenSession mavenSession = new MavenSession(repoSession, request, new DefaultMavenExecutionResult());
DefaultSession session = DefaultSession session =
new DefaultSession(mavenSession, null, null, repositorySystem, new DefaultLookup(container), null); new DefaultSession(mavenSession, null, null, repositorySystem, new DefaultLookup(container), null);

View File

@ -119,7 +119,7 @@ class TestApi {
.get() .get()
.withLocalRepositoryBaseDirectories(new File("target").toPath()) .withLocalRepositoryBaseDirectories(new File("target").toPath())
.build(); .build();
DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(); DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest(true);
DefaultMavenExecutionResult meres = new DefaultMavenExecutionResult(); DefaultMavenExecutionResult meres = new DefaultMavenExecutionResult();
MavenSession ms = new MavenSession(rss, mer, meres); MavenSession ms = new MavenSession(rss, mer, meres);
DefaultSession session = new DefaultSession( DefaultSession session = new DefaultSession(

View File

@ -164,7 +164,7 @@ public abstract class AbstractMavenProjectTestCase {
new DefaultSessionFactory(repoSystem, repositorySystem, new DefaultLookup(container), null); new DefaultSessionFactory(repoSystem, repositorySystem, new DefaultLookup(container), null);
MavenSession session = new MavenSession( MavenSession session = new MavenSession(
getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult()); getContainer(), repoSession, new DefaultMavenExecutionRequest(true), new DefaultMavenExecutionResult());
session.setSession(defaultSessionFactory.newSession(session)); session.setSession(defaultSessionFactory.newSession(session));
DefaultSession s = new DefaultSession(session, null, null, null, null, null); DefaultSession s = new DefaultSession(session, null, null, null, null, null);