maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From schu...@apache.org
Subject [5/9] maven git commit: [MNG-4463] Dependency management import should support version ranges.
Date Fri, 10 Feb 2017 21:51:52 GMT
[MNG-4463] Dependency management import should support version ranges.

o Extended the 'ModelResolver' interface to support resolving 'Dependency's
  in addition to 'Parent's.

Closes #64 without merging. ITs are pending to be committed.


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

Branch: refs/heads/DEPMGMT-INCLUDE
Commit: 82bd000484b337f85eb792073fe0ba52e5f31231
Parents: 0514c1b
Author: Christian Schulte <schulte@apache.org>
Authored: Sat Jan 30 19:17:34 2016 +0100
Committer: Christian Schulte <schulte@apache.org>
Committed: Fri Feb 10 22:50:55 2017 +0100

----------------------------------------------------------------------
 .../maven/project/ProjectModelResolver.java     | 83 +++++++++++++++----
 .../model/building/DefaultModelBuilder.java     | 20 ++++-
 .../maven/model/resolution/ModelResolver.java   | 32 ++++++++
 .../internal/DefaultModelResolver.java          | 85 ++++++++++++++++----
 4 files changed, 186 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/82bd0004/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java b/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java
index 7b93217..e5aa052 100644
--- a/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java
+++ b/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java
@@ -26,6 +26,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.maven.model.Dependency;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 
@@ -203,24 +204,26 @@ public class ProjectModelResolver
         return new FileModelSource( pomFile );
     }
 
-    public ModelSource resolveModel( Parent parent )
+    @Override
+    public ModelSource resolveModel( final Parent parent )
         throws UnresolvableModelException
     {
-        Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(),
"", "pom",
-                                                 parent.getVersion() );
-
-        VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories,
context );
-        versionRangeRequest.setTrace( trace );
-
         try
         {
-            VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session,
versionRangeRequest );
+            final Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(),
"", "pom",
+                                                           parent.getVersion() );
+
+            final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact,
repositories, context );
+            versionRangeRequest.setTrace( trace );
+
+            final VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session,
versionRangeRequest );
 
             if ( versionRangeResult.getHighestVersion() == null )
             {
-                throw new UnresolvableModelException( "No versions matched the requested
range '" + parent.getVersion()
-                                                          + "'", parent.getGroupId(), parent.getArtifactId(),
-                                                      parent.getVersion() );
+                throw new UnresolvableModelException(
+                    String.format( "No versions matched the requested parent version range
'%s'",
+                                   parent.getVersion() ),
+                    parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
 
             }
 
@@ -229,21 +232,69 @@ public class ProjectModelResolver
                      && versionRangeResult.getVersionConstraint().getRange().getUpperBound()
== null )
             {
                 // Message below is checked for in the MNG-2199 core IT.
-                throw new UnresolvableModelException( "The requested version range '" + parent.getVersion()
-                                                          + "' does not specify an upper
bound", parent.getGroupId(),
-                                                      parent.getArtifactId(), parent.getVersion()
);
+                throw new UnresolvableModelException(
+                    String.format( "The requested parent version range '%s' does not specify
an upper bound",
+                                   parent.getVersion() ),
+                    parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
 
             }
 
             parent.setVersion( versionRangeResult.getHighestVersion().toString() );
+
+            return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion()
);
         }
-        catch ( VersionRangeResolutionException e )
+        catch ( final VersionRangeResolutionException e )
         {
             throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
                                                   parent.getVersion(), e );
 
         }
