diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index 149becc0f1..d407f9a91d 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -22,8 +22,10 @@ package org.apache.maven.model.building; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -108,6 +110,12 @@ public class DefaultModelBuilder public ModelBuildingResult build( ModelBuildingRequest request ) throws ModelBuildingException + { + return build( request, new LinkedHashSet() ); + } + + private ModelBuildingResult build( ModelBuildingRequest request, Collection imports ) + throws ModelBuildingException { DefaultModelBuildingResult result = new DefaultModelBuildingResult(); @@ -212,6 +220,13 @@ public class DefaultModelBuilder public ModelBuildingResult build( ModelBuildingRequest request, ModelBuildingResult result ) throws ModelBuildingException + { + return build( request, result, new LinkedHashSet() ); + } + + private ModelBuildingResult build( ModelBuildingRequest request, ModelBuildingResult result, + Collection imports ) + throws ModelBuildingException { Model resultModel = result.getEffectiveModel(); @@ -230,7 +245,7 @@ public class DefaultModelBuilder lifecycleBindingsInjector.injectLifecycleBindings( resultModel, request, problems ); } - importDependencyManagement( resultModel, request, problems ); + importDependencyManagement( resultModel, request, problems, imports ); dependencyManagementInjector.injectManagement( resultModel, request, problems ); @@ -544,7 +559,7 @@ public class DefaultModelBuilder } private void importDependencyManagement( Model model, ModelBuildingRequest request, - DefaultModelProblemCollector problems ) + DefaultModelProblemCollector problems, Collection imports ) { DependencyManagement depMngt = model.getDependencyManagement(); @@ -553,6 +568,10 @@ public class DefaultModelBuilder return; } + String importing = model.getGroupId() + ':' + model.getArtifactId() + ':' + model.getVersion(); + + imports.add( importing ); + ModelResolver modelResolver = request.getModelResolver(); ModelBuildingRequest importRequest = null; @@ -574,6 +593,20 @@ public class DefaultModelBuilder String artifactId = dependency.getArtifactId(); String version = dependency.getVersion(); + String imported = groupId + ':' + artifactId + ':' + version; + + if ( imports.contains( imported ) ) + { + String message = "The dependencies of type=pom and with scope=import from a cycle: "; + for ( String modelId : imports ) + { + message += modelId + " -> "; + } + message += imported; + problems.addError( message ); + continue; + } + DependencyManagement importMngt = getCache( request.getModelCache(), groupId, artifactId, version, ModelCacheTag.IMPORT ); @@ -610,7 +643,7 @@ public class DefaultModelBuilder ModelBuildingResult importResult; try { - importResult = build( importRequest ); + importResult = build( importRequest, imports ); } catch ( ModelBuildingException e ) { @@ -640,6 +673,8 @@ public class DefaultModelBuilder importMngts.add( importMngt ); } + imports.remove( importing ); + dependencyManagementImporter.importManagement( model, importMngts, request, problems ); }