[MNG-5527] Relocation does not work for imported poms

Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/04436dff
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/04436dff
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/04436dff

Branch: refs/heads/DEPMGMT-INCLUDE
Commit: 04436dffa5c0b1f2ae0ef2a664e375e1ae7414a9
Parents: 63f6fcf
Author: Christian Schulte <schu...@apache.org>
Authored: Tue Jun 21 21:35:40 2016 +0200
Committer: Christian Schulte <schu...@apache.org>
Committed: Tue Jan 31 23:06:48 2017 +0100

----------------------------------------------------------------------
 .../model/building/DefaultModelBuilder.java     | 190 ++++++++++++-------
 1 file changed, 125 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/04436dff/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
----------------------------------------------------------------------
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 3532660..5ca1623 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
@@ -1114,8 +1114,6 @@ public class DefaultModelBuilder
         final WorkspaceModelResolver workspaceResolver = 
request.getWorkspaceModelResolver();
         final ModelResolver modelResolver = request.getModelResolver();
 
-        ModelBuildingRequest importRequest = null;
-
         List<DependencyManagement> importMngts = null;
 
         for ( Iterator<Dependency> it = depMngt.getDependencies().iterator(); 
it.hasNext(); )
@@ -1202,75 +1200,15 @@ public class DefaultModelBuilder
                     }
                 }
 
-                // no workspace resolver or workspace resolver returned null 
(i.e. model not in workspace)
                 if ( importModel == null )
                 {
-                    final ModelSource importSource;
-                    try
-                    {
-                        dependency = dependency.clone();
-                        importSource = modelResolver.resolveModel( dependency 
);
-                        final String resolvedId =
-                            dependency.getGroupId() + ':' + 
dependency.getArtifactId() + ':' + dependency.getVersion();
+                    // no workspace resolver or workspace resolver returned 
null (i.e. model not in workspace)
+                    importModel = this.buildImportModelFromRepository( 
request, dependency, importIds, problems );
 
-                        if ( !imported.equals( resolvedId ) && 
importIds.contains( resolvedId ) )
-                        {
-                            // A version range has been resolved to a cycle.
-                            String message = "The dependencies of type=pom and 
with scope=import form a cycle: ";
-                            for ( String modelId : importIds )
-                            {
-                                message += modelId + " -> ";
-                            }
-                            message += resolvedId;
-                            problems.add( new ModelProblemCollectorRequest( 
Severity.ERROR, Version.BASE ).
-                                setMessage( message ) );
-
-                            continue;
-                        }
-                    }
-                    catch ( UnresolvableModelException e )
+                    if ( importModel == null )
                     {
-                        StringBuilder buffer = new StringBuilder( 256 );
-                        buffer.append( "Non-resolvable import POM" );
-                        if ( !containsCoordinates( e.getMessage(), groupId, 
artifactId, version ) )
-                        {
-                            buffer.append( " " ).append( 
ModelProblemUtils.toId( groupId, artifactId, version ) );
-                        }
-                        buffer.append( ": " ).append( e.getMessage() );
-
-                        problems.add( new ModelProblemCollectorRequest( 
Severity.ERROR, Version.BASE )
-                            .setMessage( buffer.toString() ).setLocation( 
dependency.getLocation( "" ) )
-                            .setException( e ) );
                         continue;
                     }
-
-                    if ( importRequest == null )
-                    {
-                        importRequest = new DefaultModelBuildingRequest();
-                        importRequest.setValidationLevel( 
ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
-                        importRequest.setModelCache( request.getModelCache() );
-                        importRequest.setSystemProperties( 
request.getSystemProperties() );
-                        importRequest.setUserProperties( 
request.getUserProperties() );
-                        importRequest.setLocationTracking( 
request.isLocationTracking() );
-                    }
-
-                    importRequest.setModelSource( importSource );
-                    importRequest.setModelResolver( modelResolver.newCopy() );
-
-                    final ModelBuildingResult importResult;
-                    try
-                    {
-                        importResult = build( importRequest );
-                    }
-                    catch ( ModelBuildingException e )
-                    {
-                        problems.addAll( e.getProblems() );
-                        continue;
-                    }
-
-                    problems.addAll( importResult.getProblems() );
-
-                    importModel = importResult.getEffectiveModel();
                 }
 
                 importMngt = importModel.getDependencyManagement();
@@ -1296,6 +1234,128 @@ public class DefaultModelBuilder
         dependencyManagementImporter.importManagement( model, importMngts, 
request, problems );
     }
 
