From c4f0aa520906008dfdecf7243b3941a7b2eeb572 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Wed, 23 Feb 2011 20:22:03 +0000 Subject: [PATCH] o Refactored code git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1073926 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/project/DefaultProjectBuilder.java | 120 +++++++++++------- .../apache/maven/project/ProjectBuilder.java | 14 +- .../maven/project/ProjectModelResolver.java | 6 +- 3 files changed, 84 insertions(+), 56 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 30ddf0da4a..6ef729c5da 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -48,9 +48,10 @@ import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.Os; 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.repository.LocalRepositoryManager; +import org.sonatype.aether.repository.RemoteRepository; import org.sonatype.aether.repository.WorkspaceRepository; import org.sonatype.aether.resolution.ArtifactRequest; import org.sonatype.aether.resolution.ArtifactResult; @@ -59,7 +60,7 @@ import org.sonatype.aether.util.artifact.SubArtifact; /** * @version $Id$ */ -@Component(role = ProjectBuilder.class) +@Component( role = ProjectBuilder.class ) public class DefaultProjectBuilder implements ProjectBuilder { @@ -80,7 +81,7 @@ public class DefaultProjectBuilder private RepositorySystem repositorySystem; @Requirement - private ArtifactResolver artifactResolver; + private org.sonatype.aether.RepositorySystem repoSystem; @Requirement private RemoteRepositoryManager repositoryManager; @@ -92,43 +93,45 @@ public class DefaultProjectBuilder // MavenProjectBuilder Implementation // ---------------------------------------------------------------------- - public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest configuration ) + public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest request ) 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 { - 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 { ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); try { + ProjectBuildingRequest configuration = config.request; + MavenProject project = configuration.getProject(); List modelProblems = null; if ( project == null ) { - ModelBuildingRequest request = getModelBuildingRequest( configuration, null ); + ModelBuildingRequest request = getModelBuildingRequest( config ); project = new MavenProject( repositorySystem, this, configuration, logger ); DefaultModelBuildingListener listener = new DefaultModelBuildingListener( project, projectBuildingHelper, configuration ); request.setModelBuildingListener( listener ); - + request.setPomFile( pomFile ); request.setModelSource( modelSource ); request.setLocationTracking( true ); - + ModelBuildingResult result; try { @@ -205,13 +208,13 @@ public class DefaultProjectBuilder return ids; } - private ModelBuildingRequest getModelBuildingRequest( ProjectBuildingRequest configuration, - ReactorModelPool modelPool ) + private ModelBuildingRequest getModelBuildingRequest( InternalConfig config ) { + ProjectBuildingRequest configuration = config.request; + ModelResolver resolver = - new ProjectModelResolver( configuration.getRepositorySession(), artifactResolver, repositoryManager, - RepositoryUtils.toRepos( configuration.getRemoteRepositories() ), - configuration.getRepositoryMerging(), modelPool ); + new ProjectModelResolver( config.session, repoSystem, repositoryManager, config.repositories, + configuration.getRepositoryMerging(), config.modelPool ); ModelBuildingRequest request = new DefaultModelBuildingRequest(); @@ -224,17 +227,18 @@ public class DefaultProjectBuilder request.setUserProperties( configuration.getUserProperties() ); request.setBuildStartTime( configuration.getBuildStartTime() ); request.setModelResolver( resolver ); + request.setModelCache( config.modelCache ); return request; } - public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest configuration ) + public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest request ) 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 { org.sonatype.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact ); @@ -243,27 +247,30 @@ public class DefaultProjectBuilder pomArtifact = new SubArtifact( pomArtifact, "", "pom" ); } - ArtifactResult result; + InternalConfig config = new InternalConfig( request, null, null ); + + boolean localProject; + try { - ArtifactRequest request = new ArtifactRequest(); - request.setArtifact( pomArtifact ); - request.setRepositories( RepositoryUtils.toRepos( configuration.getRemoteRepositories() ) ); - result = artifactResolver.resolveArtifact( configuration.getRepositorySession(), request ); - pomArtifact = result.getArtifact(); + ArtifactRequest pomRequest = new ArtifactRequest(); + pomRequest.setArtifact( pomArtifact ); + pomRequest.setRepositories( config.repositories ); + ArtifactResult pomResult = repoSystem.resolveArtifact( config.session, pomRequest ); + + pomArtifact = pomResult.getArtifact(); + localProject = pomResult.getRepository() instanceof WorkspaceRepository; } catch ( org.sonatype.aether.resolution.ArtifactResolutionException e ) { if ( e.getResults().get( 0 ).isMissing() && allowStubModel ) { - return build( null, createStubModelSource( artifact ), configuration ); + return build( null, createStubModelSource( artifact ), config ); } throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact: " + e.getMessage(), e ); } - boolean localProject = result.getRepository() instanceof WorkspaceRepository; - File pomFile = pomArtifact.getFile(); if ( "pom".equals( artifact.getType() ) ) @@ -273,7 +280,7 @@ public class DefaultProjectBuilder 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 ) @@ -292,7 +299,7 @@ public class DefaultProjectBuilder return new StringModelSource( buffer, artifact.getId() ); } - public List build( List pomFiles, boolean recursive, ProjectBuildingRequest config ) + public List build( List pomFiles, boolean recursive, ProjectBuildingRequest request ) throws ProjectBuildingException { List results = new ArrayList(); @@ -303,11 +310,12 @@ public class DefaultProjectBuilder ReactorModelCache modelCache = new ReactorModelCache(); + InternalConfig config = new InternalConfig( request, modelPool, modelCache ); + Map projectIndex = new HashMap( 256 ); boolean noErrors = - build( results, interimResults, projectIndex, pomFiles, new LinkedHashSet(), true, recursive, config, - modelPool, modelCache ); + build( results, interimResults, projectIndex, pomFiles, new LinkedHashSet(), true, recursive, config ); populateReactorModelPool( modelPool, interimResults ); @@ -316,7 +324,7 @@ public class DefaultProjectBuilder try { noErrors = - build( results, new ArrayList(), projectIndex, interimResults, config, + build( results, new ArrayList(), projectIndex, interimResults, request, new HashMap() ) && noErrors; } finally @@ -334,8 +342,7 @@ public class DefaultProjectBuilder private boolean build( List results, List interimResults, Map projectIndex, List pomFiles, Set aggregatorFiles, - boolean isRoot, boolean recursive, ProjectBuildingRequest config, - ReactorModelPool reactorModelPool, ReactorModelCache modelCache ) + boolean isRoot, boolean recursive, InternalConfig config ) { boolean noErrors = true; @@ -343,8 +350,7 @@ public class DefaultProjectBuilder { aggregatorFiles.add( pomFile ); - if ( !build( results, interimResults, projectIndex, pomFile, aggregatorFiles, isRoot, recursive, config, - reactorModelPool, modelCache ) ) + if ( !build( results, interimResults, projectIndex, pomFile, aggregatorFiles, isRoot, recursive, config ) ) { noErrors = false; } @@ -357,22 +363,20 @@ public class DefaultProjectBuilder private boolean build( List results, List interimResults, Map projectIndex, File pomFile, Set aggregatorFiles, - boolean isRoot, boolean recursive, ProjectBuildingRequest config, - ReactorModelPool reactorModelPool, ReactorModelCache modelCache ) + boolean isRoot, boolean recursive, InternalConfig config ) { 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.setTwoPhaseBuilding( true ); request.setLocationTracking( true ); - request.setModelCache( modelCache ); DefaultModelBuildingListener listener = - new DefaultModelBuildingListener( project, projectBuildingHelper, config ); + new DefaultModelBuildingListener( project, projectBuildingHelper, config.request ); request.setModelBuildingListener( listener ); try @@ -463,7 +467,7 @@ public class DefaultProjectBuilder interimResult.modules = new ArrayList(); if ( !build( results, interimResult.modules, projectIndex, moduleFiles, aggregatorFiles, false, - recursive, config, reactorModelPool, modelCache ) ) + recursive, config ) ) { noErrors = false; } @@ -519,7 +523,7 @@ public class DefaultProjectBuilder private boolean build( List results, List projects, Map projectIndex, List interimResults, - ProjectBuildingRequest config, Map profilesXmls ) + ProjectBuildingRequest request, Map profilesXmls ) { boolean noErrors = true; @@ -534,7 +538,7 @@ public class DefaultProjectBuilder List modules = new ArrayList(); noErrors = - build( results, modules, projectIndex, interimResult.modules, config, profilesXmls ) && noErrors; + build( results, modules, projectIndex, interimResult.modules, request, profilesXmls ) && noErrors; projects.addAll( modules ); projects.add( project ); @@ -632,4 +636,28 @@ public class DefaultProjectBuilder return null; } + class InternalConfig + { + + public final ProjectBuildingRequest request; + + public final RepositorySystemSession session; + + public final List 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() ); + } + + } + } diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java index 371528942b..84e54a9fec 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java @@ -33,7 +33,7 @@ public interface ProjectBuilder /** * Builds a project descriptor from the specified POM file. - * + * * @param projectFile The POM file to build the project from, must not be {@code null}. * @param request The project building request that holds further parameters, must not be {@code null}. * @return The result of the project building, never {@code null}. @@ -44,7 +44,7 @@ public interface ProjectBuilder /** * Builds a project descriptor for the specified artifact. - * + * * @param projectArtifact The POM artifact to build the project from, must not be {@code null}. * @param request The project building request that holds further parameters, must not be {@code null}. * @return The result of the project building, never {@code null}. @@ -55,7 +55,7 @@ public interface ProjectBuilder /** * Builds a project descriptor for the specified artifact. - * + * * @param projectArtifact The POM artifact to build the project from, must not be {@code null}. * @param allowStubModel A flag controlling the case of a missing POM artifact. If {@code true} and the specified * POM artifact does not exist, a simple stub model will be returned. If {@code false}, an exception will @@ -69,7 +69,7 @@ public interface ProjectBuilder /** * Builds a project descriptor for the specified model source. - * + * * @param modelSource The source of the model to built the project descriptor from, must not be {@code null}. * @param request The project building request that holds further parameters, must not be {@code null}. * @return The result of the project building, never {@code null}. @@ -80,17 +80,17 @@ public interface ProjectBuilder /** * Builds the projects for the specified POM files and optionally their children. - * + * * @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 * 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 * {@code null}. * @throws ProjectBuildingException If an error was encountered during building of any project. * {@link ProjectBuildingException#getResults()} provides access to the details of the problems. */ - List build( List pomFiles, boolean recursive, ProjectBuildingRequest config ) + List build( List pomFiles, boolean recursive, ProjectBuildingRequest request ) throws ProjectBuildingException; } diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java b/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java index 36490710ba..985f8c667d 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java @@ -32,9 +32,9 @@ import org.apache.maven.model.building.ModelSource; import org.apache.maven.model.resolution.InvalidRepositoryException; import org.apache.maven.model.resolution.ModelResolver; import org.apache.maven.model.resolution.UnresolvableModelException; +import org.sonatype.aether.RepositorySystem; import org.sonatype.aether.RepositorySystemSession; import org.sonatype.aether.artifact.Artifact; -import org.sonatype.aether.impl.ArtifactResolver; import org.sonatype.aether.impl.RemoteRepositoryManager; import org.sonatype.aether.repository.RemoteRepository; import org.sonatype.aether.repository.RepositoryPolicy; @@ -62,7 +62,7 @@ class ProjectModelResolver private final List externalRepositories; - private final ArtifactResolver resolver; + private final RepositorySystem resolver; private final RemoteRepositoryManager remoteRepositoryManager; @@ -72,7 +72,7 @@ class ProjectModelResolver private final ProjectBuildingRequest.RepositoryMerging repositoryMerging; - public ProjectModelResolver( RepositorySystemSession session, ArtifactResolver resolver, + public ProjectModelResolver( RepositorySystemSession session, RepositorySystem resolver, RemoteRepositoryManager remoteRepositoryManager, List repositories, ProjectBuildingRequest.RepositoryMerging repositoryMerging, ReactorModelPool modelPool ) {