Skip to content

Commit

Permalink
[API] Use ProjectScope for accessing compile source roots and resources
Browse files Browse the repository at this point in the history
# Conflicts:
#	api/maven-api-core/src/main/java/org/apache/maven/api/services/ProjectManager.java
  • Loading branch information
gnodet committed Feb 9, 2024
1 parent 3f271b8 commit ef4b573
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@
package org.apache.maven.api.services;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.Artifact;
import org.apache.maven.api.Project;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Service;
import org.apache.maven.api.Session;
import java.util.*;

import org.apache.maven.api.*;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
Expand Down Expand Up @@ -86,21 +79,45 @@ default void attachArtifact(Session session, Project project, String type, Path

void attachArtifact(Project project, Artifact artifact, Path path);

List<String> getCompileSourceRoots(Project project);

void addCompileSourceRoot(Project project, String sourceRoot);

List<String> getTestCompileSourceRoots(Project project);

void addTestCompileSourceRoot(Project project, String sourceRoot);

List<Resource> getResources(Project project);
/**
* Obtain an immutable list of compile source roots for the given project and scope.
* Paths are absolute.
*
* @param project the project
* @param scope the scope, i.e. usually main or test
* @return the list of compile source roots
*/
@Nonnull
List<Path> getCompileSourceRoots(@Nonnull Project project, @Nonnull ProjectScope scope);

void addResource(Project project, Resource resource);
/**
* Add a compilation source root to the given project for the given scope.
* The path will be transformed into an absolute path and added to the list for the given scope,
* if not already present.
*
* @param project the project
* @param scope the scope, i.e. usually main or test
* @param sourceRoot the new source root
*/
void addCompileSourceRoot(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Path sourceRoot);

List<Resource> getTestResources(Project project);
/**
* Get the list of resources for the given project and scope
*
* @param project the project
* @param scope the scope, i.e. usually main or test
* @return the list of resources
*/
List<Resource> getResources(@Nonnull Project project, @Nonnull ProjectScope scope);

void addTestResource(Project project, Resource resource);
/**
* Add a resource set to the given project for the given scope.
*
* @param project the project
* @param scope the scope, i.e. usually main or test
* @param resource the resource set to add
*/
void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource);

/**
* Returns an immutable list of project remote repositories (directly specified or inherited).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import javax.inject.Named;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.*;
Expand All @@ -33,7 +35,9 @@
import org.apache.maven.project.MavenProject;
import org.eclipse.sisu.Typed;

import static java.util.stream.Collectors.toList;
import static org.apache.maven.internal.impl.Utils.map;
import static org.apache.maven.internal.impl.Utils.nonNull;

@Named
@Typed
Expand Down Expand Up @@ -86,47 +90,59 @@ public void attachArtifact(Project project, Artifact artifact, Path path) {
}

@Override
public List<String> getCompileSourceRoots(Project project) {
List<String> roots = getMavenProject(project).getCompileSourceRoots();
return Collections.unmodifiableList(roots);
}

@Override
public void addCompileSourceRoot(Project project, String sourceRoot) {
List<String> roots = getMavenProject(project).getCompileSourceRoots();
roots.add(sourceRoot);
}

@Override
public List<String> getTestCompileSourceRoots(Project project) {
List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
return Collections.unmodifiableList(roots);
}

@Override
public void addTestCompileSourceRoot(Project project, String sourceRoot) {
List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
roots.add(sourceRoot);
}

@Override
public List<Resource> getResources(Project project) {
return getMavenProject(project).getBuild().getDelegate().getResources();
public List<Path> getCompileSourceRoots(Project project, ProjectScope scope) {
MavenProject prj = getMavenProject(nonNull(project, "project"));
List<String> roots;
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
roots = prj.getCompileSourceRoots();
} else if (scope == ProjectScope.TEST) {
roots = prj.getTestCompileSourceRoots();
} else {
throw new IllegalArgumentException("Unsupported scope " + scope);
}
return roots.stream()
.map(Paths::get)
.collect(Collectors.collectingAndThen(toList(), Collections::unmodifiableList));
}

@Override
public void addResource(Project project, Resource resource) {
getMavenProject(project).addResource(new org.apache.maven.model.Resource(resource));
public void addCompileSourceRoot(Project project, ProjectScope scope, Path sourceRoot) {
MavenProject prj = getMavenProject(nonNull(project, "project"));
String root = nonNull(sourceRoot, "sourceRoot").toAbsolutePath().toString();
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
prj.addCompileSourceRoot(root);
} else if (scope == ProjectScope.TEST) {
prj.addTestCompileSourceRoot(root);
} else {
throw new IllegalArgumentException("Unsupported scope " + scope);
}
}

@Override
public List<Resource> getTestResources(Project project) {
return getMavenProject(project).getBuild().getDelegate().getTestResources();
public List<Resource> getResources(@Nonnull Project project, @Nonnull ProjectScope scope) {
Project prj = nonNull(project, "project");
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
return prj.getBuild().getResources();
} else if (scope == ProjectScope.TEST) {
return prj.getBuild().getTestResources();
} else {
throw new IllegalArgumentException("Unsupported scope " + scope);
}
}

@Override
public void addTestResource(Project project, Resource resource) {
getMavenProject(project).addTestResource(new org.apache.maven.model.Resource(resource));
public void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource) {
// TODO: we should not modify the underlying model here, but resources should be stored
// TODO: in a separate field in the project, however, that could break v3 plugins
MavenProject prj = getMavenProject(nonNull(project, "project"));
org.apache.maven.model.Resource res = new org.apache.maven.model.Resource(nonNull(resource, "resource"));
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
prj.addResource(res);
} else if (scope == ProjectScope.TEST) {
prj.addTestResource(res);
} else {
throw new IllegalArgumentException("Unsupported scope " + scope);
}
}

@Override
Expand Down

0 comments on commit ef4b573

Please sign in to comment.