From db49936302fed89159a2170fbeeb7cc8223091da Mon Sep 17 00:00:00 2001 From: Britton Isbell Date: Fri, 3 Apr 2009 10:56:06 +0000 Subject: [PATCH] Separated out the interpolation from the build. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@761609 13f79535-47bb-0310-9956-ffa450edef68 --- .../project/DefaultMavenProjectBuilder.java | 122 ++++++++++++------ .../apache/maven/project/MavenProject.java | 2 +- 2 files changed, 84 insertions(+), 40 deletions(-) diff --git a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 5910535acd..1d17e0be77 100644 --- a/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -123,6 +123,7 @@ public class DefaultMavenProjectBuilder public MavenProject build( File pomFile, ProjectBuilderConfiguration configuration ) throws ProjectBuildingException { + //Do inheritance PomClassicDomainModel domainModel; try { @@ -132,8 +133,33 @@ public class DefaultMavenProjectBuilder { throw new ProjectBuildingException("", "", e); } - - MavenProject project = buildWithProfiles( domainModel, configuration, pomFile ); + + //Profiles + List projectProfiles; + try + { + projectProfiles = ProfileContext.getActiveProfilesFrom(configuration, domainModel.getModel(), container); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( "", "Failed to activate pom profiles."); + } + catch(IOException e) + { + throw new ProjectBuildingException( "", "Failed to activate pom profiles."); + } + + try + { + domainModel = ProcessorContext.mergeProfilesIntoModel( projectProfiles, domainModel ); + } + catch ( IOException e ) + { + throw new ProjectBuildingException("", ""); + } + //Interpolation + MavenProject project = interpolateDomainModel( domainModel, configuration, pomFile ); + project.setActiveProfiles( projectProfiles ); Build build = project.getBuild(); // NOTE: setting this script-source root before path translation, because @@ -174,6 +200,11 @@ public class DefaultMavenProjectBuilder { return project; } + + if(configuration.getRemoteRepositories() == null) + { + throw new IllegalArgumentException("configuration.getRemoteRepositories(): null"); + } ArtifactResolutionRequest request = new ArtifactResolutionRequest( artifact, configuration.getLocalRepository(), configuration.getRemoteRepositories() ); ArtifactResolutionResult result = repositorySystem.resolve( request ); @@ -195,9 +226,31 @@ public class DefaultMavenProjectBuilder { throw new ProjectBuildingException(artifact.getId(), "Error reading project artifact.", e); } - - project = buildWithProfiles( domainModel, configuration, artifact.getFile() ); + List projectProfiles; + try + { + projectProfiles = ProfileContext.getActiveProfilesFrom(configuration, domainModel.getModel(), container); + } + catch ( ProfileActivationException e ) + { + throw new ProjectBuildingException( "", "Failed to activate pom profiles."); + } + catch(IOException e) + { + throw new ProjectBuildingException( "", "Failed to activate pom profiles."); + } + + try + { + domainModel = ProcessorContext.mergeProfilesIntoModel( projectProfiles, domainModel ); + } + catch ( IOException e ) + { + throw new ProjectBuildingException("", ""); + } + project = interpolateDomainModel( domainModel, configuration, artifact.getFile() ); + project.setActiveProfiles( projectProfiles ); artifact.setFile( artifact.getFile() ); project.setVersion( artifact.getVersion() ); @@ -210,6 +263,10 @@ public class DefaultMavenProjectBuilder public MavenProject buildFromRepository( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ProjectBuildingException { + if(remoteRepositories == null) + { + throw new IllegalArgumentException("repositories: null"); + } ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration() .setLocalRepository( localRepository ) .setRemoteRepositories(remoteRepositories); @@ -290,8 +347,8 @@ public class DefaultMavenProjectBuilder return new MavenProjectBuildingResult( project, result ); } - - private MavenProject buildWithProfiles( PomClassicDomainModel domainModel, ProjectBuilderConfiguration config, File projectDescriptor ) + + private MavenProject interpolateDomainModel( PomClassicDomainModel domainModel, ProjectBuilderConfiguration config, File projectDescriptor ) throws ProjectBuildingException { Model model; @@ -303,20 +360,8 @@ public class DefaultMavenProjectBuilder { throw new ProjectBuildingException("", e.getMessage()); } - File parentDescriptor = domainModel.getParentFile(); String projectId = safeVersionlessKey( model.getGroupId(), model.getArtifactId() ); - - List projectProfiles; - try - { - projectProfiles = ProfileContext.getActiveProfilesFrom(config, model, container); - } - catch ( ProfileActivationException e ) - { - throw new ProjectBuildingException( projectId, "Failed to activate pom profiles.", projectDescriptor, - e ); - } List interpolatorProperties = new ArrayList(); interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( config.getExecutionProperties(), PomInterpolatorTag.EXECUTION_PROPERTIES.name() ) ); @@ -326,25 +371,26 @@ public class DefaultMavenProjectBuilder { interpolatorProperties.add( new InterpolatorProperty( "${build.timestamp}", new SimpleDateFormat( "yyyyMMdd-hhmm" ).format( config.getBuildStartTime() ), PomInterpolatorTag.PROJECT_PROPERTIES.name() ) ); - } - - try - { - PomClassicDomainModel dm = ProcessorContext.mergeProfilesIntoModel( projectProfiles, domainModel ); - List mps = dm.getModelProperties(); - ProcessorContext.interpolateModelProperties( mps, interpolatorProperties, dm ); - if ( dm.getProjectDirectory() != null ) + } + try { - mps = ProcessorContext.alignPaths( mps, dm.getProjectDirectory() ); - } - dm = new PomClassicDomainModel( mps, false ); - model = dm.getModel(); - } - catch ( IOException e ) - { + List mps = domainModel.getModelProperties(); + ProcessorContext.interpolateModelProperties( mps, interpolatorProperties, domainModel ); + if ( domainModel.getProjectDirectory() != null ) + { + mps = ProcessorContext.alignPaths( mps, domainModel.getProjectDirectory() ); + } + File f = domainModel.getParentFile(); + domainModel = new PomClassicDomainModel( mps, false ); + domainModel.setParentFile(f); + model = domainModel.getModel(); - throw new ProjectBuildingException( projectId, "", projectDescriptor, e ); - } + } + catch ( IOException e ) + { + + throw new ProjectBuildingException(projectId, "", projectDescriptor, e); + } MavenProject project; @@ -357,7 +403,7 @@ public class DefaultMavenProjectBuilder Artifact projectArtifact = repositorySystem.createArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging() ); project.setArtifact( projectArtifact ); - project.setParentFile( parentDescriptor ); + project.setParentFile( domainModel.getParentFile() ); } catch ( InvalidRepositoryException e ) @@ -365,8 +411,6 @@ public class DefaultMavenProjectBuilder throw new InvalidProjectModelException( projectId, e.getMessage(), projectDescriptor, e ); } - project.setActiveProfiles( projectProfiles ); - return project; } @@ -429,7 +473,7 @@ public class DefaultMavenProjectBuilder Collection profiles = profileContext1.getActiveProfiles(); if(!profiles.isEmpty()) { - profileModels.add(ProcessorContext.mergeProfilesIntoModel( profileContext1.getActiveProfiles(), dm )); + profileModels.add(ProcessorContext.mergeProfilesIntoModel( profiles, dm )); } else { diff --git a/maven-project/src/main/java/org/apache/maven/project/MavenProject.java b/maven-project/src/main/java/org/apache/maven/project/MavenProject.java index b884d77b71..cf197a04ba 100644 --- a/maven-project/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-project/src/main/java/org/apache/maven/project/MavenProject.java @@ -240,7 +240,7 @@ public class MavenProject } */ - setRemoteArtifactRepositories( projectBuilderConfiguration.getRemoteRepositories() ); + setRemoteArtifactRepositories( (projectBuilderConfiguration.getRemoteRepositories() != null) ? projectBuilderConfiguration.getRemoteRepositories() : new ArrayList()); } /**