mirror of https://github.com/apache/maven.git
[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:
parent
3f271b8f1b
commit
ef4b5737a2
|
@ -19,16 +19,9 @@
|
||||||
package org.apache.maven.api.services;
|
package org.apache.maven.api.services;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.apache.maven.api.Artifact;
|
import org.apache.maven.api.*;
|
||||||
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.annotations.Experimental;
|
import org.apache.maven.api.annotations.Experimental;
|
||||||
import org.apache.maven.api.annotations.Nonnull;
|
import org.apache.maven.api.annotations.Nonnull;
|
||||||
import org.apache.maven.api.annotations.Nullable;
|
import org.apache.maven.api.annotations.Nullable;
|
||||||
|
@ -86,21 +79,45 @@ public interface ProjectManager extends Service {
|
||||||
|
|
||||||
void attachArtifact(Project project, Artifact artifact, Path path);
|
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);
|
/**
|
||||||
|
* Add a resource set to the given project for the given scope.
|
||||||
List<Resource> getResources(Project project);
|
*
|
||||||
|
* @param project the project
|
||||||
void addResource(Project project, Resource resource);
|
* @param scope the scope, i.e. usually main or test
|
||||||
|
* @param resource the resource set to add
|
||||||
List<Resource> getTestResources(Project project);
|
*/
|
||||||
|
void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource);
|
||||||
void addTestResource(Project project, Resource resource);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an immutable list of project remote repositories (directly specified or inherited).
|
* Returns an immutable list of project remote repositories (directly specified or inherited).
|
||||||
|
|
|
@ -22,7 +22,9 @@ import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.maven.RepositoryUtils;
|
import org.apache.maven.RepositoryUtils;
|
||||||
import org.apache.maven.api.*;
|
import org.apache.maven.api.*;
|
||||||
|
@ -33,7 +35,9 @@ import org.apache.maven.api.services.*;
|
||||||
import org.apache.maven.project.MavenProject;
|
import org.apache.maven.project.MavenProject;
|
||||||
import org.eclipse.sisu.Typed;
|
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.map;
|
||||||
|
import static org.apache.maven.internal.impl.Utils.nonNull;
|
||||||
|
|
||||||
@Named
|
@Named
|
||||||
@Typed
|
@Typed
|
||||||
|
@ -86,47 +90,59 @@ public class DefaultProjectManager implements ProjectManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getCompileSourceRoots(Project project) {
|
public List<Path> getCompileSourceRoots(Project project, ProjectScope scope) {
|
||||||
List<String> roots = getMavenProject(project).getCompileSourceRoots();
|
MavenProject prj = getMavenProject(nonNull(project, "project"));
|
||||||
return Collections.unmodifiableList(roots);
|
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
|
@Override
|
||||||
public void addCompileSourceRoot(Project project, String sourceRoot) {
|
public void addCompileSourceRoot(Project project, ProjectScope scope, Path sourceRoot) {
|
||||||
List<String> roots = getMavenProject(project).getCompileSourceRoots();
|
MavenProject prj = getMavenProject(nonNull(project, "project"));
|
||||||
roots.add(sourceRoot);
|
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
|
@Override
|
||||||
public List<String> getTestCompileSourceRoots(Project project) {
|
public List<Resource> getResources(@Nonnull Project project, @Nonnull ProjectScope scope) {
|
||||||
List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
|
Project prj = nonNull(project, "project");
|
||||||
return Collections.unmodifiableList(roots);
|
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
|
@Override
|
||||||
public void addTestCompileSourceRoot(Project project, String sourceRoot) {
|
public void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource) {
|
||||||
List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
|
// TODO: we should not modify the underlying model here, but resources should be stored
|
||||||
roots.add(sourceRoot);
|
// 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"));
|
||||||
@Override
|
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
|
||||||
public List<Resource> getResources(Project project) {
|
prj.addResource(res);
|
||||||
return getMavenProject(project).getBuild().getDelegate().getResources();
|
} else if (scope == ProjectScope.TEST) {
|
||||||
}
|
prj.addTestResource(res);
|
||||||
|
} else {
|
||||||
@Override
|
throw new IllegalArgumentException("Unsupported scope " + scope);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue