Skip to content

Commit 1cff594

Browse files
authored
Revert "Make rootDirectory really mandatory" (#1800)
This reverts commit 608a99f. --- https://issues.apache.org/jira/browse/MNG-8302
1 parent 9b9f720 commit 1cff594

File tree

12 files changed

+35
-26
lines changed

12 files changed

+35
-26
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/Session.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public interface Session {
158158
* Gets the root directory of the session, which is the root directory for the top directory project.
159159
*
160160
* @return the root directory, never {@code null}
161+
* @throws IllegalStateException if the root directory could not be found
161162
* @see #getTopDirectory()
162163
* @see Project#getRootDirectory()
163164
* @see Project#isRootProject()

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.apache.maven.api.services.model.ProfileActivationContext;
106106
import org.apache.maven.api.services.model.ProfileInjector;
107107
import org.apache.maven.api.services.model.ProfileSelector;
108+
import org.apache.maven.api.services.model.RootLocator;
108109
import org.apache.maven.api.services.xml.XmlReaderException;
109110
import org.apache.maven.api.services.xml.XmlReaderRequest;
110111
import org.apache.maven.api.spi.ModelParserException;
@@ -631,7 +632,12 @@ private void buildBuildPom() throws ModelBuilderException {
631632
top = top.toAbsolutePath().normalize();
632633

633634
// Obtain the root directory, resolving it if necessary
634-
Path rootDirectory = session.getRootDirectory();
635+
Path rootDirectory;
636+
try {
637+
rootDirectory = session.getRootDirectory();
638+
} catch (IllegalStateException e) {
639+
rootDirectory = session.getService(RootLocator.class).findMandatoryRoot(top);
640+
}
635641

636642
// Locate and normalize the root POM if it exists, fallback to top otherwise
637643
Path root = modelProcessor.locateExistingPom(rootDirectory);
@@ -1232,11 +1238,19 @@ Model readFileModel() throws ModelBuilderException {
12321238
Model doReadFileModel() throws ModelBuilderException {
12331239
ModelSource modelSource = request.getSource();
12341240
Model model;
1235-
Path rootDirectory = request.getSession().getRootDirectory();
1241+
Path rootDirectory;
12361242
setSource(modelSource.getLocation());
12371243
logger.debug("Reading file model from " + modelSource.getLocation());
12381244
try {
12391245
boolean strict = request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM;
1246+
try {
1247+
rootDirectory = request.getSession().getRootDirectory();
1248+
} catch (IllegalStateException ignore) {
1249+
rootDirectory = modelSource.getPath();
1250+
while (rootDirectory != null && !Files.isDirectory(rootDirectory)) {
1251+
rootDirectory = rootDirectory.getParent();
1252+
}
1253+
}
12401254
try (InputStream is = modelSource.openStream()) {
12411255
model = modelProcessor.read(XmlReaderRequest.builder()
12421256
.strict(strict)
@@ -1647,7 +1661,12 @@ private Model doLoadDependencyManagement(
16471661
return null;
16481662
}
16491663

1650-
Path rootDirectory = request.getSession().getRootDirectory();
1664+
Path rootDirectory;
1665+
try {
1666+
rootDirectory = request.getSession().getRootDirectory();
1667+
} catch (IllegalStateException e) {
1668+
rootDirectory = null;
1669+
}
16511670
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM && rootDirectory != null) {
16521671
Path sourcePath = importSource.getPath();
16531672
if (sourcePath != null && sourcePath.startsWith(rootDirectory)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public Path findMandatoryRoot(@Nonnull Path basedir) {
5353
while (rootDirectory != null && !isRootDirectory(rootDirectory)) {
5454
rootDirectory = rootDirectory.getParent();
5555
}
56-
Optional<Path> rdf = getMultiModuleProjectDirectory();
56+
Optional<Path> rdf = getRootDirectoryFallback();
5757
if (rootDirectory == null) {
58+
rootDirectory = rdf.orElseThrow(() -> new IllegalStateException(getNoRootMessage()));
5859
logger.warn(getNoRootMessage());
59-
rootDirectory = rdf.orElseGet(() -> Paths.get("").toAbsolutePath());
6060
} else {
6161
if (rdf.isPresent() && !Objects.equals(rootDirectory, rdf.get())) {
6262
logger.warn("Project root directory and multiModuleProjectDirectory are not aligned");
@@ -75,7 +75,7 @@ protected boolean isRootDirectory(Path dir) {
7575
return false;
7676
}
7777

78-
protected Optional<Path> getMultiModuleProjectDirectory() {
78+
protected Optional<Path> getRootDirectoryFallback() {
7979
String mmpd = System.getProperty("maven.multiModuleProjectDirectory");
8080
if (mmpd != null) {
8181
return Optional.of(Paths.get(mmpd));

maven-api-impl/src/test/java/org/apache/maven/internal/impl/standalone/ApiRunner.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.apache.maven.api.services.RepositoryFactory;
5353
import org.apache.maven.api.services.SettingsBuilder;
5454
import org.apache.maven.api.services.TypeRegistry;
55-
import org.apache.maven.api.services.model.RootLocator;
5655
import org.apache.maven.api.settings.Settings;
5756
import org.apache.maven.api.spi.TypeProvider;
5857
import org.apache.maven.di.Injector;
@@ -156,12 +155,12 @@ public Instant getStartTime() {
156155

157156
@Override
158157
public Path getTopDirectory() {
159-
return Paths.get("");
158+
return null;
160159
}
161160

162161
@Override
163162
public Path getRootDirectory() {
164-
return getService(RootLocator.class).findMandatoryRoot(getTopDirectory());
163+
throw new IllegalStateException();
165164
}
166165

167166
@Override

maven-compat/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected File getProject(String name) throws Exception {
9090
}
9191

9292
protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
93-
MavenExecutionRequest request = new DefaultMavenExecutionRequest(true)
93+
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
9494
.setPom(pom)
9595
.setProjectPresent(true)
9696
.setShowErrors(true)
@@ -102,7 +102,6 @@ protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exc
102102

103103
if (pom != null) {
104104
request.setMultiModuleProjectDirectory(pom.getParentFile());
105-
request.setRootDirectory(pom.getParentFile().toPath());
106105
}
107106

108107
return request;

maven-compat/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected void initRepoSession(ProjectBuildingRequest request) throws Exception
161161
session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
162162
request.setRepositorySession(session);
163163

164-
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(true);
164+
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
165165
MavenSession msession =
166166
new MavenSession(getContainer(), session, mavenExecutionRequest, new DefaultMavenExecutionResult());
167167
DefaultSession iSession = new DefaultSession(

maven-compat/src/test/java/org/apache/maven/repository/LegacyRepositorySystemTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void testThatASystemScopedDependencyIsNotResolvedFromRepositories() throws Excep
123123
new LocalRepository(request.getLocalRepository().getBasedir());
124124
session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
125125
LegacySupport legacySupport = container.lookup(LegacySupport.class);
126-
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest(true);
126+
DefaultMavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
127127
MavenSession mavenSession =
128128
new MavenSession(container, session, mavenExecutionRequest, new DefaultMavenExecutionResult());
129129
legacySupport.setSession(mavenSession);

maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.File;
2222
import java.nio.file.Path;
23-
import java.nio.file.Paths;
2423
import java.util.ArrayList;
2524
import java.util.Date;
2625
import java.util.HashMap;
@@ -173,12 +172,6 @@ public class DefaultMavenExecutionRequest implements MavenExecutionRequest {
173172

174173
public DefaultMavenExecutionRequest() {}
175174

176-
public DefaultMavenExecutionRequest(boolean withDefaultRoot) {
177-
if (withDefaultRoot) {
178-
setRootDirectory(Paths.get("").toAbsolutePath());
179-
}
180-
}
181-
182175
public static MavenExecutionRequest copy(MavenExecutionRequest original) {
183176
DefaultMavenExecutionRequest copy = new DefaultMavenExecutionRequest();
184177
copy.setLocalRepository(original.getLocalRepository());

maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import javax.inject.Inject;
2222

2323
import java.io.File;
24-
import java.nio.file.Paths;
2524
import java.util.ArrayList;
2625
import java.util.Arrays;
2726
import java.util.List;
@@ -91,8 +90,7 @@ protected File getProject(String name) throws Exception {
9190

9291
protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
9392
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
94-
.setRootDirectory(
95-
pom != null ? pom.toPath().getParent() : Paths.get("").toAbsolutePath())
93+
.setRootDirectory(pom != null ? pom.toPath().getParent() : null)
9694
.setPom(pom)
9795
.setProjectPresent(true)
9896
.setShowErrors(true)

maven-core/src/test/java/org/apache/maven/MavenTestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class MavenTestHelper {
3232
public static DefaultRepositorySystemSession createSession(
3333
MavenRepositorySystem repositorySystem, PlexusContainer container) {
3434
DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
35-
DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest(true);
35+
DefaultMavenExecutionRequest request = new DefaultMavenExecutionRequest();
3636
MavenSession mavenSession = new MavenSession(repoSession, request, new DefaultMavenExecutionResult());
3737
DefaultSession session =
3838
new DefaultSession(mavenSession, null, null, repositorySystem, new DefaultLookup(container), null);

0 commit comments

Comments
 (0)