[MNG-7985] Provide project access to modified resources / test-resources and to the executionProject (#1356)

This commit is contained in:
Guillaume Nodet 2023-12-21 06:31:27 +01:00 committed by GitHub
parent 732e3649e1
commit b4e78d7c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -30,6 +30,7 @@ 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.Nonnull;
import org.apache.maven.api.model.Resource;
/**
* Interface to manage the project during its lifecycle.
@ -75,7 +76,18 @@ public interface ProjectManager extends Service {
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);
List<RemoteRepository> getRepositories(Project project);
void setProperty(Project project, String key, String value);
@Nonnull
Optional<Project> getExecutionProject(@Nonnull Project project);
}

View File

@ -32,6 +32,7 @@ import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.*;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.SessionScoped;
import org.apache.maven.api.model.Resource;
import org.apache.maven.api.services.*;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;
@ -103,6 +104,26 @@ public class DefaultProjectManager implements ProjectManager {
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));
}
@Override
public List<RemoteRepository> getRepositories(Project project) {
return ((DefaultProject) project)
@ -116,6 +137,15 @@ public class DefaultProjectManager implements ProjectManager {
getMavenProject(project).getProperties().setProperty(key, value);
}
@Override
public Optional<Project> getExecutionProject(Project project) {
// Session keep tracks of the Project per project id,
// so we cannot use session.getProject(p) for forked projects
// which are temporary clones
return Optional.ofNullable(getMavenProject(project).getExecutionProject())
.map(p -> new DefaultProject(session, p));
}
private MavenProject getMavenProject(Project project) {
return ((DefaultProject) project).getProject();
}