Refactor ProjectsCollector.collectProjects

This method now returns it result instead of modifying one of its arguments.
Closes #388.
This commit is contained in:
Maarten Mulders 2020-10-23 14:46:26 +02:00
parent e7eef9d34f
commit ba7a0376b1
6 changed files with 30 additions and 31 deletions

View File

@ -35,26 +35,27 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Utility to collect projects for a given set of pom.xml files.
* Utility to select projects for a given set of pom.xml files.
*/
@Named
@Singleton
public class DefaultProjectsCollector implements ProjectsCollector
public class DefaultProjectsSelector implements ProjectsSelector
{
private static final Logger LOGGER = LoggerFactory.getLogger( DefaultProjectsCollector.class );
private static final Logger LOGGER = LoggerFactory.getLogger( DefaultProjectsSelector.class );
private final ProjectBuilder projectBuilder;
@Inject
public DefaultProjectsCollector( ProjectBuilder projectBuilder )
public DefaultProjectsSelector( ProjectBuilder projectBuilder )
{
this.projectBuilder = projectBuilder;
}
@Override
public void collectProjects( List<MavenProject> projects, List<File> files, MavenExecutionRequest request )
public List<MavenProject> selectProjects( List<File> files, MavenExecutionRequest request )
throws ProjectBuildingException
{
ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest();
@ -62,6 +63,8 @@ public class DefaultProjectsCollector implements ProjectsCollector
List<ProjectBuildingResult> results = projectBuilder.build( files, request.isRecursive(),
projectBuildingRequest );
List<MavenProject> projects = new ArrayList<>( results.size() );
boolean problems = false;
for ( ProjectBuildingResult result : results )
@ -94,5 +97,7 @@ public class DefaultProjectsCollector implements ProjectsCollector
+ " longer support building such malformed projects." );
LOGGER.warn( "" );
}
return projects;
}
}

View File

@ -52,13 +52,13 @@ public class MultiModuleCollectionStrategy implements ProjectCollectionStrategy
{
private final Logger logger = LoggerFactory.getLogger( getClass() );
private final ModelLocator modelLocator;
private final ProjectsCollector projectsCollector;
private final ProjectsSelector projectsSelector;
@Inject
public MultiModuleCollectionStrategy( ModelLocator modelLocator, ProjectsCollector projectsCollector )
public MultiModuleCollectionStrategy( ModelLocator modelLocator, ProjectsSelector projectsSelector )
{
this.modelLocator = modelLocator;
this.projectsCollector = projectsCollector;
this.projectsSelector = projectsSelector;
}
@Override
@ -68,8 +68,7 @@ public class MultiModuleCollectionStrategy implements ProjectCollectionStrategy
List<File> files = Collections.singletonList( moduleProjectPomFile.getAbsoluteFile() );
try
{
List<MavenProject> projects = new ArrayList<>();
projectsCollector.collectProjects( projects, files, request );
List<MavenProject> projects = projectsSelector.selectProjects( files, request );
boolean isRequestedProjectCollected = isRequestedProjectCollected( request, projects );
if ( isRequestedProjectCollected )
{

View File

@ -31,7 +31,7 @@ import org.apache.maven.project.ProjectBuildingRequest;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -60,8 +60,6 @@ public class PomlessCollectionStrategy
project.setExecutionRoot( true );
request.setProjectPresent( false );
final List<MavenProject> result = new ArrayList<>();
result.add( project );
return result;
return Arrays.asList( project );
}
}

View File

@ -27,17 +27,17 @@ import java.io.File;
import java.util.List;
/**
* Facade to collect projects for a given set of pom.xml files.
* Facade to select projects for a given set of pom.xml files.
*/
public interface ProjectsCollector
public interface ProjectsSelector
{
/**
* Collect Maven projects from a list of POM files.
* @param projects List that will be filled with the found projects.
* Select Maven projects from a list of POM files.
* @param files List of POM files.
* @param request The {@link MavenExecutionRequest}
* @return A list of projects that have been found in the specified POM files.
* @throws ProjectBuildingException In case the POMs are not used.
*/
void collectProjects( List<MavenProject> projects, List<File> files, MavenExecutionRequest request )
List<MavenProject> selectProjects( List<File> files, MavenExecutionRequest request )
throws ProjectBuildingException;
}

View File

@ -27,7 +27,6 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -38,20 +37,18 @@ import java.util.List;
@Singleton
public class RequestPomCollectionStrategy implements ProjectCollectionStrategy
{
private final ProjectsCollector projectsCollector;
private final ProjectsSelector projectsSelector;
@Inject
public RequestPomCollectionStrategy( ProjectsCollector projectsCollector )
public RequestPomCollectionStrategy( ProjectsSelector projectsSelector )
{
this.projectsCollector = projectsCollector;
this.projectsSelector = projectsSelector;
}
@Override
public List<MavenProject> collectProjects( MavenExecutionRequest request ) throws ProjectBuildingException
{
List<File> files = Collections.singletonList( request.getPom().getAbsoluteFile() );
List<MavenProject> projects = new ArrayList<>();
projectsCollector.collectProjects( projects, files, request );
return projects;
return projectsSelector.selectProjects( files, request );
}
}

View File

@ -32,10 +32,10 @@ import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.project.collector.DefaultProjectsCollector;
import org.apache.maven.project.collector.DefaultProjectsSelector;
import org.apache.maven.project.collector.MultiModuleCollectionStrategy;
import org.apache.maven.project.collector.PomlessCollectionStrategy;
import org.apache.maven.project.collector.ProjectsCollector;
import org.apache.maven.project.collector.ProjectsSelector;
import org.apache.maven.project.collector.RequestPomCollectionStrategy;
import org.codehaus.plexus.util.StringUtils;
import org.junit.Before;
@ -92,14 +92,14 @@ public class DefaultGraphBuilderTest
private final MavenSession session = mock( MavenSession.class );
private final MavenExecutionRequest mavenExecutionRequest = mock( MavenExecutionRequest.class );
private final ProjectsCollector projectsCollector = new DefaultProjectsCollector( projectBuilder );
private final ProjectsSelector projectsSelector = new DefaultProjectsSelector( projectBuilder );
// Not using mocks for these strategies - a mock would just copy the actual implementation.
private final ModelLocator modelLocator = new DefaultModelLocator();
private final PomlessCollectionStrategy pomlessCollectionStrategy = new PomlessCollectionStrategy( projectBuilder );
private final MultiModuleCollectionStrategy multiModuleCollectionStrategy = new MultiModuleCollectionStrategy( modelLocator, projectsCollector );
private final RequestPomCollectionStrategy requestPomCollectionStrategy = new RequestPomCollectionStrategy( projectsCollector );
private final MultiModuleCollectionStrategy multiModuleCollectionStrategy = new MultiModuleCollectionStrategy( modelLocator, projectsSelector );
private final RequestPomCollectionStrategy requestPomCollectionStrategy = new RequestPomCollectionStrategy( projectsSelector );
private Map<String, MavenProject> artifactIdProjectMap;