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

Make rootDirectory mandatory #1787

Merged
merged 5 commits into from
Oct 8, 2024
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 @@ -158,7 +158,6 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.services.model;

import java.nio.file.Path;

import org.apache.maven.api.Service;

/**
* Interface used to detect is a given directory "root directory".
*/
public interface RootDetector extends Service {
boolean isRootDirectory(Path dir);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

import org.apache.maven.api.Service;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

/**
* 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}
* property which happens very early. The implementation used in this case
* will be discovered using the JDK service mechanism.
Expand All @@ -42,27 +41,10 @@ public interface RootLocator extends Service {
+ " attribute on the root project's model to identify it.";

@Nonnull
default Path findMandatoryRoot(@Nullable Path basedir) {
Path rootDirectory = findRoot(basedir);
if (rootDirectory == null) {
throw new IllegalStateException(getNoRootMessage());
}
return rootDirectory;
}

@Nullable
default Path findRoot(@Nullable Path basedir) {
Path rootDirectory = basedir;
while (rootDirectory != null && !isRootDirectory(rootDirectory)) {
rootDirectory = rootDirectory.getParent();
}
return rootDirectory;
}
Path findMandatoryRoot(@Nonnull Path basedir);

@Nonnull
default String getNoRootMessage() {
return UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE;
}

boolean isRootDirectory(Path dir);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
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;
Expand Down Expand Up @@ -633,12 +632,7 @@ private void buildBuildPom() throws ModelBuilderException {
top = top.toAbsolutePath().normalize();

// Obtain the root directory, resolving it if necessary
Path rootDirectory;
try {
rootDirectory = session.getRootDirectory();
} catch (IllegalStateException e) {
rootDirectory = session.getService(RootLocator.class).findRoot(top);
}
Path rootDirectory = session.getRootDirectory();

// Locate and normalize the root POM if it exists, fallback to top otherwise
Path root = modelProcessor.locateExistingPom(rootDirectory);
Expand Down Expand Up @@ -1177,19 +1171,11 @@ Model readFileModel() throws ModelBuilderException {
Model doReadFileModel() throws ModelBuilderException {
ModelSource modelSource = request.getSource();
Model model;
Path rootDirectory;
Path rootDirectory = request.getSession().getRootDirectory();
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)
Expand Down Expand Up @@ -1592,12 +1578,7 @@ private Model doLoadDependencyManagement(
return null;
}

Path rootDirectory;
try {
rootDirectory = request.getSession().getRootDirectory();
} catch (IllegalStateException e) {
rootDirectory = null;
}
Path rootDirectory = request.getSession().getRootDirectory();
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM && rootDirectory != null) {
Path sourcePath = importSource.getPath();
if (sourcePath != null && sourcePath.startsWith(rootDirectory)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.internal.impl.model.rootlocator;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.services.model.RootDetector;
import org.apache.maven.api.services.model.RootLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Objects.requireNonNull;

@Named
public class DefaultRootLocator implements RootLocator {
private final Logger logger = LoggerFactory.getLogger(getClass());

private final List<RootDetector> rootDetectors;

public DefaultRootLocator() {
this.rootDetectors = ServiceLoader.load(RootDetector.class).stream()
.map(ServiceLoader.Provider::get)
.toList();
}

@Nonnull
public Path findMandatoryRoot(@Nonnull Path basedir) {
requireNonNull(basedir, "basedir is null");
Path rootDirectory = basedir;
while (rootDirectory != null && !isRootDirectory(rootDirectory)) {
rootDirectory = rootDirectory.getParent();
}
Optional<Path> rdf = getMultiModuleProjectDirectory();
if (rootDirectory == null) {
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");
}
}
return rootDirectory;
}

protected boolean isRootDirectory(Path dir) {
requireNonNull(dir, "dir is null");
for (RootDetector rootDetector : rootDetectors) {
if (rootDetector.isRootDirectory(dir)) {
return true;
}
}
return false;
}

protected Optional<Path> getMultiModuleProjectDirectory() {
String mmpd = System.getProperty("maven.multiModuleProjectDirectory");
if (mmpd != null) {
return Optional.of(Paths.get(mmpd));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.internal.impl.model.rootlocator;

import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.api.di.Named;
import org.apache.maven.api.services.model.RootDetector;

@Named
public class DotMvnRootDetector implements RootDetector {
@Override
public boolean isRootDirectory(Path dir) {
return Files.isDirectory(dir.resolve(".mvn"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.internal.impl.model;
package org.apache.maven.internal.impl.model.rootlocator;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
Expand All @@ -27,28 +27,13 @@
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.services.model.RootLocator;
import org.apache.maven.api.services.model.RootDetector;

@Named
public class DefaultRootLocator implements RootLocator {

@Override
@Nullable
public Path findRoot(Path basedir) {
Path rootDirectory = basedir;
while (rootDirectory != null && !isRootDirectory(rootDirectory)) {
rootDirectory = rootDirectory.getParent();
}
return rootDirectory;
}

public class PomXmlRootDetector implements RootDetector {
@Override
public boolean isRootDirectory(Path dir) {
if (Files.isDirectory(dir.resolve(".mvn"))) {
return true;
}
// we're too early to use the modelProcessor ...
Path pom = dir.resolve("pom.xml");
try (InputStream is = Files.newInputStream(pom)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.apache.maven.internal.impl.model.rootlocator.DotMvnRootDetector
org.apache.maven.internal.impl.model.rootlocator.PomXmlRootDetector
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.maven.internal.impl.model.rootlocator.DefaultRootLocator
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
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;
Expand Down Expand Up @@ -153,12 +154,12 @@ public Instant getStartTime() {

@Override
public Path getTopDirectory() {
return null;
return Paths.get("");
}

@Override
public Path getRootDirectory() {
throw new IllegalStateException();
return getService(RootLocator.class).findMandatoryRoot(getTopDirectory());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
import org.apache.maven.internal.impl.model.DefaultPluginManagementInjector;
import org.apache.maven.internal.impl.model.DefaultProfileInjector;
import org.apache.maven.internal.impl.model.DefaultProfileSelector;
import org.apache.maven.internal.impl.model.DefaultRootLocator;
import org.apache.maven.internal.impl.model.ProfileActivationFilePathInterpolator;
import org.apache.maven.internal.impl.model.rootlocator.DefaultRootLocator;
import org.apache.maven.internal.impl.resolver.DefaultArtifactDescriptorReader;
import org.apache.maven.internal.impl.resolver.DefaultModelResolver;
import org.apache.maven.internal.impl.resolver.DefaultVersionRangeResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.function.Function;
Expand All @@ -43,10 +42,10 @@
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.api.cli.extensions.CoreExtension;
import org.apache.maven.api.services.model.RootLocator;
import org.apache.maven.cli.CLIReportingUtils;
import org.apache.maven.cli.internal.extension.io.CoreExtensionsStaxReader;
import org.apache.maven.cli.props.MavenPropertiesLoader;
import org.apache.maven.model.root.RootLocator;
import org.apache.maven.properties.internal.EnvironmentUtils;
import org.apache.maven.properties.internal.SystemProperties;

Expand Down Expand Up @@ -196,30 +195,10 @@ protected Path getTopDirectory(LocalContext context) throws ParserException {
}

protected Path getRootDirectory(LocalContext context) throws ParserException {
RootLocator rootLocator =
ServiceLoader.load(RootLocator.class).iterator().next();
Path rootDirectory = rootLocator.findRoot(requireNonNull(context.topDirectory));

// TODO: multiModuleProjectDirectory vs rootDirectory?
// fallback if no root? otherwise make sure they are same?
Path mmpd = System.getProperty("maven.multiModuleProjectDirectory") == null
? null
: getCanonicalPath(context.cwd.resolve(requireNonNull(
System.getProperty("maven.multiModuleProjectDirectory"),
"maven.multiModuleProjectDirectory is not set")));
if (rootDirectory == null) {
context.parserRequest.logger().warn(rootLocator.getNoRootMessage());
rootDirectory = requireNonNull(
mmpd, "maven.multiModuleProjectDirectory is not set and rootDirectory was not discovered");
} else {
rootDirectory = getCanonicalPath(rootDirectory);
if (mmpd != null && !Objects.equals(rootDirectory, mmpd)) {
context.parserRequest
.logger()
.warn("Project root directory and multiModuleProjectDirectory are not aligned");
}
}
return getCanonicalPath(rootDirectory);
return getCanonicalPath(ServiceLoader.load(RootLocator.class)
.iterator()
.next()
.findMandatoryRoot(requireNonNull(context.topDirectory)));
}

protected Map<String, String> populateSystemProperties(LocalContext context) throws ParserException {
Expand Down
Loading