From 7f526ca65dd5ea9d9d9cb5ce6ae94ce2e3d98fef Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Sat, 20 Jun 2009 15:00:45 +0000 Subject: [PATCH] o Extended model builder to report the ids of the models from the lineage and used these ids as keys to pull infos from the building result git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@786838 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven/project/DefaultProjectBuilder.java | 5 +- .../maven/model/DefaultModelBuilder.java | 100 +++++---- .../model/DefaultModelBuildingResult.java | 86 +++---- .../maven/model/ModelBuildingResult.java | 37 ++- .../org/apache/maven/model/ModelData.java | 211 ++++++++++++++++++ 5 files changed, 346 insertions(+), 93 deletions(-) create mode 100644 maven-model-builder/src/main/java/org/apache/maven/model/ModelData.java 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 ec4d3d2f6c..fd642dc7b7 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 @@ -120,7 +120,8 @@ public class DefaultProjectBuilder Model model = result.getEffectiveModel(); - MavenProject project = fromModelToMavenProject( model, result.getRawModels().get( 1 ).getPomFile(), configuration, model.getPomFile() ); + File parentPomFile = result.getRawModel( result.getModelIds().get( 1 ) ).getPomFile(); + MavenProject project = fromModelToMavenProject( model, parentPomFile, configuration, model.getPomFile() ); project.setOriginalModel( result.getRawModel() ); @@ -145,7 +146,7 @@ public class DefaultProjectBuilder project.setFile( pomFile ); List activeProfiles = new ArrayList(); - activeProfiles.addAll( result.getActivePomProfiles( result.getRawModel() ) ); + activeProfiles.addAll( result.getActivePomProfiles( result.getModelIds().get( 0 ) ) ); activeProfiles.addAll( result.getActiveExternalProfiles() ); project.setActiveProfiles( activeProfiles ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java index 30491ff09c..f569894460 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuilder.java @@ -117,55 +117,55 @@ public class DefaultModelBuilder List activeExternalProfiles = getActiveExternalProfiles( request, profileActivationContext, problems ); - result.setActiveExternalProfiles( activeExternalProfiles ); + Model inputModel = readModel( modelSource, pomFile, request, problems ); - Model model = readModel( modelSource, pomFile, request, problems ); + ModelData resultData = new ModelData( inputModel ); - List rawModels = new ArrayList(); - List resultModels = new ArrayList(); + List lineage = new ArrayList(); - for ( Model current = model; current != null; current = readParent( current, request, problems ) ) + for ( ModelData currentData = resultData; currentData != null; ) { - Model resultModel = current; - resultModels.add( resultModel ); + lineage.add( currentData ); - Model rawModel = ModelUtils.cloneModel( current ); - rawModels.add( rawModel ); + Model tmpModel = currentData.getModel(); - modelNormalizer.mergeDuplicates( resultModel, request ); + Model rawModel = ModelUtils.cloneModel( tmpModel ); + currentData.setRawModel( rawModel ); + + modelNormalizer.mergeDuplicates( tmpModel, request ); List activePomProfiles = getActivePomProfiles( rawModel, profileActivationContext, problems ); - - result.setActivePomProfiles( rawModel, activePomProfiles ); + currentData.setActiveProfiles( activePomProfiles ); for ( Profile activeProfile : activePomProfiles ) { - profileInjector.injectProfile( resultModel, activeProfile, request ); + profileInjector.injectProfile( tmpModel, activeProfile, request ); } - if ( current == model ) + if ( currentData == resultData ) { for ( Profile activeProfile : activeExternalProfiles ) { - profileInjector.injectProfile( resultModel, activeProfile, request ); + profileInjector.injectProfile( tmpModel, activeProfile, request ); } } - configureResolver( request.getModelResolver(), resultModel, problems ); + configureResolver( request.getModelResolver(), tmpModel, problems ); + + currentData = readParent( tmpModel, request, problems ); } - Model superModel = getSuperModel(); - rawModels.add( superModel ); - resultModels.add( superModel ); + ModelData superData = new ModelData( getSuperModel() ); + superData.setRawModel( superData.getModel() ); + superData.setActiveProfiles( Collections. emptyList() ); + lineage.add( superData ); - result.setRawModels( rawModels ); + assembleInheritance( lineage, request ); - assembleInheritance( resultModels, request ); - - Model resultModel = resultModels.get( 0 ); + Model resultModel = resultData.getModel(); resultModel = interpolateModel( resultModel, request, problems ); - resultModels.set( 0, resultModel ); + resultData.setModel( resultModel ); modelPathTranslator.alignToBaseDirectory( resultModel, resultModel.getProjectDirectory(), request ); @@ -191,8 +191,23 @@ public class DefaultModelBuilder throw new ModelBuildingException( problems ); } + resultData.setGroupId( resultModel.getGroupId() ); + resultData.setArtifactId( resultModel.getArtifactId() ); + resultData.setVersion( resultModel.getVersion() ); + result.setEffectiveModel( resultModel ); + result.setActiveExternalProfiles( activeExternalProfiles ); + + for ( ModelData currentData : lineage ) + { + String modelId = ( currentData != superData ) ? currentData.getId() : ""; + + result.addModelId( modelId ); + result.setActivePomProfiles( modelId, currentData.getActiveProfiles() ); + result.setRawModel( modelId, currentData.getRawModel() ); + } + return result; } @@ -302,12 +317,12 @@ public class DefaultModelBuilder } } - private void assembleInheritance( List models, ModelBuildingRequest request ) + private void assembleInheritance( List lineage, ModelBuildingRequest request ) { - for ( int i = models.size() - 2; i >= 0; i-- ) + for ( int i = lineage.size() - 2; i >= 0; i-- ) { - Model parent = models.get( i + 1 ); - Model child = models.get( i ); + Model parent = lineage.get( i + 1 ).getModel(); + Model child = lineage.get( i ).getModel(); inheritanceAssembler.assembleModelInheritance( child, parent, request ); } } @@ -329,31 +344,31 @@ public class DefaultModelBuilder } } - private Model readParent( Model childModel, ModelBuildingRequest request, List problems ) + private ModelData readParent( Model childModel, ModelBuildingRequest request, List problems ) throws ModelBuildingException { - Model parentModel; + ModelData parentData; Parent parent = childModel.getParent(); if ( parent != null ) { - parentModel = readParentLocally( childModel, request, problems ); + parentData = readParentLocally( childModel, request, problems ); - if ( parentModel == null ) + if ( parentData == null ) { - parentModel = readParentExternally( childModel, request, problems ); + parentData = readParentExternally( childModel, request, problems ); } } else { - parentModel = null; + parentData = null; } - return parentModel; + return parentData; } - private Model readParentLocally( Model childModel, ModelBuildingRequest request, List problems ) + private ModelData readParentLocally( Model childModel, ModelBuildingRequest request, List problems ) throws ModelBuildingException { File projectDirectory = childModel.getProjectDirectory(); @@ -401,10 +416,12 @@ public class DefaultModelBuilder return null; } - return candidateModel; + ModelData parentData = new ModelData( candidateModel, groupId, artifactId, version ); + + return parentData; } - private Model readParentExternally( Model childModel, ModelBuildingRequest request, List problems ) + private ModelData readParentExternally( Model childModel, ModelBuildingRequest request, List problems ) throws ModelBuildingException { Parent parent = childModel.getParent(); @@ -430,7 +447,12 @@ public class DefaultModelBuilder throw new ModelBuildingException( problems ); } - return readModel( modelSource, null, request, problems ); + Model parentModel = readModel( modelSource, null, request, problems ); + + ModelData parentData = + new ModelData( parentModel, parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); + + return parentData; } private Model getSuperModel() diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java index 7ff195d521..8dedf46296 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/DefaultModelBuildingResult.java @@ -30,101 +30,104 @@ import java.util.Map; * * @author Benjamin Bentmann */ -public class DefaultModelBuildingResult +class DefaultModelBuildingResult implements ModelBuildingResult { - private Model model; + private Model effectiveModel; - private List rawModels; + private List modelIds; - private Map> activePomProfiles; + private Map rawModels; + + private Map> activePomProfiles; private List activeExternalProfiles; public DefaultModelBuildingResult() { - rawModels = new ArrayList(); - activePomProfiles = new HashMap>(); + modelIds = new ArrayList(); + rawModels = new HashMap(); + activePomProfiles = new HashMap>(); activeExternalProfiles = new ArrayList(); } public Model getEffectiveModel() { - return model; + return effectiveModel; } public DefaultModelBuildingResult setEffectiveModel( Model model ) { - this.model = model; + this.effectiveModel = model; + + return this; + } + + public List getModelIds() + { + return Collections.unmodifiableList( modelIds ); + } + + public DefaultModelBuildingResult addModelId( String modelId ) + { + if ( modelId == null ) + { + throw new IllegalArgumentException( "no model identifier specified" ); + } + + modelIds.add( modelId ); return this; } public Model getRawModel() { - return rawModels.get( 0 ); + return rawModels.get( modelIds.get( 0 ) ); } - public List getRawModels() + public Model getRawModel( String modelId ) { - return Collections.unmodifiableList( rawModels ); + return rawModels.get( modelId ); } - public DefaultModelBuildingResult setRawModels( List rawModels ) + public DefaultModelBuildingResult setRawModel( String modelId, Model rawModel ) { - this.rawModels.clear(); - if ( rawModels != null ) + if ( modelId == null ) { - this.rawModels.addAll( rawModels ); + throw new IllegalArgumentException( "no model identifier specified" ); } + rawModels.put( modelId, rawModel ); + return this; } - public List getActivePomProfiles( Model rawModel ) + public List getActivePomProfiles( String modelId ) { - List profiles = this.activePomProfiles.get( rawModel ); - return ( profiles == null ) ? Collections. emptyList() : Collections.unmodifiableList( profiles ); + List profiles = this.activePomProfiles.get( modelId ); + return ( profiles != null ) ? Collections.unmodifiableList( profiles ) : null; } - public DefaultModelBuildingResult setActivePomProfiles( Model rawModel, List activeProfiles ) + public DefaultModelBuildingResult setActivePomProfiles( String modelId, List activeProfiles ) { - if ( rawModel == null ) + if ( modelId == null ) { - throw new IllegalArgumentException( "no model specified" ); + throw new IllegalArgumentException( "no model identifier specified" ); } if ( activeProfiles != null ) { - this.activePomProfiles.put( rawModel, new ArrayList( activeProfiles ) ); + this.activePomProfiles.put( modelId, new ArrayList( activeProfiles ) ); } else { - this.activePomProfiles.remove( rawModel ); + this.activePomProfiles.remove( modelId ); } return this; } - public DefaultModelBuildingResult addActivePomProfiles( Model rawModel, List activeProfiles ) - { - if ( rawModel == null ) - { - throw new IllegalArgumentException( "no model specified" ); - } - - List currentProfiles = this.activePomProfiles.get( rawModel ); - if ( currentProfiles == null ) - { - currentProfiles = new ArrayList( activeProfiles.size() ); - this.activePomProfiles.put( rawModel, currentProfiles ); - } - currentProfiles.addAll( activeProfiles ); - - return this; - } - public List getActiveExternalProfiles() { return Collections.unmodifiableList( activeExternalProfiles ); @@ -133,6 +136,7 @@ public class DefaultModelBuildingResult public DefaultModelBuildingResult setActiveExternalProfiles( List activeProfiles ) { this.activeExternalProfiles.clear(); + if ( activeProfiles != null ) { this.activeExternalProfiles.addAll( activeProfiles ); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java index 39ee46f15b..5d47b5ff6e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/ModelBuildingResult.java @@ -29,6 +29,16 @@ import java.util.List; public interface ModelBuildingResult { + /** + * Gets the sequence of model identifiers that denote the lineage of models from which the effective model was + * constructed. Model identifiers have the form {@code ::}. The first identifier from + * the list denotes the model on which the model builder was originally invoked. The last identifier will always be + * an empty string that by definition denotes the super POM. + * + * @return The model identifiers from the lineage of models, never {@code null}. + */ + List getModelIds(); + /** * Gets the fully assembled model. * @@ -37,29 +47,34 @@ public interface ModelBuildingResult Model getEffectiveModel(); /** - * Gets the raw model as it was read from the model source. Apart from basic validation, the raw model has not - * undergone any updates by the model builder, e.g. reflects neither inheritance or interpolation. + * Gets the raw model as it was read from the input model source. Apart from basic validation, the raw model has not + * undergone any updates by the model builder, e.g. reflects neither inheritance nor interpolation. * * @return The raw model, never {@code null}. */ Model getRawModel(); /** - * Gets the lineage of raw models from which the effective model was constructed. The first model is the model on - * which the model builder was originally invoked, the last model is the super POM. + * Gets the specified raw model as it was read from a model source. Apart from basic validation, a raw model has not + * undergone any updates by the model builder, e.g. reflects neither inheritance nor interpolation. The model + * identifier should be from the collection obtained by {@link #getModelIds()}. As a special case, an empty string + * can be used as the identifier for the super POM. * - * @return The lineage of raw models, never {@code null}. + * @param modelId The identifier of the desired raw model, must not be {@code null}. + * @return The raw model or {@code null} if the specified model id does not refer to a known model. */ - List getRawModels(); + Model getRawModel( String modelId ); /** - * Gets the profiles from the specified (raw) model that were active during model building. The input parameter - * should be a model from the collection obtained by {@link #getRawModels()}. + * Gets the profiles from the specified model that were active during model building. The model identifier should be + * from the collection obtained by {@link #getModelIds()}. As a special case, an empty string can be used as the + * identifier for the super POM. * - * @param rawModel The (raw) model for whose active profiles should be retrieved, must not be {@code null}. - * @return The active profiles of the model or an empty list if none, never {@code null}. + * @param modelId The identifier of the model whose active profiles should be retrieved, must not be {@code null}. + * @return The active profiles of the model or an empty list if none or {@code null} if the specified model id does + * not refer to a known model. */ - List getActivePomProfiles( Model rawModel ); + List getActivePomProfiles( String modelId ); /** * Gets the external profiles that were active during model building. External profiles are those that were diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/ModelData.java b/maven-model-builder/src/main/java/org/apache/maven/model/ModelData.java new file mode 100644 index 0000000000..392595972f --- /dev/null +++ b/maven-model-builder/src/main/java/org/apache/maven/model/ModelData.java @@ -0,0 +1,211 @@ +package org.apache.maven.model; + +/* + * 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.util.List; + +/** + * Holds a model along with some auxiliary information. This internal utility class assists the model builder during POM + * processing by providing a means to transport information that cannot be (easily) extracted from the model itself. + * + * @author Benjamin Bentmann + */ +class ModelData +{ + + private Model model; + + private Model rawModel; + + private List activeProfiles; + + private String groupId; + + private String artifactId; + + private String version; + + /** + * Creates a new container for the specified model. + * + * @param model The model to wrap, may be {@code null}. + */ + public ModelData( Model model ) + { + this.model = model; + } + + /** + * Creates a new container for the specified model. + * + * @param model The model to wrap, may be {@code null}. + * @param groupId The effective group identifier of the model, may be {@code null}. + * @param artifactId The effective artifact identifier of the model, may be {@code null}. + * @param version The effective version of the model, may be {@code null}. + */ + public ModelData( Model model, String groupId, String artifactId, String version ) + { + this.model = model; + setGroupId( groupId ); + setArtifactId( artifactId ); + setVersion( version ); + } + + /** + * Gets the model being wrapped. + * + * @return The model or {@code null} if not set. + */ + public Model getModel() + { + return model; + } + + /** + * Sets the model being wrapped. + * + * @param model The model, may be {@code null}. + */ + public void setModel( Model model ) + { + this.model = model; + } + + /** + * Gets the raw model being wrapped. + * + * @return The raw model or {@code null} if not set. + */ + public Model getRawModel() + { + return rawModel; + } + + /** + * Sets the raw model being wrapped. + * + * @param rawModel The raw model, may be {@code null}. + */ + public void setRawModel( Model rawModel ) + { + this.rawModel = rawModel; + } + + /** + * Gets the active profiles from the model. + * + * @return The active profiles or {@code null} if not set. + */ + public List getActiveProfiles() + { + return activeProfiles; + } + + /** + * Sets the active profiles from the model. + * + * @param activeProfiles The active profiles, may be {@code null}. + */ + public void setActiveProfiles( List activeProfiles ) + { + this.activeProfiles = activeProfiles; + } + + /** + * Gets the effective group identifier of the model. + * + * @return The effective group identifier of the model or an empty string if unknown, never {@code null}. + */ + public String getGroupId() + { + return ( groupId != null ) ? groupId : ""; + } + + /** + * Sets the effective group identifier of the model. + * + * @param groupId The effective group identifier of the model, may be {@code null}. + */ + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + /** + * Gets the effective artifact identifier of the model. + * + * @return The effective artifact identifier of the model or an empty string if unknown, never {@code null}. + */ + public String getArtifactId() + { + return ( artifactId != null ) ? artifactId : ""; + } + + /** + * Sets the effective artifact identifier of the model. + * + * @param artifactId The effective artifact identifier of the model, may be {@code null}. + */ + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + /** + * Gets the effective version of the model. + * + * @return The effective version of the model or an empty string if unknown, never {@code null}. + */ + public String getVersion() + { + return ( version != null ) ? version : ""; + } + + /** + * Sets the effective version of the model. + * + * @param version The effective version of the model, may be {@code null}. + */ + public void setVersion( String version ) + { + this.version = version; + } + + /** + * Gets the effective identifier of the model in the form {@code ::}. + * + * @return The effective identifier of the model, never {@code null}. + */ + public String getId() + { + StringBuilder buffer = new StringBuilder( 64 ); + + buffer.append( getGroupId() ).append( ':' ).append( getArtifactId() ).append( ':' ).append( getVersion() ); + + return buffer.toString(); + } + + @Override + public String toString() + { + return model.toString(); + } + +}