o Refactored code

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1073926 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2011-02-23 20:22:03 +00:00
parent 39e9e35c3d
commit c4f0aa5209
3 changed files with 84 additions and 56 deletions

View File

@ -48,9 +48,10 @@ import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.Os; import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import org.sonatype.aether.impl.ArtifactResolver; import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.impl.RemoteRepositoryManager; import org.sonatype.aether.impl.RemoteRepositoryManager;
import org.sonatype.aether.repository.LocalRepositoryManager; import org.sonatype.aether.repository.LocalRepositoryManager;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.repository.WorkspaceRepository; import org.sonatype.aether.repository.WorkspaceRepository;
import org.sonatype.aether.resolution.ArtifactRequest; import org.sonatype.aether.resolution.ArtifactRequest;
import org.sonatype.aether.resolution.ArtifactResult; import org.sonatype.aether.resolution.ArtifactResult;
@ -59,7 +60,7 @@ import org.sonatype.aether.util.artifact.SubArtifact;
/** /**
* @version $Id$ * @version $Id$
*/ */
@Component(role = ProjectBuilder.class) @Component( role = ProjectBuilder.class )
public class DefaultProjectBuilder public class DefaultProjectBuilder
implements ProjectBuilder implements ProjectBuilder
{ {
@ -80,7 +81,7 @@ public class DefaultProjectBuilder
private RepositorySystem repositorySystem; private RepositorySystem repositorySystem;
@Requirement @Requirement
private ArtifactResolver artifactResolver; private org.sonatype.aether.RepositorySystem repoSystem;
@Requirement @Requirement
private RemoteRepositoryManager repositoryManager; private RemoteRepositoryManager repositoryManager;
@ -92,32 +93,34 @@ public class DefaultProjectBuilder
// MavenProjectBuilder Implementation // MavenProjectBuilder Implementation
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest configuration ) public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest request )
throws ProjectBuildingException throws ProjectBuildingException
{ {
return build( pomFile, new FileModelSource( pomFile ), configuration ); return build( pomFile, new FileModelSource( pomFile ), new InternalConfig( request, null, null ) );
} }
public ProjectBuildingResult build( ModelSource modelSource, ProjectBuildingRequest configuration ) public ProjectBuildingResult build( ModelSource modelSource, ProjectBuildingRequest request )
throws ProjectBuildingException throws ProjectBuildingException
{ {
return build( null, modelSource, configuration ); return build( null, modelSource, new InternalConfig( request, null, null ) );
} }
private ProjectBuildingResult build( File pomFile, ModelSource modelSource, ProjectBuildingRequest configuration ) private ProjectBuildingResult build( File pomFile, ModelSource modelSource, InternalConfig config )
throws ProjectBuildingException throws ProjectBuildingException
{ {
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try try
{ {
ProjectBuildingRequest configuration = config.request;
MavenProject project = configuration.getProject(); MavenProject project = configuration.getProject();
List<ModelProblem> modelProblems = null; List<ModelProblem> modelProblems = null;
if ( project == null ) if ( project == null )
{ {
ModelBuildingRequest request = getModelBuildingRequest( configuration, null ); ModelBuildingRequest request = getModelBuildingRequest( config );
project = new MavenProject( repositorySystem, this, configuration, logger ); project = new MavenProject( repositorySystem, this, configuration, logger );
@ -205,13 +208,13 @@ public class DefaultProjectBuilder
return ids; return ids;
} }
private ModelBuildingRequest getModelBuildingRequest( ProjectBuildingRequest configuration, private ModelBuildingRequest getModelBuildingRequest( InternalConfig config )
ReactorModelPool modelPool )
{ {
ProjectBuildingRequest configuration = config.request;
ModelResolver resolver = ModelResolver resolver =
new ProjectModelResolver( configuration.getRepositorySession(), artifactResolver, repositoryManager, new ProjectModelResolver( config.session, repoSystem, repositoryManager, config.repositories,
RepositoryUtils.toRepos( configuration.getRemoteRepositories() ), configuration.getRepositoryMerging(), config.modelPool );
configuration.getRepositoryMerging(), modelPool );
ModelBuildingRequest request = new DefaultModelBuildingRequest(); ModelBuildingRequest request = new DefaultModelBuildingRequest();
@ -224,17 +227,18 @@ public class DefaultProjectBuilder
request.setUserProperties( configuration.getUserProperties() ); request.setUserProperties( configuration.getUserProperties() );
request.setBuildStartTime( configuration.getBuildStartTime() ); request.setBuildStartTime( configuration.getBuildStartTime() );
request.setModelResolver( resolver ); request.setModelResolver( resolver );
request.setModelCache( config.modelCache );
return request; return request;
} }
public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest configuration ) public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest request )
throws ProjectBuildingException throws ProjectBuildingException
{ {
return build( artifact, false, configuration ); return build( artifact, false, request );
} }
public ProjectBuildingResult build( Artifact artifact, boolean allowStubModel, ProjectBuildingRequest configuration ) public ProjectBuildingResult build( Artifact artifact, boolean allowStubModel, ProjectBuildingRequest request )
throws ProjectBuildingException throws ProjectBuildingException
{ {
org.sonatype.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact ); org.sonatype.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact );
@ -243,27 +247,30 @@ public class DefaultProjectBuilder
pomArtifact = new SubArtifact( pomArtifact, "", "pom" ); pomArtifact = new SubArtifact( pomArtifact, "", "pom" );
} }
ArtifactResult result; InternalConfig config = new InternalConfig( request, null, null );
boolean localProject;
try try
{ {
ArtifactRequest request = new ArtifactRequest(); ArtifactRequest pomRequest = new ArtifactRequest();
request.setArtifact( pomArtifact ); pomRequest.setArtifact( pomArtifact );
request.setRepositories( RepositoryUtils.toRepos( configuration.getRemoteRepositories() ) ); pomRequest.setRepositories( config.repositories );
result = artifactResolver.resolveArtifact( configuration.getRepositorySession(), request ); ArtifactResult pomResult = repoSystem.resolveArtifact( config.session, pomRequest );
pomArtifact = result.getArtifact();
pomArtifact = pomResult.getArtifact();
localProject = pomResult.getRepository() instanceof WorkspaceRepository;
} }
catch ( org.sonatype.aether.resolution.ArtifactResolutionException e ) catch ( org.sonatype.aether.resolution.ArtifactResolutionException e )
{ {
if ( e.getResults().get( 0 ).isMissing() && allowStubModel ) if ( e.getResults().get( 0 ).isMissing() && allowStubModel )
{ {
return build( null, createStubModelSource( artifact ), configuration ); return build( null, createStubModelSource( artifact ), config );
} }
throw new ProjectBuildingException( artifact.getId(), throw new ProjectBuildingException( artifact.getId(),
"Error resolving project artifact: " + e.getMessage(), e ); "Error resolving project artifact: " + e.getMessage(), e );
} }
boolean localProject = result.getRepository() instanceof WorkspaceRepository;
File pomFile = pomArtifact.getFile(); File pomFile = pomArtifact.getFile();
if ( "pom".equals( artifact.getType() ) ) if ( "pom".equals( artifact.getType() ) )
@ -273,7 +280,7 @@ public class DefaultProjectBuilder
artifact.setResolved( true ); artifact.setResolved( true );
} }
return build( localProject ? pomFile : null, new FileModelSource( pomFile ), configuration ); return build( localProject ? pomFile : null, new FileModelSource( pomFile ), config );
} }
private ModelSource createStubModelSource( Artifact artifact ) private ModelSource createStubModelSource( Artifact artifact )
@ -292,7 +299,7 @@ public class DefaultProjectBuilder
return new StringModelSource( buffer, artifact.getId() ); return new StringModelSource( buffer, artifact.getId() );
} }
public List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest config ) public List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest request )
throws ProjectBuildingException throws ProjectBuildingException
{ {
List<ProjectBuildingResult> results = new ArrayList<ProjectBuildingResult>(); List<ProjectBuildingResult> results = new ArrayList<ProjectBuildingResult>();
@ -303,11 +310,12 @@ public class DefaultProjectBuilder
ReactorModelCache modelCache = new ReactorModelCache(); ReactorModelCache modelCache = new ReactorModelCache();
InternalConfig config = new InternalConfig( request, modelPool, modelCache );
Map<String, MavenProject> projectIndex = new HashMap<String, MavenProject>( 256 ); Map<String, MavenProject> projectIndex = new HashMap<String, MavenProject>( 256 );
boolean noErrors = boolean noErrors =
build( results, interimResults, projectIndex, pomFiles, new LinkedHashSet<File>(), true, recursive, config, build( results, interimResults, projectIndex, pomFiles, new LinkedHashSet<File>(), true, recursive, config );
modelPool, modelCache );
populateReactorModelPool( modelPool, interimResults ); populateReactorModelPool( modelPool, interimResults );
@ -316,7 +324,7 @@ public class DefaultProjectBuilder
try try
{ {
noErrors = noErrors =
build( results, new ArrayList<MavenProject>(), projectIndex, interimResults, config, build( results, new ArrayList<MavenProject>(), projectIndex, interimResults, request,
new HashMap<File, Boolean>() ) && noErrors; new HashMap<File, Boolean>() ) && noErrors;
} }
finally finally
@ -334,8 +342,7 @@ public class DefaultProjectBuilder
private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults, private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults,
Map<String, MavenProject> projectIndex, List<File> pomFiles, Set<File> aggregatorFiles, Map<String, MavenProject> projectIndex, List<File> pomFiles, Set<File> aggregatorFiles,
boolean isRoot, boolean recursive, ProjectBuildingRequest config, boolean isRoot, boolean recursive, InternalConfig config )
ReactorModelPool reactorModelPool, ReactorModelCache modelCache )
{ {
boolean noErrors = true; boolean noErrors = true;
@ -343,8 +350,7 @@ public class DefaultProjectBuilder
{ {
aggregatorFiles.add( pomFile ); aggregatorFiles.add( pomFile );
if ( !build( results, interimResults, projectIndex, pomFile, aggregatorFiles, isRoot, recursive, config, if ( !build( results, interimResults, projectIndex, pomFile, aggregatorFiles, isRoot, recursive, config ) )
reactorModelPool, modelCache ) )
{ {
noErrors = false; noErrors = false;
} }
@ -357,22 +363,20 @@ public class DefaultProjectBuilder
private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults, private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults,
Map<String, MavenProject> projectIndex, File pomFile, Set<File> aggregatorFiles, Map<String, MavenProject> projectIndex, File pomFile, Set<File> aggregatorFiles,
boolean isRoot, boolean recursive, ProjectBuildingRequest config, boolean isRoot, boolean recursive, InternalConfig config )
ReactorModelPool reactorModelPool, ReactorModelCache modelCache )
{ {
boolean noErrors = true; boolean noErrors = true;
ModelBuildingRequest request = getModelBuildingRequest( config, reactorModelPool ); ModelBuildingRequest request = getModelBuildingRequest( config );
MavenProject project = new MavenProject( repositorySystem, this, config, logger ); MavenProject project = new MavenProject( repositorySystem, this, config.request, logger );
request.setPomFile( pomFile ); request.setPomFile( pomFile );
request.setTwoPhaseBuilding( true ); request.setTwoPhaseBuilding( true );
request.setLocationTracking( true ); request.setLocationTracking( true );
request.setModelCache( modelCache );
DefaultModelBuildingListener listener = DefaultModelBuildingListener listener =
new DefaultModelBuildingListener( project, projectBuildingHelper, config ); new DefaultModelBuildingListener( project, projectBuildingHelper, config.request );
request.setModelBuildingListener( listener ); request.setModelBuildingListener( listener );
try try
@ -463,7 +467,7 @@ public class DefaultProjectBuilder
interimResult.modules = new ArrayList<InterimResult>(); interimResult.modules = new ArrayList<InterimResult>();
if ( !build( results, interimResult.modules, projectIndex, moduleFiles, aggregatorFiles, false, if ( !build( results, interimResult.modules, projectIndex, moduleFiles, aggregatorFiles, false,
recursive, config, reactorModelPool, modelCache ) ) recursive, config ) )
{ {
noErrors = false; noErrors = false;
} }
@ -519,7 +523,7 @@ public class DefaultProjectBuilder
private boolean build( List<ProjectBuildingResult> results, List<MavenProject> projects, private boolean build( List<ProjectBuildingResult> results, List<MavenProject> projects,
Map<String, MavenProject> projectIndex, List<InterimResult> interimResults, Map<String, MavenProject> projectIndex, List<InterimResult> interimResults,
ProjectBuildingRequest config, Map<File, Boolean> profilesXmls ) ProjectBuildingRequest request, Map<File, Boolean> profilesXmls )
{ {
boolean noErrors = true; boolean noErrors = true;
@ -534,7 +538,7 @@ public class DefaultProjectBuilder
List<MavenProject> modules = new ArrayList<MavenProject>(); List<MavenProject> modules = new ArrayList<MavenProject>();
noErrors = noErrors =
build( results, modules, projectIndex, interimResult.modules, config, profilesXmls ) && noErrors; build( results, modules, projectIndex, interimResult.modules, request, profilesXmls ) && noErrors;
projects.addAll( modules ); projects.addAll( modules );
projects.add( project ); projects.add( project );
@ -632,4 +636,28 @@ public class DefaultProjectBuilder
return null; return null;
} }
class InternalConfig
{
public final ProjectBuildingRequest request;
public final RepositorySystemSession session;
public final List<RemoteRepository> repositories;
public final ReactorModelPool modelPool;
public final ReactorModelCache modelCache;
public InternalConfig( ProjectBuildingRequest request, ReactorModelPool modelPool, ReactorModelCache modelCache )
{
this.request = request;
this.modelPool = modelPool;
this.modelCache = modelCache;
session = request.getRepositorySession();
repositories = RepositoryUtils.toRepos( request.getRemoteRepositories() );
}
}
} }

View File

@ -84,13 +84,13 @@ public interface ProjectBuilder
* @param pomFiles The POM files to build, must not be {@code null}. * @param pomFiles The POM files to build, must not be {@code null}.
* @param recursive {@code true} to recursively build sub modules referenced by the POM files, {@code false} to * @param recursive {@code true} to recursively build sub modules referenced by the POM files, {@code false} to
* build only the specified POM files. * build only the specified POM files.
* @param config The project builder configuration that provides further parameters, must not be {@code null}. * @param request The project builder configuration that provides further parameters, must not be {@code null}.
* @return The results of the project builder where each result corresponds to one project that was built, never * @return The results of the project builder where each result corresponds to one project that was built, never
* {@code null}. * {@code null}.
* @throws ProjectBuildingException If an error was encountered during building of any project. * @throws ProjectBuildingException If an error was encountered during building of any project.
* {@link ProjectBuildingException#getResults()} provides access to the details of the problems. * {@link ProjectBuildingException#getResults()} provides access to the details of the problems.
*/ */
List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest config ) List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest request )
throws ProjectBuildingException; throws ProjectBuildingException;
} }

