From 8c9c57652e22e36002df5ffddbc27bc0b8275c3d Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 29 May 2009 20:36:55 +0000 Subject: [PATCH] o Introduced new interface ProjectBuilder to supercede MavenProjectBuilder and to draw a clearer line between legacy API and new API git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@780104 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 5 +- .../project/DefaultMavenProjectBuilder.java | 635 +--------------- .../maven/project/DefaultProjectBuilder.java | 709 ++++++++++++++++++ .../apache/maven/project/MavenProject.java | 6 +- .../maven/project/MavenProjectBuilder.java | 8 +- .../apache/maven/project/ProjectBuilder.java | 37 + .../project/ProjectBuilderConfiguration.java | 2 +- .../project/artifact/MavenMetadataSource.java | 10 +- .../AbstractCoreMavenComponentTestCase.java | 5 +- .../maven/project/PomConstructionTest.java | 8 +- .../PomConstructionWithSettingsTest.java | 17 +- 11 files changed, 776 insertions(+), 666 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java create mode 100644 maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 9cfe05fc37..e1a37b18eb 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -26,7 +26,6 @@ import java.util.List; import java.util.Map; import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.exception.DefaultExceptionHandler; import org.apache.maven.exception.ExceptionHandler; @@ -40,7 +39,7 @@ import org.apache.maven.execution.ProjectSorter; import org.apache.maven.execution.RuntimeInformation; import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.repository.DelegatingLocalArtifactRepository; import org.codehaus.plexus.PlexusContainer; @@ -58,7 +57,7 @@ public class DefaultMaven implements Maven { @Requirement - protected MavenProjectBuilder projectBuilder; + protected ProjectBuilder projectBuilder; @Requirement protected LifecycleExecutor lifecycleExecutor; diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index ad6ba0348a..b9906c9cb3 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -16,48 +16,18 @@ package org.apache.maven.project; */ import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.List; -import java.util.Map; -import org.apache.maven.ProjectDependenciesResolver; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; -import org.apache.maven.artifact.resolver.ResolutionErrorHandler; -import org.apache.maven.lifecycle.LifecycleExecutionException; -import org.apache.maven.lifecycle.LifecycleExecutor; -import org.apache.maven.model.Build; -import org.apache.maven.model.Model; -import org.apache.maven.model.ModelEventListener; -import org.apache.maven.model.Parent; -import org.apache.maven.model.Profile; -import org.apache.maven.model.Repository; -import org.apache.maven.model.inheritance.InheritanceAssembler; -import org.apache.maven.model.interpolator.Interpolator; -import org.apache.maven.model.io.ModelReader; -import org.apache.maven.model.lifecycle.LifecycleBindingsInjector; -import org.apache.maven.model.management.ManagementInjector; -import org.apache.maven.model.normalization.ModelNormalizer; -import org.apache.maven.model.plugin.PluginConfigurationExpander; -import org.apache.maven.model.profile.ProfileActivationException; -import org.apache.maven.model.profile.ProfileInjector; -import org.apache.maven.model.profile.ProfileSelector; -import org.apache.maven.model.validation.ModelValidationResult; -import org.apache.maven.model.validation.ModelValidator; import org.apache.maven.profiles.ProfileManager; import org.apache.maven.project.artifact.ProjectArtifact; import org.apache.maven.repository.RepositorySystem; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; -import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.StringUtils; /** @@ -67,59 +37,13 @@ import org.codehaus.plexus.util.StringUtils; public class DefaultMavenProjectBuilder implements MavenProjectBuilder { - @Requirement - private Logger logger; @Requirement - private ModelReader modelReader; - - @Requirement - private ModelValidator validator; - - @Requirement - private LifecycleExecutor lifecycle; + private ProjectBuilder projectBuilder; @Requirement private RepositorySystem repositorySystem; - @Requirement - private List listeners; - - @Requirement - private ModelNormalizer normalizer; - - @Requirement - private InheritanceAssembler inheritanceAssembler; - - @Requirement - private Interpolator interpolator; - - @Requirement - private ProfileSelector profileSelector; - - @Requirement - private ProfileInjector profileInjector; - - @Requirement - private ManagementInjector managementInjector; - - @Requirement - private LifecycleBindingsInjector lifecycleBindingsInjector; - - @Requirement - private PluginConfigurationExpander pluginConfigurationExpander; - - @Requirement - private ResolutionErrorHandler resolutionErrorHandler; - - @Requirement - private MavenProjectCache projectCache; - - @Requirement - private ProjectDependenciesResolver projectDependenciesResolver; - - private MavenProject superProject; - // ---------------------------------------------------------------------- // MavenProjectBuilder Implementation // ---------------------------------------------------------------------- @@ -127,147 +51,13 @@ public class DefaultMavenProjectBuilder public MavenProject build( File pomFile, ProjectBuilderConfiguration configuration ) throws ProjectBuildingException { - return build( pomFile, pomFile.getParentFile(), configuration ); - } - - private MavenProject build( File pomFile, File projectDirectory, ProjectBuilderConfiguration configuration ) - throws ProjectBuildingException - { - String cacheKey = getCacheKey( pomFile, configuration ); - - MavenProject project = projectCache.get( cacheKey ); - - if ( project != null ) - { - return project; - } - - List models; - - try - { - models = build( "unknown", pomFile, configuration ); - } - catch ( IOException e ) - { - throw new ProjectBuildingException( "", "", e ); - } - - Model model = models.get(0); - - model.setProjectDirectory( projectDirectory ); - - //Profiles - // - // Active profiles can be contributed to the MavenExecutionRequest as well as from the POM - - List projectProfiles; - - try - { - projectProfiles = new ArrayList(); - projectProfiles.addAll( model.getProfiles() ); - if ( configuration.getProfiles() != null ) - { - projectProfiles.addAll( configuration.getProfiles() ); - } - projectProfiles = profileSelector.getActiveProfiles( projectProfiles, configuration ); - } - catch ( ProfileActivationException e ) - { - throw new ProjectBuildingException( model.getId(), "Failed to activate pom profiles.", e ); - } - - for ( Profile p : projectProfiles ) - { - if ( !"pom".equals( p.getSource() ) ) - { - logger.debug( "Merging profile into model (build): Model = " + model.getId() + ", Profile = " - + p.getId() ); - profileInjector.injectProfile( model, p ); - } - } - - try - { - model = interpolateModel( model, configuration, pomFile ); - - if ( configuration.isProcessPlugins() ) - { - lifecycleBindingsInjector.injectLifecycleBindings( model ); - } - - managementInjector.injectManagement( model ); - - validateModel( model, pomFile, configuration.istLenientValidation() ); - - File parentFile = ( models.size() > 1 ) ? models.get( 1 ).getPomFile() : null; - - project = fromModelToMavenProject( model, parentFile, configuration, pomFile ); - - if ( configuration.isProcessPlugins() ) - { - pluginConfigurationExpander.expandPluginConfiguration( project.getModel() ); - - lifecycle.populateDefaultConfigurationForPlugins( project.getModel().getBuild().getPlugins(), configuration.getLocalRepository(), project.getRemoteArtifactRepositories() ); - } - } - catch ( IOException e ) - { - throw new ProjectBuildingException( "", "", e ); - } - catch ( LifecycleExecutionException e ) - { - throw new ProjectBuildingException( "", e.getMessage(), e ); - } - - Build build = project.getBuild(); - // NOTE: setting this script-source root before path translation, because - // the plugin tools compose basedir and scriptSourceRoot into a single file. - project.addScriptSourceRoot( build.getScriptSourceDirectory() ); - project.addCompileSourceRoot( build.getSourceDirectory() ); - project.addTestCompileSourceRoot( build.getTestSourceDirectory() ); - project.setFile( pomFile ); - project.setActiveProfiles( projectProfiles ); - - projectCache.put( cacheKey, project ); - - return project; - } - - private String getCacheKey( File pomFile, ProjectBuilderConfiguration configuration ) - { - StringBuilder buffer = new StringBuilder( 256 ); - buffer.append( pomFile.getAbsolutePath() ); - buffer.append( '/' ).append( pomFile.lastModified() ); - return buffer.toString(); + return projectBuilder.build( pomFile, configuration ); } public MavenProject buildFromRepository( Artifact artifact, ProjectBuilderConfiguration configuration ) throws ProjectBuildingException { - if ( !artifact.getType().equals( "pom" ) ) - { - artifact = repositorySystem.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); - } - - ArtifactResolutionRequest request = new ArtifactResolutionRequest() - .setArtifact( artifact ) - .setLocalRepository( configuration.getLocalRepository() ) - .setRemoteRepostories( configuration.getRemoteRepositories() ); - - ArtifactResolutionResult result = repositorySystem.resolve( request ); - - try - { - resolutionErrorHandler.throwErrors( request, result ); - } - catch ( ArtifactResolutionException e ) - { - throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact.", e ); - } - - return build( artifact.getFile(), null, configuration ); + return projectBuilder.build( artifact, configuration ); } // This is used by the SITE plugin. @@ -305,31 +95,13 @@ public class DefaultMavenProjectBuilder public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration config ) throws ProjectBuildingException { - if ( superProject != null ) - { - return superProject; - } - - Model superModel = getSuperModel(); - - try - { - superProject = new MavenProject( superModel, repositorySystem, this, config ); - } - catch ( InvalidRepositoryException e ) - { - // Not going to happen. - } - - superProject.setExecutionRoot( true ); - - return superProject; + return projectBuilder.buildStandaloneSuperProject( config ); } public MavenProjectBuildingResult buildProjectWithDependencies( File pomFile, ProjectBuilderConfiguration configuration ) throws ProjectBuildingException { - MavenProject project = build( pomFile, configuration ); + MavenProject project = projectBuilder.build( pomFile, configuration ); Artifact artifact = new ProjectArtifact( project ); @@ -356,173 +128,6 @@ public class DefaultMavenProjectBuilder return new MavenProjectBuildingResult( project, result ); } - private Model interpolateModel( Model model, ProjectBuilderConfiguration config, File projectDescriptor ) - throws ProjectBuildingException - { - try - { - model = interpolator.interpolateModel( model, config.getExecutionProperties(), model.getProjectDirectory() ); - } - catch ( IOException e ) - { - String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); - throw new ProjectBuildingException( projectId, "", projectDescriptor, e ); - } - - return model; - } - - private MavenProject fromModelToMavenProject( Model model, File parentFile, ProjectBuilderConfiguration config, File projectDescriptor ) - throws InvalidProjectModelException, IOException - { - MavenProject project; - String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); - try - { - project = new MavenProject( model, repositorySystem, this, config ); - - Artifact projectArtifact = repositorySystem.createArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging() ); - project.setArtifact( projectArtifact ); - - project.setParentFile( parentFile ); - - } - catch ( InvalidRepositoryException e ) - { - throw new InvalidProjectModelException( projectId, e.getMessage(), projectDescriptor, e ); - } - - return project; - } - - private List build( String projectId, File pomFile, ProjectBuilderConfiguration projectBuilderConfiguration ) - throws ProjectBuildingException, IOException - { - Model mainModel = readModel( projectId, pomFile, !projectBuilderConfiguration.istLenientValidation() ); - mainModel.setProjectDirectory( pomFile.getParentFile() ); - - List domainModels = new ArrayList(); - - domainModels.add( mainModel ); - - ArtifactRepository localRepository = projectBuilderConfiguration.getLocalRepository(); - - List remoteRepositories = new ArrayList(); - try - { - for ( Profile profile : profileSelector.getActiveProfiles( projectBuilderConfiguration.getProfiles(), - projectBuilderConfiguration ) ) - { - for ( Repository repository : profile.getRepositories() ) - { - try - { - remoteRepositories.add( repositorySystem.buildArtifactRepository( repository ) ); - } - catch ( InvalidRepositoryException e ) - { - throw new ProjectBuildingException( projectId, "Failed to create remote repository " - + repository, pomFile, e ); - } - } - } - remoteRepositories = repositorySystem.getMirrors( remoteRepositories ); - } - catch ( ProfileActivationException e ) - { - throw new ProjectBuildingException( projectId, "Failed to determine active profiles", pomFile, e ); - } - remoteRepositories.addAll( projectBuilderConfiguration.getRemoteRepositories() ); - - if ( mainModel.getParent() != null ) - { - List mavenParents; - - if ( isParentLocal( mainModel.getParent().getRelativePath(), pomFile.getParentFile() ) ) - { - mavenParents = getDomainModelParentsFromLocalPath( mainModel, localRepository, remoteRepositories, pomFile.getParentFile(), projectBuilderConfiguration ); - } - else - { - mavenParents = getDomainModelParentsFromRepository( mainModel, localRepository, remoteRepositories ); - } - - domainModels.addAll( mavenParents ); - } - - for ( Model model : domainModels ) - { - normalizer.mergeDuplicates( model ); - } - - domainModels.add( getSuperModel() ); - List profileModels = new ArrayList(); - //Process Profiles - for ( Model model : domainModels ) - { - if ( !model.getProfiles().isEmpty() ) - { - Collection profiles; - try - { - profiles = - profileSelector.getActiveProfiles( model.getProfiles(), projectBuilderConfiguration ); - } - catch ( ProfileActivationException e ) - { - throw new ProjectBuildingException( projectId, "Failed to determine active profiles", pomFile, e ); - } - if ( !profiles.isEmpty() ) - { - for ( Profile p : profiles ) - { - logger.debug( "Merging profile into model: Model = " + model.getId() + ", Profile = " + p.getId() ); - profileInjector.injectProfile( model, p ); - } - } - } - profileModels.add( model ); - } - - processModelsForInheritance( profileModels ); - - return profileModels; - } - - private Model processModelsForInheritance( List models ) - { - List parentsFirst = new ArrayList( models ); - Collections.reverse( parentsFirst ); - - Model previousModel = null; - - for ( Model currentModel : parentsFirst ) - { - inheritanceAssembler.assembleModelInheritance( currentModel, previousModel ); - previousModel = currentModel; - } - - return previousModel; - } - - private void validateModel( Model model, File pomFile, boolean lenient ) - throws InvalidProjectModelException - { - // Must validate before artifact construction to make sure dependencies are good - ModelValidationResult validationResult = validator.validateEffectiveModel( model, lenient ); - - String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); - - if ( validationResult.getMessageCount() > 0 ) - { - for ( String s : validationResult.getMessages() ) - { - logger.error( s ); - } - throw new InvalidProjectModelException( projectId, "Failed to validate POM", pomFile, validationResult ); - } - } - private static String safeVersionlessKey( String groupId, String artifactId ) { String gid = groupId; @@ -542,234 +147,4 @@ public class DefaultMavenProjectBuilder return ArtifactUtils.versionlessKey( gid, aid ); } - /** - * Returns true if the relative path of the specified parent references a pom, otherwise returns - * false. - * - * @param relativePath the parent model info - * @param projectDirectory the project directory of the child pom - * @return true if the relative path of the specified parent references a pom, otherwise returns - * fals - */ - private static boolean isParentLocal( String relativePath, File projectDirectory ) - { - File f = new File( projectDirectory, relativePath ).getAbsoluteFile(); - - if ( f.isDirectory() ) - { - f = new File( f, "pom.xml" ); - } - - return f.isFile(); - } - - private List getDomainModelParentsFromRepository( Model model, ArtifactRepository localRepository, List remoteRepositories ) - throws IOException, ProjectBuildingException - { - List models = new ArrayList(); - - Parent parent = model.getParent(); - - if ( parent == null || localRepository == null ) - { - return models; - } - - Artifact artifactParent = - repositorySystem.createProjectArtifact( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); - - ArtifactResolutionRequest request = new ArtifactResolutionRequest() - .setArtifact( artifactParent ) - .setLocalRepository( localRepository ) - .setRemoteRepostories( remoteRepositories ); - - ArtifactResolutionResult result; - try - { - result = repositorySystem.resolve( request ); - } - catch ( Exception e ) - { - throw (IOException) new IOException( "The parent POM " + artifactParent + " could not be retrieved from any repository" ).initCause( e ); - } - - try - { - resolutionErrorHandler.throwErrors( request, result ); - } - catch ( ArtifactResolutionException e ) - { - throw (IOException) new IOException( "The parent POM " + artifactParent + " could not be retrieved from any repository" ).initCause( e ); - } - - Model parentModel = readModel( parent.getId(), artifactParent.getFile(), true ); - - if ( !isMatchingParent( parentModel, parent ) ) - { - //shane: what does this mean exactly and why does it occur - logger.debug( "Parent pom ids do not match: Parent File = " + artifactParent.getFile().getAbsolutePath() + ": Child ID = " + model.getId() ); - - // return domainModels; - // TODO: review the proper handling of this, can it happen at all and if should we really continue or error out? - } - - models.add( parentModel ); - - models.addAll( getDomainModelParentsFromRepository( parentModel, localRepository, remoteRepositories ) ); - return models; - } - - /** - * Returns list of domain model parents of the specified domain model. The parent domain models - * are part - * - * @param domainModel - * @param projectDirectory - * @return - * @throws IOException - * @throws ProjectBuildingException - */ - private List getDomainModelParentsFromLocalPath( Model model, ArtifactRepository localRepository, List remoteRepositories, File projectDirectory, - ProjectBuilderConfiguration projectBuilderConfiguration ) - throws IOException, ProjectBuildingException - { - List models = new ArrayList(); - - Parent parent = model.getParent(); - - if ( parent == null ) - { - return models; - } - - File parentFile = new File( projectDirectory, parent.getRelativePath() ).getCanonicalFile(); - if ( parentFile.isDirectory() ) - { - parentFile = new File( parentFile.getAbsolutePath(), "pom.xml" ); - } - - if ( !parentFile.isFile() ) - { - throw new IOException( "File does not exist: File = " + parentFile.getAbsolutePath() ); - } - - Model parentModel = readModel( parent.getId(), parentFile, true ); - parentModel.setProjectDirectory( parentFile.getParentFile() ); - - if ( !isMatchingParent( parentModel, parent ) ) - { - logger.info( "Parent pom ids do not match: Parent File = " + parentFile.getAbsolutePath() + ", Parent ID = " + parentModel.getId() + ", Child ID = " + model.getId() - + ", Expected Parent ID = " + parent.getId() ); - - List parentModels = getDomainModelParentsFromRepository( model, localRepository, remoteRepositories ); - - if ( parentModels.isEmpty() ) - { - throw new IOException( "Unable to find parent pom on local path or repo: " + parent.getId() ); - } - - models.addAll( parentModels ); - return models; - } - - models.add( parentModel ); - - if ( parentModel.getParent() != null ) - { - if ( isParentLocal( parentModel.getParent().getRelativePath(), parentFile.getParentFile() ) ) - { - models.addAll( getDomainModelParentsFromLocalPath( parentModel, localRepository, remoteRepositories, - parentFile.getParentFile(), - projectBuilderConfiguration ) ); - } - else - { - models.addAll( getDomainModelParentsFromRepository( parentModel, localRepository, remoteRepositories ) ); - } - } - - return models; - } - - private boolean isMatchingParent( Model parentModel, Parent parent ) - { - if ( parentModel.getGroupId() != null ) - { - if ( !parent.getGroupId().equals( parentModel.getGroupId() ) ) - { - return false; - } - } - else if ( parentModel.getParent() == null || !parent.getGroupId().equals( parentModel.getParent().getGroupId() ) ) - { - return false; - } - if ( !parent.getArtifactId().equals( parentModel.getArtifactId() ) ) - { - return false; - } - if ( parentModel.getVersion() != null ) - { - if ( !parent.getVersion().equals( parentModel.getVersion() ) ) - { - return false; - } - } - else if ( parentModel.getParent() == null || !parent.getVersion().equals( parentModel.getParent().getVersion() ) ) - { - return false; - } - return true; - } - - private Model readModel( String projectId, File pomFile, boolean strict ) - throws ProjectBuildingException - { - Model model; - - Map options = - Collections. singletonMap( ModelReader.IS_STRICT, Boolean.valueOf( strict ) ); - try - { - model = modelReader.read( pomFile, options ); - } - catch ( IOException e ) - { - throw new ProjectBuildingException( projectId, "Failed to read POM for " + projectId + " from " + pomFile - + ": " + e.getMessage(), pomFile, e ); - } - - validator.validateRawModel( model, !strict ); - - return model; - } - - // Super Model Handling - - private static final String MAVEN_MODEL_VERSION = "4.0.0"; - - private Model superModel; - - protected Model getSuperModel() - { - if ( superModel != null ) - { - return superModel; - } - - String superPomResource = "/org/apache/maven/project/pom-" + MAVEN_MODEL_VERSION + ".xml"; - - try - { - superModel = modelReader.read( getClass().getResourceAsStream( superPomResource ), null ); - } - catch ( IOException e ) - { - throw new IllegalStateException( "The super POM is damaged" - + ", please verify the integrity of your Maven installation", e ); - } - - return superModel; - } - } \ No newline at end of file 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 new file mode 100644 index 0000000000..83fe3a425f --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -0,0 +1,709 @@ +package org.apache.maven.project; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.artifact.InvalidRepositoryException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.ResolutionErrorHandler; +import org.apache.maven.lifecycle.LifecycleExecutionException; +import org.apache.maven.lifecycle.LifecycleExecutor; +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.model.Parent; +import org.apache.maven.model.Profile; +import org.apache.maven.model.Repository; +import org.apache.maven.model.inheritance.InheritanceAssembler; +import org.apache.maven.model.interpolator.Interpolator; +import org.apache.maven.model.io.ModelReader; +import org.apache.maven.model.lifecycle.LifecycleBindingsInjector; +import org.apache.maven.model.management.ManagementInjector; +import org.apache.maven.model.normalization.ModelNormalizer; +import org.apache.maven.model.plugin.PluginConfigurationExpander; +import org.apache.maven.model.profile.ProfileActivationException; +import org.apache.maven.model.profile.ProfileInjector; +import org.apache.maven.model.profile.ProfileSelector; +import org.apache.maven.model.validation.ModelValidationResult; +import org.apache.maven.model.validation.ModelValidator; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.util.StringUtils; + +/** + * @version $Id$ + */ +@Component(role = ProjectBuilder.class) +public class DefaultProjectBuilder + implements ProjectBuilder +{ + @Requirement + private Logger logger; + + @Requirement + private ModelReader modelReader; + + @Requirement + private ModelValidator validator; + + @Requirement + private LifecycleExecutor lifecycle; + + @Requirement + private RepositorySystem repositorySystem; + + @Requirement + private ModelNormalizer normalizer; + + @Requirement + private InheritanceAssembler inheritanceAssembler; + + @Requirement + private Interpolator interpolator; + + @Requirement + private ProfileSelector profileSelector; + + @Requirement + private ProfileInjector profileInjector; + + @Requirement + private ManagementInjector managementInjector; + + @Requirement + private LifecycleBindingsInjector lifecycleBindingsInjector; + + @Requirement + private PluginConfigurationExpander pluginConfigurationExpander; + + @Requirement + private ResolutionErrorHandler resolutionErrorHandler; + + @Requirement + private MavenProjectCache projectCache; + + private MavenProject superProject; + + // ---------------------------------------------------------------------- + // MavenProjectBuilder Implementation + // ---------------------------------------------------------------------- + + public MavenProject build( File pomFile, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException + { + return build( pomFile, pomFile.getParentFile(), configuration ); + } + + private MavenProject build( File pomFile, File projectDirectory, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException + { + String cacheKey = getCacheKey( pomFile, configuration ); + + MavenProject project = projectCache.get( cacheKey ); + + if ( project != null ) + { + return project; + } + + List models; + + try + { + models = build( "unknown", pomFile, configuration ); + } + catch ( IOException e ) + { + throw new ProjectBuildingException( "", "", e ); + } + + Model model = models.get(0); + + model.setProjectDirectory( projectDirectory ); + + //Profiles + // + // Active profiles can be contributed to the MavenExecutionRequest as well as from the POM + + List projectProfiles; + + try + { + projectProfiles = new ArrayList(); + projectProfiles.addAll( model.getProfiles() ); + if ( configuration.getProfiles() != null ) + { + projectProfiles.addAll( configuration.getProfiles() ); + } + projectProfiles = profileSelector.getActiveProfiles( projectProfiles, configuration ); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( model.getId(), "Failed to activate pom profiles.", e ); + } + + for ( Profile p : projectProfiles ) + { + if ( !"pom".equals( p.getSource() ) ) + { + logger.debug( "Merging profile into model (build): Model = " + model.getId() + ", Profile = " + + p.getId() ); + profileInjector.injectProfile( model, p ); + } + } + + try + { + model = interpolateModel( model, configuration, pomFile ); + + if ( configuration.isProcessPlugins() ) + { + lifecycleBindingsInjector.injectLifecycleBindings( model ); + } + + managementInjector.injectManagement( model ); + + validateModel( model, pomFile, configuration.istLenientValidation() ); + + File parentFile = ( models.size() > 1 ) ? models.get( 1 ).getPomFile() : null; + + project = fromModelToMavenProject( model, parentFile, configuration, pomFile ); + + if ( configuration.isProcessPlugins() ) + { + pluginConfigurationExpander.expandPluginConfiguration( project.getModel() ); + + lifecycle.populateDefaultConfigurationForPlugins( project.getModel().getBuild().getPlugins(), configuration.getLocalRepository(), project.getRemoteArtifactRepositories() ); + } + } + catch ( IOException e ) + { + throw new ProjectBuildingException( "", "", e ); + } + catch ( LifecycleExecutionException e ) + { + throw new ProjectBuildingException( "", e.getMessage(), e ); + } + + Build build = project.getBuild(); + // NOTE: setting this script-source root before path translation, because + // the plugin tools compose basedir and scriptSourceRoot into a single file. + project.addScriptSourceRoot( build.getScriptSourceDirectory() ); + project.addCompileSourceRoot( build.getSourceDirectory() ); + project.addTestCompileSourceRoot( build.getTestSourceDirectory() ); + project.setFile( pomFile ); + project.setActiveProfiles( projectProfiles ); + + projectCache.put( cacheKey, project ); + + return project; + } + + private String getCacheKey( File pomFile, ProjectBuilderConfiguration configuration ) + { + StringBuilder buffer = new StringBuilder( 256 ); + buffer.append( pomFile.getAbsolutePath() ); + buffer.append( '/' ).append( pomFile.lastModified() ); + return buffer.toString(); + } + + public MavenProject build( Artifact artifact, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException + { + if ( !artifact.getType().equals( "pom" ) ) + { + artifact = repositorySystem.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); + } + + ArtifactResolutionRequest request = new ArtifactResolutionRequest() + .setArtifact( artifact ) + .setLocalRepository( configuration.getLocalRepository() ) + .setRemoteRepostories( configuration.getRemoteRepositories() ); + + ArtifactResolutionResult result = repositorySystem.resolve( request ); + + try + { + resolutionErrorHandler.throwErrors( request, result ); + } + catch ( ArtifactResolutionException e ) + { + throw new ProjectBuildingException( artifact.getId(), "Error resolving project artifact.", e ); + } + + return build( artifact.getFile(), null, configuration ); + } + + /** + * This is used for pom-less execution like running archetype:generate. + * + * I am taking out the profile handling and the interpolation of the base directory until we + * spec this out properly. + */ + public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration config ) + throws ProjectBuildingException + { + if ( superProject != null ) + { + return superProject; + } + + Model superModel = getSuperModel(); + + try + { + superProject = new MavenProject( superModel, repositorySystem, this, config ); + } + catch ( InvalidRepositoryException e ) + { + // Not going to happen. + } + + superProject.setExecutionRoot( true ); + + return superProject; + } + + private Model interpolateModel( Model model, ProjectBuilderConfiguration config, File projectDescriptor ) + throws ProjectBuildingException + { + try + { + model = interpolator.interpolateModel( model, config.getExecutionProperties(), model.getProjectDirectory() ); + } + catch ( IOException e ) + { + String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); + throw new ProjectBuildingException( projectId, "", projectDescriptor, e ); + } + + return model; + } + + private MavenProject fromModelToMavenProject( Model model, File parentFile, ProjectBuilderConfiguration config, File projectDescriptor ) + throws InvalidProjectModelException, IOException + { + MavenProject project; + String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); + try + { + project = new MavenProject( model, repositorySystem, this, config ); + + Artifact projectArtifact = repositorySystem.createArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging() ); + project.setArtifact( projectArtifact ); + + project.setParentFile( parentFile ); + + } + catch ( InvalidRepositoryException e ) + { + throw new InvalidProjectModelException( projectId, e.getMessage(), projectDescriptor, e ); + } + + return project; + } + + private List build( String projectId, File pomFile, ProjectBuilderConfiguration projectBuilderConfiguration ) + throws ProjectBuildingException, IOException + { + Model mainModel = readModel( projectId, pomFile, !projectBuilderConfiguration.istLenientValidation() ); + mainModel.setProjectDirectory( pomFile.getParentFile() ); + + List domainModels = new ArrayList(); + + domainModels.add( mainModel ); + + ArtifactRepository localRepository = projectBuilderConfiguration.getLocalRepository(); + + List remoteRepositories = new ArrayList(); + try + { + for ( Profile profile : profileSelector.getActiveProfiles( projectBuilderConfiguration.getProfiles(), + projectBuilderConfiguration ) ) + { + for ( Repository repository : profile.getRepositories() ) + { + try + { + remoteRepositories.add( repositorySystem.buildArtifactRepository( repository ) ); + } + catch ( InvalidRepositoryException e ) + { + throw new ProjectBuildingException( projectId, "Failed to create remote repository " + + repository, pomFile, e ); + } + } + } + remoteRepositories = repositorySystem.getMirrors( remoteRepositories ); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( projectId, "Failed to determine active profiles", pomFile, e ); + } + remoteRepositories.addAll( projectBuilderConfiguration.getRemoteRepositories() ); + + if ( mainModel.getParent() != null ) + { + List mavenParents; + + if ( isParentLocal( mainModel.getParent().getRelativePath(), pomFile.getParentFile() ) ) + { + mavenParents = getDomainModelParentsFromLocalPath( mainModel, localRepository, remoteRepositories, pomFile.getParentFile(), projectBuilderConfiguration ); + } + else + { + mavenParents = getDomainModelParentsFromRepository( mainModel, localRepository, remoteRepositories ); + } + + domainModels.addAll( mavenParents ); + } + + for ( Model model : domainModels ) + { + normalizer.mergeDuplicates( model ); + } + + domainModels.add( getSuperModel() ); + List profileModels = new ArrayList(); + //Process Profiles + for ( Model model : domainModels ) + { + if ( !model.getProfiles().isEmpty() ) + { + Collection profiles; + try + { + profiles = + profileSelector.getActiveProfiles( model.getProfiles(), projectBuilderConfiguration ); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( projectId, "Failed to determine active profiles", pomFile, e ); + } + if ( !profiles.isEmpty() ) + { + for ( Profile p : profiles ) + { + logger.debug( "Merging profile into model: Model = " + model.getId() + ", Profile = " + p.getId() ); + profileInjector.injectProfile( model, p ); + } + } + } + profileModels.add( model ); + } + + processModelsForInheritance( profileModels ); + + return profileModels; + } + + private Model processModelsForInheritance( List models ) + { + List parentsFirst = new ArrayList( models ); + Collections.reverse( parentsFirst ); + + Model previousModel = null; + + for ( Model currentModel : parentsFirst ) + { + inheritanceAssembler.assembleModelInheritance( currentModel, previousModel ); + previousModel = currentModel; + } + + return previousModel; + } + + private void validateModel( Model model, File pomFile, boolean lenient ) + throws InvalidProjectModelException + { + // Must validate before artifact construction to make sure dependencies are good + ModelValidationResult validationResult = validator.validateEffectiveModel( model, lenient ); + + String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); + + if ( validationResult.getMessageCount() > 0 ) + { + for ( String s : validationResult.getMessages() ) + { + logger.error( s ); + } + throw new InvalidProjectModelException( projectId, "Failed to validate POM", pomFile, validationResult ); + } + } + + private static String safeVersionlessKey( String groupId, String artifactId ) + { + String gid = groupId; + + if ( StringUtils.isEmpty( gid ) ) + { + gid = "unknown"; + } + + String aid = artifactId; + + if ( StringUtils.isEmpty( aid ) ) + { + aid = "unknown"; + } + + return ArtifactUtils.versionlessKey( gid, aid ); + } + + /** + * Returns true if the relative path of the specified parent references a pom, otherwise returns + * false. + * + * @param relativePath the parent model info + * @param projectDirectory the project directory of the child pom + * @return true if the relative path of the specified parent references a pom, otherwise returns + * fals + */ + private static boolean isParentLocal( String relativePath, File projectDirectory ) + { + File f = new File( projectDirectory, relativePath ).getAbsoluteFile(); + + if ( f.isDirectory() ) + { + f = new File( f, "pom.xml" ); + } + + return f.isFile(); + } + + private List getDomainModelParentsFromRepository( Model model, ArtifactRepository localRepository, List remoteRepositories ) + throws IOException, ProjectBuildingException + { + List models = new ArrayList(); + + Parent parent = model.getParent(); + + if ( parent == null || localRepository == null ) + { + return models; + } + + Artifact artifactParent = + repositorySystem.createProjectArtifact( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); + + ArtifactResolutionRequest request = new ArtifactResolutionRequest() + .setArtifact( artifactParent ) + .setLocalRepository( localRepository ) + .setRemoteRepostories( remoteRepositories ); + + ArtifactResolutionResult result; + try + { + result = repositorySystem.resolve( request ); + } + catch ( Exception e ) + { + throw (IOException) new IOException( "The parent POM " + artifactParent + " could not be retrieved from any repository" ).initCause( e ); + } + + try + { + resolutionErrorHandler.throwErrors( request, result ); + } + catch ( ArtifactResolutionException e ) + { + throw (IOException) new IOException( "The parent POM " + artifactParent + " could not be retrieved from any repository" ).initCause( e ); + } + + Model parentModel = readModel( parent.getId(), artifactParent.getFile(), true ); + + if ( !isMatchingParent( parentModel, parent ) ) + { + //shane: what does this mean exactly and why does it occur + logger.debug( "Parent pom ids do not match: Parent File = " + artifactParent.getFile().getAbsolutePath() + ": Child ID = " + model.getId() ); + + // return domainModels; + // TODO: review the proper handling of this, can it happen at all and if should we really continue or error out? + } + + models.add( parentModel ); + + models.addAll( getDomainModelParentsFromRepository( parentModel, localRepository, remoteRepositories ) ); + return models; + } + + /** + * Returns list of domain model parents of the specified domain model. The parent domain models + * are part + * + * @param domainModel + * @param projectDirectory + * @return + * @throws IOException + * @throws ProjectBuildingException + */ + private List getDomainModelParentsFromLocalPath( Model model, ArtifactRepository localRepository, List remoteRepositories, File projectDirectory, + ProjectBuilderConfiguration projectBuilderConfiguration ) + throws IOException, ProjectBuildingException + { + List models = new ArrayList(); + + Parent parent = model.getParent(); + + if ( parent == null ) + { + return models; + } + + File parentFile = new File( projectDirectory, parent.getRelativePath() ).getCanonicalFile(); + if ( parentFile.isDirectory() ) + { + parentFile = new File( parentFile.getAbsolutePath(), "pom.xml" ); + } + + if ( !parentFile.isFile() ) + { + throw new IOException( "File does not exist: File = " + parentFile.getAbsolutePath() ); + } + + Model parentModel = readModel( parent.getId(), parentFile, true ); + parentModel.setProjectDirectory( parentFile.getParentFile() ); + + if ( !isMatchingParent( parentModel, parent ) ) + { + logger.info( "Parent pom ids do not match: Parent File = " + parentFile.getAbsolutePath() + ", Parent ID = " + parentModel.getId() + ", Child ID = " + model.getId() + + ", Expected Parent ID = " + parent.getId() ); + + List parentModels = getDomainModelParentsFromRepository( model, localRepository, remoteRepositories ); + + if ( parentModels.isEmpty() ) + { + throw new IOException( "Unable to find parent pom on local path or repo: " + parent.getId() ); + } + + models.addAll( parentModels ); + return models; + } + + models.add( parentModel ); + + if ( parentModel.getParent() != null ) + { + if ( isParentLocal( parentModel.getParent().getRelativePath(), parentFile.getParentFile() ) ) + { + models.addAll( getDomainModelParentsFromLocalPath( parentModel, localRepository, remoteRepositories, + parentFile.getParentFile(), + projectBuilderConfiguration ) ); + } + else + { + models.addAll( getDomainModelParentsFromRepository( parentModel, localRepository, remoteRepositories ) ); + } + } + + return models; + } + + private boolean isMatchingParent( Model parentModel, Parent parent ) + { + if ( parentModel.getGroupId() != null ) + { + if ( !parent.getGroupId().equals( parentModel.getGroupId() ) ) + { + return false; + } + } + else if ( parentModel.getParent() == null || !parent.getGroupId().equals( parentModel.getParent().getGroupId() ) ) + { + return false; + } + if ( !parent.getArtifactId().equals( parentModel.getArtifactId() ) ) + { + return false; + } + if ( parentModel.getVersion() != null ) + { + if ( !parent.getVersion().equals( parentModel.getVersion() ) ) + { + return false; + } + } + else if ( parentModel.getParent() == null || !parent.getVersion().equals( parentModel.getParent().getVersion() ) ) + { + return false; + } + return true; + } + + private Model readModel( String projectId, File pomFile, boolean strict ) + throws ProjectBuildingException + { + Model model; + + Map options = + Collections. singletonMap( ModelReader.IS_STRICT, Boolean.valueOf( strict ) ); + try + { + model = modelReader.read( pomFile, options ); + } + catch ( IOException e ) + { + throw new ProjectBuildingException( projectId, "Failed to read POM for " + projectId + " from " + pomFile + + ": " + e.getMessage(), pomFile, e ); + } + + validator.validateRawModel( model, !strict ); + + return model; + } + + // Super Model Handling + + private static final String MAVEN_MODEL_VERSION = "4.0.0"; + + private Model superModel; + + protected Model getSuperModel() + { + if ( superModel != null ) + { + return superModel; + } + + String superPomResource = "/org/apache/maven/project/pom-" + MAVEN_MODEL_VERSION + ".xml"; + + try + { + superModel = modelReader.read( getClass().getResourceAsStream( superPomResource ), null ); + } + catch ( IOException e ) + { + throw new IllegalStateException( "The super POM is damaged" + + ", please verify the integrity of your Maven installation", e ); + } + + return superModel; + } + +} \ No newline at end of file diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 5a20200997..38bbf5bd2d 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -151,7 +151,7 @@ public class MavenProject private Stack previousExecutionProjects = new Stack(); - private MavenProjectBuilder mavenProjectBuilder; + private ProjectBuilder mavenProjectBuilder; private ProjectBuilderConfiguration projectBuilderConfiguration; @@ -212,7 +212,7 @@ public class MavenProject * @param projectBuilderConfiguration * @throws InvalidRepositoryException */ - public MavenProject( Model model, RepositorySystem repositorySystem, MavenProjectBuilder mavenProjectBuilder, ProjectBuilderConfiguration projectBuilderConfiguration ) + public MavenProject( Model model, RepositorySystem repositorySystem, ProjectBuilder mavenProjectBuilder, ProjectBuilderConfiguration projectBuilderConfiguration ) throws InvalidRepositoryException { if ( model == null ) @@ -366,7 +366,7 @@ public class MavenProject { try { - parent = mavenProjectBuilder.buildFromRepository( getParentArtifact(), projectBuilderConfiguration ); + parent = mavenProjectBuilder.build( getParentArtifact(), projectBuilderConfiguration ); } catch ( ProjectBuildingException e ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java index 60108c2f9b..dc48833a50 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProjectBuilder.java @@ -22,26 +22,22 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.profiles.ProfileManager; +@Deprecated public interface MavenProjectBuilder { + MavenProject build( File projectFile, ProjectBuilderConfiguration configuration ) throws ProjectBuildingException; - MavenProject buildFromRepository( Artifact projectArtifact, ProjectBuilderConfiguration configuration ) - throws ProjectBuildingException; - //TODO maven-site-plugin -- Vincent, Dennis and Lukas are checking but this doesn't appear to be required anymore. - @Deprecated MavenProject build( File project, ArtifactRepository localRepository, ProfileManager profileManager ) throws ProjectBuildingException; //TODO remote-resources-plugin - @Deprecated MavenProject buildFromRepository( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ProjectBuildingException; //TODO remote-resources-plugin - @Deprecated MavenProject buildFromRepository( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository, boolean force ) throws ProjectBuildingException; 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 new file mode 100644 index 0000000000..e0713f4177 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilder.java @@ -0,0 +1,37 @@ +package org.apache.maven.project; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +import java.io.File; + +import org.apache.maven.artifact.Artifact; + +public interface ProjectBuilder +{ + + MavenProject build( File projectFile, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException; + + MavenProject build( Artifact projectArtifact, ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException; + + // TODO: this is only to provide a project for plugins that don't need a project to execute but need some + // of the values from a MavenProject. Ideally this should be something internal and nothing outside Maven + // would ever need this so it should not be exposed in a public API + MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration ) + throws ProjectBuildingException; + +} diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java index 62e8b18bb4..3c7639f048 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuilderConfiguration.java @@ -51,7 +51,7 @@ public interface ProjectBuilderConfiguration // Profiles /** - * Set any active profiles that the {@link MavenProjectBuilder} should consider while constructing + * Set any active profiles that the {@link ProjectBuilder} should consider while constructing * a {@link MavenProject}. */ void setActiveProfileIds( List activeProfileIds ); diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index 55f79f43ab..07cedbf4ae 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -39,7 +39,7 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.model.Dependency; import org.apache.maven.project.DefaultProjectBuilderConfiguration; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuilderConfiguration; import org.apache.maven.project.ProjectBuildingException; import org.codehaus.plexus.PlexusContainer; @@ -63,7 +63,7 @@ public class MavenMetadataSource //TODO: This prevents a cycle in the composition which shows us another problem we need to deal with. //@Requirement - private MavenProjectBuilder projectBuilder; + private ProjectBuilder projectBuilder; @Requirement private PlexusContainer container; @@ -118,7 +118,7 @@ public class MavenMetadataSource try { - dependencies = getProjectBuilder().buildFromRepository( pomArtifact, configuration ).getDependencies(); + dependencies = getProjectBuilder().build( pomArtifact, configuration ).getDependencies(); } catch ( ProjectBuildingException e ) { @@ -296,7 +296,7 @@ public class MavenMetadataSource return artifacts; } - public MavenProjectBuilder getProjectBuilder() + public ProjectBuilder getProjectBuilder() { if ( projectBuilder != null ) { @@ -305,7 +305,7 @@ public class MavenMetadataSource try { - projectBuilder = container.lookup( MavenProjectBuilder.class ); + projectBuilder = container.lookup( ProjectBuilder.class ); } catch ( ComponentLookupException e ) { diff --git a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java index 8b528ab060..03e8027f19 100644 --- a/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/AbstractCoreMavenComponentTestCase.java @@ -20,7 +20,6 @@ import org.apache.maven.model.Plugin; import org.apache.maven.plugin.PluginManager; import org.apache.maven.project.DefaultProjectBuilderConfiguration; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.project.ProjectBuilderConfiguration; import org.apache.maven.repository.RepositorySystem; import org.codehaus.plexus.ContainerConfiguration; @@ -35,13 +34,13 @@ public abstract class AbstractCoreMavenComponentTestCase protected RepositorySystem repositorySystem; @Requirement - protected MavenProjectBuilder projectBuilder; + protected org.apache.maven.project.ProjectBuilder projectBuilder; protected void setUp() throws Exception { repositorySystem = lookup( RepositorySystem.class ); - projectBuilder = lookup( MavenProjectBuilder.class ); + projectBuilder = lookup( org.apache.maven.project.ProjectBuilder.class ); } @Override diff --git a/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java b/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java index 24bca297ca..9bf736d53e 100644 --- a/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/PomConstructionTest.java @@ -40,7 +40,7 @@ public class PomConstructionTest private static String BASE_MIXIN_DIR = BASE_DIR + "/resources-mixins"; - private DefaultMavenProjectBuilder mavenProjectBuilder; + private DefaultProjectBuilder projectBuilder; private File testDirectory; @@ -49,14 +49,14 @@ public class PomConstructionTest { testDirectory = new File( getBasedir(), BASE_POM_DIR ); new File( getBasedir(), BASE_MIXIN_DIR ); - mavenProjectBuilder = (DefaultMavenProjectBuilder) lookup( MavenProjectBuilder.class ); + projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class ); } @Override protected void tearDown() throws Exception { - mavenProjectBuilder = null; + projectBuilder = null; super.tearDown(); } @@ -1488,7 +1488,7 @@ public class PomConstructionTest config.setActiveProfileIds( Arrays.asList( profileIds ) ); config.setExecutionProperties( executionProperties ); - return new PomTestWrapper( pomFile, mavenProjectBuilder.build( pomFile, config ) ); + return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ) ); } protected void assertModelEquals( PomTestWrapper pom, Object expected, String expression ) diff --git a/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java b/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java index 27f2c2513d..e44046abff 100644 --- a/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java +++ b/maven-core/src/test/java/org/apache/maven/settings/PomConstructionWithSettingsTest.java @@ -3,18 +3,13 @@ package org.apache.maven.settings; import java.io.File; import java.io.IOException; import java.io.Reader; -import java.util.Arrays; -import java.util.List; import org.apache.maven.artifact.repository.DefaultArtifactRepository; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; import org.apache.maven.model.Profile; -import org.apache.maven.profiles.DefaultProfileManager; -import org.apache.maven.profiles.ProfileActivationContext; -import org.apache.maven.profiles.ProfileManager; -import org.apache.maven.project.DefaultMavenProjectBuilder; +import org.apache.maven.project.DefaultProjectBuilder; import org.apache.maven.project.DefaultProjectBuilderConfiguration; -import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuilderConfiguration; import org.apache.maven.project.harness.PomTestWrapper; import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; @@ -30,7 +25,7 @@ public class PomConstructionWithSettingsTest private static String BASE_POM_DIR = BASE_DIR + "/resources-settings"; - private DefaultMavenProjectBuilder mavenProjectBuilder; + private DefaultProjectBuilder projectBuilder; private File testDirectory; @@ -38,12 +33,12 @@ public class PomConstructionWithSettingsTest throws Exception { testDirectory = new File( getBasedir(), BASE_POM_DIR ); - mavenProjectBuilder = (DefaultMavenProjectBuilder) lookup( MavenProjectBuilder.class ); + projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class ); } @Override protected void tearDown() throws Exception { - mavenProjectBuilder = null; + projectBuilder = null; super.tearDown(); } @@ -91,7 +86,7 @@ public class PomConstructionWithSettingsTest config.setLocalRepository( new DefaultArtifactRepository( "local", localRepoUrl, new DefaultRepositoryLayout() ) ); config.setActiveProfileIds( settings.getActiveProfiles() ); - return new PomTestWrapper( pomFile, mavenProjectBuilder.build( pomFile, config ) ); + return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ) ); } private static Settings readSettingsFile(File settingsFile)