+    private Model buildImportModelFromRepository( final ModelBuildingRequest 
targetModelBuildingRequest,
+                                                  final Dependency dependency, 
final Collection<String> importIds,
+                                                  final 
DefaultModelProblemCollector problems )
+    {
+        try
+        {
+            final String imported =
+                String.format( "%s:%s:%s", dependency.getGroupId(), 
dependency.getArtifactId(),
+                               dependency.getVersion() );
+
+            final Dependency resolvedDependency = dependency.clone();
+            final ModelSource importSource =
+                targetModelBuildingRequest.getModelResolver().resolveModel( 
resolvedDependency );
+
+            final String resolvedId =
+                String.format( "%s:%s:%s", resolvedDependency.getGroupId(), 
resolvedDependency.getArtifactId(),
+                               resolvedDependency.getVersion() );
+
+            if ( !imported.equals( resolvedId ) && importIds.contains( 
resolvedId ) )
+            {
+                // A version range has been resolved to a cycle.
+                String message = "The dependencies of type=pom and scope=" + 
dependency.getScope() + " form a cycle: ";
+                for ( String modelId : importIds )
+                {
+                    message += modelId + " -> ";
+                }
+                message += resolvedId;
+                problems.add( new ModelProblemCollectorRequest( 
Severity.ERROR, Version.BASE ).setMessage( message ) );
+            }
+            else
+            {
+                final ModelBuildingRequest importRequest = new 
DefaultModelBuildingRequest();
+                importRequest.setValidationLevel( 
ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
+                importRequest.setModelCache( 
targetModelBuildingRequest.getModelCache() );
+                importRequest.setSystemProperties( 
targetModelBuildingRequest.getSystemProperties() );
+                importRequest.setUserProperties( 
targetModelBuildingRequest.getUserProperties() );
+                importRequest.setLocationTracking( 
targetModelBuildingRequest.isLocationTracking() );
+                importRequest.setModelSource( importSource );
+                importRequest.setModelResolver( 
targetModelBuildingRequest.getModelResolver().newCopy() );
+
+                final ModelBuildingResult importResult = build( importRequest 
);
+                problems.addAll( importResult.getProblems() );
+
+                Model importModel = importResult.getEffectiveModel();
+
+                if ( importModel.getDistributionManagement() != null
+                         && 
importModel.getDistributionManagement().getRelocation() != null )
+                {
+                    final Dependency relocated = dependency.clone();
+                    relocated.setGroupId( 
importModel.getDistributionManagement().getRelocation().getGroupId() );
+                    relocated.setArtifactId( 
importModel.getDistributionManagement().getRelocation().getArtifactId() );
+                    relocated.setVersion( 
importModel.getDistributionManagement().getRelocation().getVersion() );
+
+                    String message = String.format(
+                        "The dependency of type='%s' and scope='%s' has been 
relocated to '%s:%s:%s'",
+                        dependency.getType(), dependency.getScope(), 
relocated.getGroupId(),
+                        relocated.getArtifactId(), relocated.getVersion() );
+
+                    if ( 
importModel.getDistributionManagement().getRelocation().getMessage() != null )
+                    {
+                        message += ". " + 
importModel.getDistributionManagement().getRelocation().getMessage();
+                    }
+
+                    problems.add( new ModelProblemCollectorRequest( 
Severity.WARNING, Version.BASE ).
+                        setMessage( message ).
+                        setLocation( 
importModel.getDistributionManagement().getRelocation().getLocation( "" ) ) );
+
+                    importModel = this.buildImportModelFromRepository(
+                        targetModelBuildingRequest, relocated, importIds, 
problems );
+
+                }
+
+                return importModel;
+            }
+        }
+        catch ( final UnresolvableModelException e )
+        {
+            final StringBuilder buffer = new StringBuilder( 256 );
+            buffer.append( "Non-resolvable " ).append( dependency.getScope() 
).append( " POM" );
+
+            if ( !containsCoordinates( e.getMessage(), 
dependency.getGroupId(), dependency.getArtifactId(),
+                                       dependency.getVersion() ) )
+            {
+                buffer.append( ' ' ).append( ModelProblemUtils.toId(
+                    dependency.getGroupId(), dependency.getArtifactId(), 
dependency.getVersion() ) );
+
+            }
+
+            buffer.append( ": " ).append( e.getMessage() );
+
+            problems.add( new ModelProblemCollectorRequest( Severity.ERROR, 
Version.BASE ).
+                setMessage( buffer.toString() ).
+                setLocation( dependency.getLocation( "" ) ).
+                setException( e ) );
+
+        }
+        catch ( final ModelBuildingException e )
+        {
+            final StringBuilder buffer = new StringBuilder( 256 );
+            buffer.append( "Failure building " ).append( dependency.getScope() 
).append( " POM" );
+
+            if ( !containsCoordinates( e.getMessage(), 
dependency.getGroupId(), dependency.getArtifactId(),
+                                       dependency.getVersion() ) )
+            {
+                buffer.append( ' ' ).append( ModelProblemUtils.toId(
+                    dependency.getGroupId(), dependency.getArtifactId(), 
dependency.getVersion() ) );
+
+            }
+
+            buffer.append( ": " ).append( e.getMessage() );
+
+            problems.add( new ModelProblemCollectorRequest( Severity.ERROR, 
Version.BASE ).
+                setMessage( buffer.toString() ).
+                setLocation( dependency.getLocation( "" ) ).
+                setException( e ) );
+
+            problems.addAll( e.getProblems() );
+        }
+
+        return null;
+    }
+
     private <T> void putCache( ModelCache modelCache, String groupId, String 
artifactId, String version,
                                ModelCacheTag<T> tag, T data )
     {

Reply via email to