+    }
+
+    @Override
+    public ModelSource resolveModel( final Dependency dependency )
+        throws UnresolvableModelException
+    {
+        try
+        {
+            final Artifact artifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(),
"",
+                                                           "pom", dependency.getVersion()
);
+
+            final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact,
repositories, context );
+            versionRangeRequest.setTrace( trace );
+
+            final VersionRangeResult versionRangeResult = resolver.resolveVersionRange( session,
versionRangeRequest );
+
+            if ( versionRangeResult.getHighestVersion() == null )
+            {
+                throw new UnresolvableModelException(
+                    String.format( "No versions matched the requested dependency version
range '%s'",
+                                   dependency.getVersion() ),
+                    dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()
);
+
+            }
+
+            if ( versionRangeResult.getVersionConstraint() != null
+                     && versionRangeResult.getVersionConstraint().getRange() != null
+                     && versionRangeResult.getVersionConstraint().getRange().getUpperBound()
== null )
+            {
+                // Message below is checked for in the MNG-4463 core IT.
+                throw new UnresolvableModelException(
+                    String.format( "The requested dependency version range '%s' does not
specify an upper bound",
+                                   dependency.getVersion() ),
+                    dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()
);
+
+            }
+
+            dependency.setVersion( versionRangeResult.getHighestVersion().toString() );
+
+            return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()
);
+        }
+        catch ( VersionRangeResolutionException e )
+        {
+            throw new UnresolvableModelException( e.getMessage(), dependency.getGroupId(),
dependency.getArtifactId(),
+                                                  dependency.getVersion(), e );
 
-        return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion()
);
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/82bd0004/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 ac54737..e07f8e9 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
@@ -1213,7 +1213,25 @@ public class DefaultModelBuilder
                     final ModelSource importSource;
                     try
                     {
-                        importSource = modelResolver.resolveModel( groupId, artifactId, version
);
+                        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 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 )
                     {

http://git-wip-us.apache.org/repos/asf/maven/blob/82bd0004/maven-model-builder/src/main/java/org/apache/maven/model/resolution/ModelResolver.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/resolution/ModelResolver.java
b/maven-model-builder/src/main/java/org/apache/maven/model/resolution/ModelResolver.java
index c81a536..cb2a3ed 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/resolution/ModelResolver.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/resolution/ModelResolver.java
@@ -19,6 +19,7 @@ package org.apache.maven.model.resolution;
  * under the License.
  */
 
+import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Parent;
 import org.apache.maven.model.Repository;
 import org.apache.maven.model.building.ModelSource;
@@ -47,16 +48,47 @@ public interface ModelResolver
 
     /**
      * Tries to resolve the POM for the specified parent coordinates possibly updating {@code
parent}.
+     * <p>
+     * Unlike the {@link #resolveModel(java.lang.String, java.lang.String, java.lang.String)}
method, this method
+     * supports version ranges and updates the given {@code parent} instance to match the
returned {@code ModelSource}.
+     * If {@code parent} declares a version range, the version corresponding to the returned
{@code ModelSource} will
+     * be set on the given {@code parent}.
+     * </p>
      *
      * @param parent The parent coordinates to resolve, must not be {@code null}.
+     *
      * @return The source of the requested POM, never {@code null}.
+     *
      * @throws UnresolvableModelException If the POM could not be resolved from any configured
repository.
      * @since 3.2.2
+     *
+     * @see Parent#clone()
      */
     ModelSource resolveModel( Parent parent )
         throws UnresolvableModelException;
 
     /**
+     * Tries to resolve the POM for the specified dependency coordinates possibly updating
{@code dependency}.
+     * <p>
+     * Unlike the {@link #resolveModel(java.lang.String, java.lang.String, java.lang.String)}
method, this method
+     * supports version ranges and updates the given {@code dependency} instance to match
the returned
+     * {@code ModelSource}. If {@code dependency} declares a version range, the version corresponding
to the returned
+     * {@code ModelSource} will be set on the given {@code dependency}.
+     * </p>
+     *
+     * @param dependency The dependency coordinates to resolve, must not be {@code null}.
+     *
+     * @return The source of the requested POM, never {@code null}.
+     *
+     * @throws UnresolvableModelException If the POM could not be resolved from any configured
repository.
+     * @since 3.4
+     *
+     * @see Dependency#clone()
+     */
+    ModelSource resolveModel( Dependency dependency )
+        throws UnresolvableModelException;
+
+    /**
      * Adds a repository to use for subsequent resolution requests. The order in which repositories
are added matters,
      * repositories that were added first should also be searched first. When multiple repositories
with the same
      * identifier are added, only the first repository being added will be used.

http://git-wip-us.apache.org/repos/asf/maven/blob/82bd0004/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java
----------------------------------------------------------------------
diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java
b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java
index 9ea91ff..260a770 100644
--- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java
+++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java
@@ -26,6 +26,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.maven.model.Dependency;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
 import org.apache.maven.model.Parent;
@@ -182,25 +183,27 @@ class DefaultModelResolver
         return new FileModelSource( pomFile );
     }
 
-    public ModelSource resolveModel( Parent parent )
+    @Override
+    public ModelSource resolveModel( final Parent parent )
         throws UnresolvableModelException
     {
-        Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(),
"", "pom",
-                                                 parent.getVersion() );
-
-        VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories,
context );
-        versionRangeRequest.setTrace( trace );
-
         try
         {
-            VersionRangeResult versionRangeResult =
+            final Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(),
"", "pom",
+                                                           parent.getVersion() );
+
+            final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact,
repositories, context );
+            versionRangeRequest.setTrace( trace );
+
+            final VersionRangeResult versionRangeResult =
                 versionRangeResolver.resolveVersionRange( session, versionRangeRequest );
 
             if ( versionRangeResult.getHighestVersion() == null )
             {
-                throw new UnresolvableModelException( "No versions matched the requested
range '" + parent.getVersion()
-                                                          + "'", parent.getGroupId(), parent.getArtifactId(),
-                                                      parent.getVersion() );
+                throw new UnresolvableModelException(
+                    String.format( "No versions matched the requested parent version range
'%s'",
+                                   parent.getVersion() ),
+                    parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
 
             }
 
@@ -209,22 +212,70 @@ class DefaultModelResolver
                      && versionRangeResult.getVersionConstraint().getRange().getUpperBound()
== null )
             {
                 // Message below is checked for in the MNG-2199 core IT.
-                throw new UnresolvableModelException( "The requested version range '" + parent.getVersion()
-                                                          + "' does not specify an upper
bound", parent.getGroupId(),
-                                                      parent.getArtifactId(), parent.getVersion()
);
+                throw new UnresolvableModelException(
+                    String.format( "The requested parent version range '%s' does not specify
an upper bound",
+                                   parent.getVersion() ),
+                    parent.getGroupId(), parent.getArtifactId(), parent.getVersion() );
 
             }
 
             parent.setVersion( versionRangeResult.getHighestVersion().toString() );
+
+            return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion()
);
         }
-        catch ( VersionRangeResolutionException e )
+        catch ( final VersionRangeResolutionException e )
         {
             throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(),
                                                   parent.getVersion(), e );
 
         }
-
-        return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion()
);
     }
 
+    @Override
+    public ModelSource resolveModel( final Dependency dependency )
+        throws UnresolvableModelException
+    {
+        try
+        {
+            final Artifact artifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(),
"",
+                                                           "pom", dependency.getVersion()
);
+
+            final VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact,
repositories, context );
+            versionRangeRequest.setTrace( trace );
+
+            final VersionRangeResult versionRangeResult =
+                versionRangeResolver.resolveVersionRange( session, versionRangeRequest );
+
+            if ( versionRangeResult.getHighestVersion() == null )
+            {
+                throw new UnresolvableModelException(
+                    String.format( "No versions matched the requested dependency version
range '%s'",
+                                   dependency.getVersion() ),
+                    dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()
);
+
+            }
+
+            if ( versionRangeResult.getVersionConstraint() != null
+                     && versionRangeResult.getVersionConstraint().getRange() != null
+                     && versionRangeResult.getVersionConstraint().getRange().getUpperBound()
== null )
+            {
+                // Message below is checked for in the MNG-4463 core IT.
+                throw new UnresolvableModelException(
+                    String.format( "The requested dependency version range '%s' does not
specify an upper bound",
+                                   dependency.getVersion() ),
+                    dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()
);
+
+            }
+
+            dependency.setVersion( versionRangeResult.getHighestVersion().toString() );
+
+            return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()
);
+        }
+        catch ( VersionRangeResolutionException e )
+        {
+            throw new UnresolvableModelException( e.getMessage(), dependency.getGroupId(),
dependency.getArtifactId(),
+                                                  dependency.getVersion(), e );
+
+        }
+    }
 }


Mime
View raw message