From 4397321a10673a6e1bef816508085ce0e819a2bc Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Thu, 6 Aug 2009 17:35:15 +0000 Subject: [PATCH] o Extended metadata resolution group to also hold managed version map. This can save us another trip through the project builder when transitively resolving say plugin dependencies. We already need to build the project to grab the direct dependencies so we can as well grab the managed ones while we're there git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@801738 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/artifact/ArtifactUtils.java | 13 ++ .../metadata/ArtifactMetadataSource.java | 5 + .../artifact/metadata/ResolutionGroup.java | 12 +- .../resolver/DefaultArtifactResolver.java | 19 +- .../metadata/ArtifactMetadataSource.java | 4 + .../DefaultMetadataResolutionRequest.java | 91 +++++++++ .../metadata/MetadataResolutionRequest.java | 96 ++++++++++ .../legacy/metadata/ResolutionGroup.java | 18 +- .../SimpleArtifactMetadataSource.java | 9 +- .../artifact/metadata/TestMetadataSource.java | 8 + .../resolver/ArtifactResolverTest.java | 7 + .../DefaultArtifactCollectorTest.java | 7 + .../metadata/TestMetadataSource.java | 8 + .../artifact/ArtifactWithDependencies.java | 4 + .../artifact/DefaultMavenMetadataCache.java | 46 ++++- .../project/artifact/MavenMetadataCache.java | 4 +- .../project/artifact/MavenMetadataSource.java | 177 ++++++++++++------ .../project/artifact/PluginArtifact.java | 8 +- .../project/artifact/ProjectArtifact.java | 10 +- .../DefaultMavenMetadataCacheTest.java | 4 +- 20 files changed, 469 insertions(+), 81 deletions(-) create mode 100644 maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java create mode 100644 maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/ArtifactUtils.java b/maven-compat/src/main/java/org/apache/maven/artifact/ArtifactUtils.java index 1eb95c71a9..d59e895470 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/ArtifactUtils.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/ArtifactUtils.java @@ -177,6 +177,19 @@ public final class ArtifactUtils return to; } + public static > T copyArtifacts( Map from, T to ) + { + if ( from != null ) + { + for ( Map.Entry entry : from.entrySet() ) + { + to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) ); + } + } + + return to; + } + private static List copyList( List original ) { List copy = null; diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ArtifactMetadataSource.java b/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ArtifactMetadataSource.java index b43799a4d8..92c07d4509 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ArtifactMetadataSource.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ArtifactMetadataSource.java @@ -24,11 +24,16 @@ import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; @Deprecated public interface ArtifactMetadataSource extends org.apache.maven.repository.legacy.metadata.ArtifactMetadataSource { + + ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException; + ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) throws ArtifactMetadataRetrievalException; diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ResolutionGroup.java b/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ResolutionGroup.java index fe5b339da3..b2226c8fe3 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ResolutionGroup.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/metadata/ResolutionGroup.java @@ -16,6 +16,7 @@ package org.apache.maven.artifact.metadata; */ import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -25,8 +26,17 @@ import org.apache.maven.artifact.repository.ArtifactRepository; public class ResolutionGroup extends org.apache.maven.repository.legacy.metadata.ResolutionGroup { - public ResolutionGroup( Artifact pomArtifact, Set artifacts, List resolutionRepositories ) + + public ResolutionGroup( Artifact pomArtifact, Set artifacts, + List resolutionRepositories ) { super( pomArtifact, artifacts, resolutionRepositories ); } + + public ResolutionGroup( Artifact pomArtifact, Set artifacts, Map managedVersions, + List resolutionRepositories ) + { + super( pomArtifact, artifacts, managedVersions, resolutionRepositories ); + } + } diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index e9bb3c1434..bf04296c01 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -29,6 +29,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.metadata.ResolutionGroup; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Snapshot; @@ -37,6 +38,8 @@ import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.resolver.filter.ArtifactFilter; import org.apache.maven.repository.legacy.WagonManager; import org.apache.maven.repository.legacy.metadata.ArtifactMetadata; +import org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.apache.maven.repository.legacy.resolver.conflict.ConflictResolver; import org.apache.maven.repository.legacy.resolver.transform.ArtifactTransformationManager; import org.apache.maven.wagon.ResourceDoesNotExistException; @@ -412,9 +415,23 @@ public class DefaultArtifactResolver if ( request.isResolveTransitively() ) { + MetadataResolutionRequest metadataRequest = new DefaultMetadataResolutionRequest(); + + metadataRequest.setArtifact( rootArtifact ); + metadataRequest.setLocalRepository( localRepository ); + metadataRequest.setRemoteRepositories( remoteRepositories ); + metadataRequest.setResolveManagedVersions( managedVersions == null ); + try { - Set directArtifacts = source.retrieve( rootArtifact, localRepository, remoteRepositories ).getArtifacts(); + ResolutionGroup resolutionGroup = source.retrieve( metadataRequest ); + + if ( managedVersions == null ) + { + managedVersions = resolutionGroup.getManagedVersions(); + } + + Set directArtifacts = resolutionGroup.getArtifacts(); if ( artifacts == null || artifacts.isEmpty() ) { diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ArtifactMetadataSource.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ArtifactMetadataSource.java index b6e573d303..3e9a28e639 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ArtifactMetadataSource.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ArtifactMetadataSource.java @@ -34,6 +34,10 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; */ public interface ArtifactMetadataSource { + + ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException; + ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) throws ArtifactMetadataRetrievalException; diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java new file mode 100644 index 0000000000..4a6dd37ba1 --- /dev/null +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java @@ -0,0 +1,91 @@ +package org.apache.maven.repository.legacy.metadata; + +/* + * 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; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; + +/** + * Forms a request to retrieve artifact metadata. + * + * @author Benjamin Bentmann + */ +public class DefaultMetadataResolutionRequest + implements MetadataResolutionRequest +{ + + private Artifact artifact; + + private ArtifactRepository localRepository; + + private List remoteRepositories; + + private boolean resolveManagedVersions; + + public Artifact getArtifact() + { + return artifact; + } + + public DefaultMetadataResolutionRequest setArtifact( Artifact artifact ) + { + this.artifact = artifact; + + return this; + } + + public ArtifactRepository getLocalRepository() + { + return localRepository; + } + + public DefaultMetadataResolutionRequest setLocalRepository( ArtifactRepository localRepository ) + { + this.localRepository = localRepository; + + return this; + } + + public List getRemoteRepositories() + { + return remoteRepositories; + } + + public DefaultMetadataResolutionRequest setRemoteRepositories( List remoteRepositories ) + { + this.remoteRepositories = remoteRepositories; + + return this; + } + + public boolean isResolveManagedVersions() + { + return resolveManagedVersions; + } + + public MetadataResolutionRequest setResolveManagedVersions( boolean resolveManagedVersions ) + { + this.resolveManagedVersions = resolveManagedVersions; + return this; + } + +} diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java new file mode 100644 index 0000000000..8eb89a5a9d --- /dev/null +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java @@ -0,0 +1,96 @@ +package org.apache.maven.repository.legacy.metadata; + +/* + * 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; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; + +/** + * Forms a request to retrieve artifact metadata. + * + * @author Benjamin Bentmann + */ +public interface MetadataResolutionRequest +{ + + /** + * Gets the artifact to resolve metadata for. + * + * @return The artifact to resolve metadata for or {@code null} if not set. + */ + Artifact getArtifact(); + + /** + * Sets the artifact for which to resolve metadata. + * + * @param artifact The artifact for which to resolve metadata. + * @return This request, never {@code null}. + */ + MetadataResolutionRequest setArtifact( Artifact artifact ); + + /** + * Gets the local repository to use for the resolution. + * + * @return The local repository to use for the resolution or {@code null} if not set. + */ + ArtifactRepository getLocalRepository(); + + /** + * Sets the local repository to use for the resolution. + * + * @param localRepository The local repository to use for the resolution. + * @return This request, never {@code null}. + */ + MetadataResolutionRequest setLocalRepository( ArtifactRepository localRepository ); + + /** + * Gets the remote repositories to use for the resolution. + * + * @return The remote repositories to use for the resolution or {@code null} if not set. + */ + List getRemoteRepositories(); + + /** + * Sets the remote repository to use for the resolution. + * + * @param remoteRepositories The remote repository to use for the resolution. + * @return This request, never {@code null}. + */ + MetadataResolutionRequest setRemoteRepositories( List remoteRepositories ); + + /** + * Determines whether the managed version information should be retrieved. + * + * @return {@code true} if the dependency management information should be retrieved, {@code false} otherwise. + */ + boolean isResolveManagedVersions(); + + /** + * Enables/disables resolution of the dependency manageemnt information. + * + * @param resolveManagedVersions {@code true} if the dependency management information should be retrieved, {@code + * false} otherwise. + * @return This request, never {@code null}. + */ + MetadataResolutionRequest setResolveManagedVersions( boolean resolveManagedVersions ); + +} diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ResolutionGroup.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ResolutionGroup.java index 17af23f87f..722e150869 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ResolutionGroup.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/ResolutionGroup.java @@ -20,6 +20,7 @@ package org.apache.maven.repository.legacy.metadata; */ import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -34,12 +35,20 @@ public class ResolutionGroup private final Artifact pomArtifact; - public ResolutionGroup( Artifact pomArtifact, - Set artifacts, + private final Map managedVersions; + + public ResolutionGroup( Artifact pomArtifact, Set artifacts, + List resolutionRepositories ) + { + this( pomArtifact, artifacts, null, resolutionRepositories ); + } + + public ResolutionGroup( Artifact pomArtifact, Set artifacts, Map managedVersions, List resolutionRepositories ) { this.pomArtifact = pomArtifact; this.artifacts = artifacts; + this.managedVersions = managedVersions; this.resolutionRepositories = resolutionRepositories; } @@ -58,4 +67,9 @@ public class ResolutionGroup return resolutionRepositories; } + public Map getManagedVersions() + { + return managedVersions; + } + } diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/deployer/SimpleArtifactMetadataSource.java b/maven-compat/src/test/java/org/apache/maven/artifact/deployer/SimpleArtifactMetadataSource.java index 56334b181c..7158b7bb97 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/deployer/SimpleArtifactMetadataSource.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/deployer/SimpleArtifactMetadataSource.java @@ -24,11 +24,11 @@ import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.filter.ArtifactFilter; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.repository.legacy.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.repository.legacy.metadata.ArtifactMetadataSource; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.apache.maven.repository.legacy.metadata.ResolutionGroup; /** @author Jason van Zyl */ @@ -64,4 +64,11 @@ public class SimpleArtifactMetadataSource { return artifact; } + + public ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException + { + return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() ); + } + } diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/metadata/TestMetadataSource.java b/maven-compat/src/test/java/org/apache/maven/artifact/metadata/TestMetadataSource.java index 3b0c645114..832da9a849 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/metadata/TestMetadataSource.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/metadata/TestMetadataSource.java @@ -9,6 +9,7 @@ import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.filter.ArtifactFilter; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; @@ -73,4 +74,11 @@ public class TestMetadataSource { return artifact; } + + public ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException + { + return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() ); + } + } diff --git a/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java b/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java index 5e4bb0460d..ad54df2fbb 100644 --- a/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java +++ b/maven-compat/src/test/java/org/apache/maven/artifact/resolver/ArtifactResolverTest.java @@ -34,6 +34,7 @@ import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.metadata.ResolutionGroup; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; // It would be cool if there was a hook that i could use to setup a test environment. // I want to setup a local/remote repositories for testing but i don't want to have @@ -223,6 +224,12 @@ public class ArtifactResolverTest { return artifact; } + + public ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException + { + return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() ); + } }; ArtifactResolutionResult result = null; diff --git a/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java b/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java index 5989f3831c..6e99af4d84 100644 --- a/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java +++ b/maven-compat/src/test/java/org/apache/maven/repository/legacy/resolver/DefaultArtifactCollectorTest.java @@ -47,6 +47,7 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.apache.maven.repository.legacy.resolver.LegacyArtifactCollector; import org.codehaus.plexus.PlexusTestCase; @@ -975,5 +976,11 @@ public class DefaultArtifactCollectorTest { return artifact; } + + public ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException + { + return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() ); + } } } diff --git a/maven-compat/src/test/java/org/apache/maven/repository/metadata/TestMetadataSource.java b/maven-compat/src/test/java/org/apache/maven/repository/metadata/TestMetadataSource.java index 28fa58ab9b..783b3d3b6d 100644 --- a/maven-compat/src/test/java/org/apache/maven/repository/metadata/TestMetadataSource.java +++ b/maven-compat/src/test/java/org/apache/maven/repository/metadata/TestMetadataSource.java @@ -11,6 +11,7 @@ import org.apache.maven.artifact.resolver.filter.ArtifactFilter; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.repository.legacy.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.repository.legacy.metadata.ArtifactMetadataSource; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.apache.maven.repository.legacy.metadata.ResolutionGroup; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; @@ -76,4 +77,11 @@ public class TestMetadataSource { return artifact; } + + public ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException + { + return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() ); + } + } diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/ArtifactWithDependencies.java b/maven-core/src/main/java/org/apache/maven/project/artifact/ArtifactWithDependencies.java index c13fe810f0..cb06b70fe9 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/ArtifactWithDependencies.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/ArtifactWithDependencies.java @@ -25,5 +25,9 @@ import org.apache.maven.model.Dependency; public interface ArtifactWithDependencies { + List getDependencies(); + + List getManagedDependencies(); + } diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java index 6a73a3fb66..7c91763703 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultMavenMetadataCache.java @@ -19,6 +19,7 @@ package org.apache.maven.project.artifact; import java.io.File; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -38,17 +39,21 @@ public class DefaultMavenMetadataCache public static class CacheKey { private final Artifact artifact; + private final boolean resolveManagedVersions; private final List repositories = new ArrayList(); private final int hashCode; - public CacheKey( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + public CacheKey( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository, + List remoteRepositories ) { this.artifact = ArtifactUtils.copyArtifact( artifact ); + this.resolveManagedVersions = resolveManagedVersions; this.repositories.add( localRepository ); this.repositories.addAll( remoteRepositories ); int hash = 17; hash = hash * 31 + artifactHashCode( artifact ); + hash = hash * 31 + ( resolveManagedVersions ? 1 : 2 ); hash = hash * 31 + repositories.hashCode(); this.hashCode = hash; } @@ -74,7 +79,8 @@ public class DefaultMavenMetadataCache CacheKey other = (CacheKey) o; - return artifactEquals( artifact, other.artifact ) && repositories.equals( other.repositories ); + return artifactEquals( artifact, other.artifact ) && resolveManagedVersions == other.resolveManagedVersions + && repositories.equals( other.repositories ); } } @@ -121,17 +127,24 @@ public class DefaultMavenMetadataCache { private Artifact pomArtifact; private List artifacts; + private Map managedVersions; private List remoteRepositories; private long length; private long timestamp; - CacheRecord(Artifact pomArtifact, Set artifacts, List remoteRepositories) + CacheRecord(Artifact pomArtifact, Set artifacts, Map managedVersions, List remoteRepositories) { this.pomArtifact = ArtifactUtils.copyArtifact( pomArtifact ); this.artifacts = ArtifactUtils.copyArtifacts( artifacts, new ArrayList() ); this.remoteRepositories = new ArrayList( remoteRepositories ); + this.managedVersions = managedVersions; + if ( managedVersions != null ) + { + this.managedVersions = + ArtifactUtils.copyArtifacts( managedVersions, new LinkedHashMap() ); + } File pomFile = pomArtifact.getFile(); if ( pomFile != null && pomFile.canRead() ) @@ -156,6 +169,11 @@ public class DefaultMavenMetadataCache return artifacts; } + public Map getManagedVersions() + { + return managedVersions; + } + public List getRemoteRepositories() { return remoteRepositories; @@ -175,18 +193,24 @@ public class DefaultMavenMetadataCache protected Map cache = new HashMap(); - public ResolutionGroup get( Artifact artifact, ArtifactRepository localRepository, + public ResolutionGroup get( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository, List remoteRepositories ) { - CacheKey cacheKey = new CacheKey( artifact, localRepository, remoteRepositories ); + CacheKey cacheKey = new CacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories ); CacheRecord cacheRecord = cache.get( cacheKey ); if ( cacheRecord != null && !cacheRecord.isStale() ) { Artifact pomArtifact = ArtifactUtils.copyArtifact( cacheRecord.getArtifact() ); - Set artifacts = ArtifactUtils.copyArtifacts( cacheRecord.getArtifacts(), new LinkedHashSet() ); - return new ResolutionGroup( pomArtifact, artifacts , cacheRecord.getRemoteRepositories() ); + Set artifacts = + ArtifactUtils.copyArtifacts( cacheRecord.getArtifacts(), new LinkedHashSet() ); + Map managedVersions = cacheRecord.getManagedVersions(); + if ( managedVersions != null ) + { + managedVersions = ArtifactUtils.copyArtifacts( managedVersions, new LinkedHashMap() ); + } + return new ResolutionGroup( pomArtifact, artifacts, managedVersions, cacheRecord.getRemoteRepositories() ); } cache.remove( cacheKey ); @@ -194,11 +218,13 @@ public class DefaultMavenMetadataCache return null; } - public void put( Artifact artifact, ArtifactRepository localRepository, + public void put( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository, List remoteRepositories, ResolutionGroup result ) { - CacheKey cacheKey = new CacheKey( artifact, localRepository, remoteRepositories ); - CacheRecord cacheRecord = new CacheRecord( result.getPomArtifact(), result.getArtifacts(), result.getResolutionRepositories() ); + CacheKey cacheKey = new CacheKey( artifact, resolveManagedVersions, localRepository, remoteRepositories ); + CacheRecord cacheRecord = + new CacheRecord( result.getPomArtifact(), result.getArtifacts(), result.getManagedVersions(), + result.getResolutionRepositories() ); cache.put( cacheKey, cacheRecord ); } diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataCache.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataCache.java index 862057cb2e..2aa7c20a5a 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataCache.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataCache.java @@ -25,10 +25,10 @@ import org.apache.maven.artifact.metadata.ResolutionGroup; public interface MavenMetadataCache { - ResolutionGroup get( Artifact artifact, ArtifactRepository localRepository, + ResolutionGroup get( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository, List remoteRepositories ); - void put( Artifact artifact, ArtifactRepository localRepository, + void put( Artifact artifact, boolean resolveManagedVersions, ArtifactRepository localRepository, List remoteRepositories, ResolutionGroup result ); void flush(); diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index 94cff47179..9a62d5d0e7 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -19,8 +19,10 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -42,6 +44,7 @@ import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.DistributionManagement; import org.apache.maven.model.Exclusion; import org.apache.maven.model.Relocation; @@ -51,6 +54,7 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuilder; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; @@ -82,8 +86,23 @@ public class MavenMetadataSource @Requirement private MavenMetadataCache cache; - - public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + + public ResolutionGroup retrieve( MetadataResolutionRequest request ) + throws ArtifactMetadataRetrievalException + { + return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories(), + request.isResolveManagedVersions() ); + } + + public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, + List remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + return retrieve( artifact, localRepository, remoteRepositories, false ); + } + + public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, + List remoteRepositories, boolean resolveManagedVersions ) throws ArtifactMetadataRetrievalException { // @@ -95,7 +114,7 @@ public class MavenMetadataSource return new ResolutionGroup( null, null, null ); } - ResolutionGroup cached = cache.get( artifact, localRepository, remoteRepositories ); + ResolutionGroup cached = cache.get( artifact, resolveManagedVersions, localRepository, remoteRepositories ); if ( cached != null ) { @@ -104,6 +123,8 @@ public class MavenMetadataSource List dependencies; + List managedDependencies = null; + Artifact pomArtifact; //TODO: Not even sure this is really required as the project will be cached in the builder, we'll see this @@ -112,7 +133,9 @@ public class MavenMetadataSource { pomArtifact = artifact; - dependencies = ((ArtifactWithDependencies)artifact).getDependencies(); + dependencies = ( (ArtifactWithDependencies) artifact ).getDependencies(); + + managedDependencies = ( (ArtifactWithDependencies) artifact ).getManagedDependencies(); } else { @@ -135,6 +158,9 @@ public class MavenMetadataSource else { dependencies = rel.project.getDependencies(); + + DependencyManagement depMngt = rel.project.getDependencyManagement(); + managedDependencies = ( depMngt != null ) ? depMngt.getDependencies() : null; } } @@ -144,74 +170,81 @@ public class MavenMetadataSource { artifacts = new LinkedHashSet(); - ArtifactFilter dependencyFilter = artifact.getDependencyFilter(); - - for ( Dependency d : dependencies ) + for ( Dependency dependency : dependencies ) { - String effectiveScope = getEffectiveScope( d.getScope(), artifact.getScope() ); + Artifact dependencyArtifact = createDependencyArtifact( dependency, artifact, pomArtifact ); - if ( effectiveScope != null ) + if ( dependencyArtifact != null ) { - Artifact dependencyArtifact; - - VersionRange versionRange; - try - { - versionRange = VersionRange.createFromVersionSpec( d.getVersion() ); - } - catch ( InvalidVersionSpecificationException e ) - { - throw new ArtifactMetadataRetrievalException( "Invalid version for dependency " - + d.getManagementKey() + ": " + e.getMessage(), e, pomArtifact ); - } - - dependencyArtifact = - repositorySystem.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, - d.getType(), d.getClassifier(), effectiveScope, - d.isOptional() ); - - if ( dependencyFilter == null || dependencyFilter.include( dependencyArtifact ) ) - { - dependencyArtifact.setOptional( d.isOptional() ); - - if ( Artifact.SCOPE_SYSTEM.equals( effectiveScope ) ) - { - dependencyArtifact.setFile( new File( d.getSystemPath() ) ); - } - - ArtifactFilter newFilter = dependencyFilter; - - if ( !d.getExclusions().isEmpty() ) - { - List exclusions = new ArrayList(); - - for ( Exclusion e : d.getExclusions() ) - { - exclusions.add( e.getGroupId() + ":" + e.getArtifactId() ); - } - - newFilter = new ExcludesArtifactFilter( exclusions ); - if ( dependencyFilter != null ) - { - newFilter = new AndArtifactFilter( Arrays.asList( dependencyFilter, newFilter ) ); - } - } - - dependencyArtifact.setDependencyFilter( newFilter ); - - artifacts.add( dependencyArtifact ); - } + artifacts.add( dependencyArtifact ); } } } - ResolutionGroup result = new ResolutionGroup( pomArtifact, artifacts, remoteRepositories ); + Map managedVersions = null; - cache.put( artifact, localRepository, remoteRepositories, result ); + if ( managedDependencies != null && resolveManagedVersions ) + { + managedVersions = new HashMap(); + + for ( Dependency managedDependency : managedDependencies ) + { + Artifact managedArtifact = createDependencyArtifact( managedDependency, null, pomArtifact ); + + managedVersions.put( managedDependency.getManagementKey(), managedArtifact ); + } + } + + ResolutionGroup result = new ResolutionGroup( pomArtifact, artifacts, managedVersions, remoteRepositories ); + + cache.put( artifact, resolveManagedVersions, localRepository, remoteRepositories, result ); return result; } + private Artifact createDependencyArtifact( Dependency dependency, Artifact owner, Artifact pom ) + throws ArtifactMetadataRetrievalException + { + String effectiveScope = getEffectiveScope( dependency.getScope(), ( owner != null ) ? owner.getScope() : null ); + + if ( effectiveScope == null ) + { + return null; + } + + VersionRange versionRange; + try + { + versionRange = VersionRange.createFromVersionSpec( dependency.getVersion() ); + } + catch ( InvalidVersionSpecificationException e ) + { + throw new ArtifactMetadataRetrievalException( "Invalid version for dependency " + + dependency.getManagementKey() + ": " + e.getMessage(), e, pom ); + } + + Artifact dependencyArtifact = + repositorySystem.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), + versionRange, dependency.getType(), dependency.getClassifier(), + effectiveScope, dependency.isOptional() ); + + ArtifactFilter dependencyFilter = ( owner != null ) ? owner.getDependencyFilter() : null; + + if ( dependencyFilter != null && !dependencyFilter.include( dependencyArtifact ) ) + { + return null; + } + + if ( Artifact.SCOPE_SYSTEM.equals( effectiveScope ) ) + { + dependencyArtifact.setFile( new File( dependency.getSystemPath() ) ); + } + + dependencyArtifact.setDependencyFilter( createDependencyFilter( dependency, dependencyFilter ) ); + + return dependencyArtifact; + } + private String getEffectiveScope( String originalScope, String inheritedScope ) { String effectiveScope = Artifact.SCOPE_RUNTIME; @@ -253,6 +286,30 @@ public class MavenMetadataSource return effectiveScope; } + private ArtifactFilter createDependencyFilter( Dependency dependency, ArtifactFilter inheritedFilter ) + { + ArtifactFilter effectiveFilter = inheritedFilter; + + if ( !dependency.getExclusions().isEmpty() ) + { + List exclusions = new ArrayList(); + + for ( Exclusion e : dependency.getExclusions() ) + { + exclusions.add( e.getGroupId() + ':' + e.getArtifactId() ); + } + + effectiveFilter = new ExcludesArtifactFilter( exclusions ); + + if ( inheritedFilter != null ) + { + effectiveFilter = new AndArtifactFilter( Arrays.asList( inheritedFilter, effectiveFilter ) ); + } + } + + return effectiveFilter; + } + public List retrieveAvailableVersions( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) throws ArtifactMetadataRetrievalException { diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/PluginArtifact.java b/maven-core/src/main/java/org/apache/maven/project/artifact/PluginArtifact.java index 07424d089d..e5a8b7d9e2 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/PluginArtifact.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/PluginArtifact.java @@ -19,6 +19,7 @@ package org.apache.maven.project.artifact; * under the License. */ +import java.util.Collections; import java.util.List; import org.apache.maven.artifact.Artifact; @@ -45,7 +46,12 @@ public class PluginArtifact { return plugin.getDependencies(); } - + + public List getManagedDependencies() + { + return Collections.emptyList(); + } + static class PluginArtifactHandler implements ArtifactHandler { diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/ProjectArtifact.java b/maven-core/src/main/java/org/apache/maven/project/artifact/ProjectArtifact.java index 92f5f8a642..d6f66b6e9c 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/ProjectArtifact.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/ProjectArtifact.java @@ -19,11 +19,13 @@ package org.apache.maven.project.artifact; * under the License. */ +import java.util.Collections; import java.util.List; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.project.MavenProject; public class ProjectArtifact @@ -49,7 +51,13 @@ public class ProjectArtifact { return project.getDependencies(); } - + + public List getManagedDependencies() + { + DependencyManagement depMngt = project.getDependencyManagement(); + return ( depMngt != null ) ? depMngt.getDependencies() : Collections. emptyList(); + } + static class PomArtifactHandler implements ArtifactHandler { diff --git a/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultMavenMetadataCacheTest.java b/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultMavenMetadataCacheTest.java index dd90657a5a..8433e43a67 100644 --- a/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultMavenMetadataCacheTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultMavenMetadataCacheTest.java @@ -71,8 +71,8 @@ public class DefaultMavenMetadataCacheTest assertNotSame( lr1, lr2 ); assertNotSame( rr1, rr2 ); - CacheKey k1 = new CacheKey( a1, lr1, Collections.singletonList( rr1 ) ); - CacheKey k2 = new CacheKey( a2, lr2, Collections.singletonList( rr2 ) ); + CacheKey k1 = new CacheKey( a1, false, lr1, Collections.singletonList( rr1 ) ); + CacheKey k2 = new CacheKey( a2, false, lr2, Collections.singletonList( rr2 ) ); assertEquals(k1.hashCode(), k2.hashCode()); }