View File

@ -32,9 +32,9 @@ import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.resolution.InvalidRepositoryException; import org.apache.maven.model.resolution.InvalidRepositoryException;
import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.model.resolution.UnresolvableModelException; import org.apache.maven.model.resolution.UnresolvableModelException;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession; import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact; import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.impl.ArtifactResolver;
import org.sonatype.aether.impl.RemoteRepositoryManager; import org.sonatype.aether.impl.RemoteRepositoryManager;
import org.sonatype.aether.repository.RemoteRepository; import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.repository.RepositoryPolicy; import org.sonatype.aether.repository.RepositoryPolicy;
@ -62,7 +62,7 @@ class ProjectModelResolver
private final List<RemoteRepository> externalRepositories; private final List<RemoteRepository> externalRepositories;
private final ArtifactResolver resolver; private final RepositorySystem resolver;
private final RemoteRepositoryManager remoteRepositoryManager; private final RemoteRepositoryManager remoteRepositoryManager;
@ -72,7 +72,7 @@ class ProjectModelResolver
private final ProjectBuildingRequest.RepositoryMerging repositoryMerging; private final ProjectBuildingRequest.RepositoryMerging repositoryMerging;
public ProjectModelResolver( RepositorySystemSession session, ArtifactResolver resolver, public ProjectModelResolver( RepositorySystemSession session, RepositorySystem resolver,
RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories, RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories,
ProjectBuildingRequest.RepositoryMerging repositoryMerging, ReactorModelPool modelPool ) ProjectBuildingRequest.RepositoryMerging repositoryMerging, ReactorModelPool modelPool )
{ {