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 5e5930ca964b..7788ae85e422 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 @@ -31,6 +31,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.eventspy.internal.EventSpyDispatcher; import org.apache.maven.model.Profile; +import org.apache.maven.model.root.RootLocator; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.properties.internal.SystemProperties; @@ -211,7 +212,11 @@ public static MavenExecutionRequest copy(MavenExecutionRequest original) { copy.setBuilderId(original.getBuilderId()); copy.setTopDirectory(original.getTopDirectory()); - copy.setRootDirectory(original.getRootDirectory()); + try { + copy.setRootDirectory(original.getRootDirectory()); + } catch (IllegalStateException e) { + // ignore during copy + } return copy; } @@ -1120,6 +1125,9 @@ public MavenExecutionRequest setTopDirectory(Path topDirectory) { @Override public Path getRootDirectory() { + if (rootDirectory == null) { + throw new IllegalStateException(RootLocator.UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE); + } return rootDirectory; } diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java index e3cf7b43e577..ceb76ec56181 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java @@ -19,6 +19,7 @@ package org.apache.maven.execution; import java.io.File; +import java.nio.file.Path; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -170,20 +171,22 @@ public MavenExecutionResult getResult() { * * @since 3.10.0 */ - public File getTopDirectory() { - return request.getTopDirectory() != null ? request.getTopDirectory().toFile() : null; + public Path getTopDirectory() { + return request.getTopDirectory(); } /** - * Returns root directory if discovered or {@code null}. + * Returns root directory if discovered or throws {@link IllegalStateException} if it could not be found. * Root directory is detected, usually by the presence of {@code .mvn} directory, and is used to discover * extensions and other configuration files. It usually represents the "project root" or "checkout root". * Root directory may be equal to top directory or some parent of it (first directory where {@code .mvn} is found). * + * @throws IllegalStateException if the root directory could not be found + * * @since 3.10.0 */ - public File getRootDirectory() { - return request.getRootDirectory() != null ? request.getRootDirectory().toFile() : null; + public Path getRootDirectory() { + return request.getRootDirectory(); } // Backward compat diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 0859050fe131..7c6b6ac3f7ae 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -625,7 +625,9 @@ void properties(CliRequest cliRequest) throws ExitException { } catch (IllegalUseOfUndefinedProperty e) { String message = "ERROR: Illegal use of undefined property: " + e.property; System.err.println(message); - if (cliRequest.request.getRootDirectory() == null) { + try { + cliRequest.request.getRootDirectory(); + } catch (IllegalStateException ex) { System.err.println(); System.err.println(RootLocator.UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE); } @@ -1652,10 +1654,9 @@ public Object getValue(String expression) { throw new IllegalUseOfUndefinedProperty(expression); } } else if ("session.rootDirectory".equals(expression)) { - Path rootDirectory = cliRequest.request.getRootDirectory(); - if (rootDirectory != null) { - return rootDirectory.toString(); - } else { + try { + return cliRequest.request.getRootDirectory(); + } catch (IllegalStateException e) { throw new IllegalUseOfUndefinedProperty(expression); } }