[API] Use ProjectScope for accessing compile source roots and resources

# Conflicts:
#	api/maven-api-core/src/main/java/org/apache/maven/api/services/ProjectManager.java
This commit is contained in:
Guillaume Nodet 2024-02-09 13:06:21 +01:00
parent 3f271b8f1b
commit ef4b5737a2
2 changed files with 86 additions and 53 deletions

View File

@ -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 java.util.*;
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 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;
@ -86,21 +79,45 @@ public interface ProjectManager extends Service {
void attachArtifact(Project project, Artifact artifact, Path path);
List<String> getCompileSourceRoots(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 addCompileSourceRoot(Project project, String sourceRoot);
/**
* 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<String> getTestCompileSourceRoots(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 addTestCompileSourceRoot(Project project, String sourceRoot);
List<Resource> getResources(Project project);
void addResource(Project project, Resource resource);
List<Resource> getTestResources(Project project);
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).

View File

@ -22,7 +22,9 @@ import javax.inject.Inject;
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.*;
@ -33,7 +35,9 @@ import org.apache.maven.api.services.*;
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
@ -86,47 +90,59 @@ public class DefaultProjectManager implements ProjectManager {
}
@Override
public List<String> getCompileSourceRoots(Project project) {
List<String> roots = getMavenProject(project).getCompileSourceRoots();
return Collections.unmodifiableList(roots);
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 addCompileSourceRoot(Project project, String sourceRoot) {
List<String> roots = getMavenProject(project).getCompileSourceRoots();
roots.add(sourceRoot);
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<String> getTestCompileSourceRoots(Project project) {
List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
return Collections.unmodifiableList(roots);
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 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();
}
@Override
public void addResource(Project project, Resource resource) {
getMavenProject(project).addResource(new org.apache.maven.model.Resource(resource));
}
@Override
public List<Resource> getTestResources(Project project) {
return getMavenProject(project).getBuild().getDelegate().getTestResources();
}
@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