Skip to content

Commit

Permalink
Restrict Project to the buildable projects, i.e. from the filesyste…
Browse files Browse the repository at this point in the history
…m so that getBasedir() and getPomPath() always return a valid directory

That means that getParent() will return an empty optional if the parent not in the reactor.
  • Loading branch information
gnodet committed Sep 19, 2024
1 parent 85bf392 commit b9a4125
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ default String getId() {

/**
* Returns project parent project, if any.
* <p>
* Note that the model may have a parent defined, but an empty parent
* project may be returned if the parent comes from a remote repository,
* as a {@code Project} must refer to a buildable project.
*
* @return an optional containing the parent project
* @see Model#getParent()
*/
@Nonnull
Optional<Project> getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -49,6 +50,6 @@ static <T> T cast(Class<T> clazz, Object o, String name) {
}

static <U, V> List<V> map(Collection<U> list, Function<U, V> mapper) {
return list.stream().map(mapper).collect(Collectors.toList());
return list.stream().map(mapper).filter(Objects::nonNull).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Session getSession() {

@Override
public Optional<Project> getProject() {
return Optional.ofNullable(delegate.getProject()).map(session::getProject);
return Optional.ofNullable(session.getProject(delegate.getProject()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Path getRootDirectory() {
@Override
public Optional<Project> getParent() {
MavenProject parent = project.getParent();
return parent != null ? Optional.of(session.getProject(parent)) : Optional.empty();
return Optional.ofNullable(session.getProject(parent));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Optional<Path> getPomFile() {
@Nonnull
@Override
public Optional<Project> getProject() {
return Optional.ofNullable(res.getProject()).map(session::getProject);
return Optional.ofNullable(session.getProject(res.getProject()));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public List<Project> getProjects(List<MavenProject> projects) {

@Override
public Project getProject(MavenProject project) {
return allProjects.computeIfAbsent(project.getId(), id -> new DefaultProject(this, project));
return project != null && project.getBasedir() != null
? allProjects.computeIfAbsent(project.getId(), id -> new DefaultProject(this, project))
: null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.maven.api.Project;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Session;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.execution.MavenSession;

import static org.apache.maven.internal.impl.Utils.cast;
Expand All @@ -35,6 +36,10 @@ static InternalMavenSession from(Session session) {

List<Project> getProjects(List<org.apache.maven.project.MavenProject> projects);

/**
* May return null if the input projcet is null or is not part of the reactor.
*/
@Nullable
Project getProject(org.apache.maven.project.MavenProject project);

List<org.apache.maven.artifact.repository.ArtifactRepository> toArtifactRepositories(
Expand Down

0 comments on commit b9a4125

Please sign in to comment.