You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sc...@apache.org on 2016/06/21 19:36:20 UTC

maven git commit: [MNG-5527] Relocation does not work for imported poms

Repository: maven
Updated Branches:
  refs/heads/master c6fb60b54 -> d3761d465


[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/d3761d46
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/d3761d46
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/d3761d46

Branch: refs/heads/master
Commit: d3761d46551e2eb8e29d476b4ae52fcf4ab3d4bc
Parents: c6fb60b
Author: Christian Schulte <sc...@apache.org>
Authored: Tue Jun 21 21:35:40 2016 +0200
Committer: Christian Schulte <sc...@apache.org>
Committed: Tue Jun 21 21:35:40 2016 +0200

----------------------------------------------------------------------
 .../model/building/DefaultModelBuilder.java     | 173 +++++++++++--------
 1 file changed, 105 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/d3761d46/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 5970dc4..87b6a96 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
@@ -1275,8 +1275,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(); )
@@ -1370,78 +1368,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();
-
-                            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=" + scope + " 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 )
-                        {
-                            StringBuilder buffer = new StringBuilder( 256 );
-                            buffer.append( "Non-resolvable " + scope + " 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;
-                        }
+                        // no workspace resolver or workspace resolver returned null (i.e. model not in workspace)
+                        importModel = this.buildImportModelFromRepository( request, dependency, importIds, problems );
 
-                        if ( importRequest == null )
+                        if ( importModel == 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() != null
@@ -1500,6 +1435,108 @@ public class DefaultModelBuilder
         }
     }
 
+    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 ( UnresolvableModelException e )
+        {
+            StringBuilder buffer = new StringBuilder( 256 );
+            buffer.append( "Non-resolvable " + dependency.getScope() + " 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 ( ModelBuildingException e )
+        {
+            problems.addAll( e.getProblems() );
+        }
+
+        return null;
+    }
+
     private <T> void putCache( ModelCache modelCache, String groupId, String artifactId, String version,
                                ModelCacheTag<T> tag, T data )
     {