Author: rfscholte Date: Sat Dec 26 15:48:15 2015 New Revision: 1721735 URL: http://svn.apache.org/viewvc?rev=1721735&view=rev Log: Moved dependency related methods from ArtifactResolver to DependencyResolver
Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolver.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolverException.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/DefaultDependencyResolver.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Invoker.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30ArtifactResult.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30DependencyResolver.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31ArtifactResult.java maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31DependencyResolver.java Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java Modified: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java?rev=1721735&r1=1721734&r2=1721735&view=diff ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java (original) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/artifact/resolve/ArtifactResolver.java Sat Dec 26 15:48:15 2015 @@ -24,7 +24,7 @@ import org.apache.maven.project.ProjectB import org.apache.maven.shared.artifact.ArtifactCoordinate; /** - * + * Resolves the artifact, i.e download the file when required and attach it to the artifact */ public interface ArtifactResolver { Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolver.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolver.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolver.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolver.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,63 @@ +package org.apache.maven.shared.dependency.resolve; + +/* + * 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.Collection; + +import org.apache.maven.model.Dependency; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter; +import org.apache.maven.shared.artifact.resolve.ArtifactResult; +import org.apache.maven.shared.dependency.DependencyCoordinate; + +/** + * + */ +public interface DependencyResolver +{ + /** + * This will resolve the dependencies of the coordinate, not resolving the the artifact of the coordinate itself. + * If the coordinate needs to be resolved too, use + * {@link #resolveDependencies(ProjectBuildingRequest, Collection, Collection, TransformableFilter)} passing + * {@code Collections.singletonList(coordinate)} + * + * @param buildingRequest {@link ProjectBuildingRequest} + * @param coordinate {@link DependencyCoordinate} + * @param filter {@link TransformableFilter} + * @return the resolved dependencies. + * @throws DependencyResolverException in case of an error. + */ + Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + DependencyCoordinate coordinate, + TransformableFilter filter ) throws DependencyResolverException; + + /** + * @param buildingRequest the project building request, never {@code null} + * @param dependencies the dependencies to resolve, never {@code null} + * @param managedDependencies managed dependencies, can be {@code null} + * @param filter a filter, can be {@code null} + * @return the resolved dependencies. + * @throws DependencyResolverException in case of an error. + */ + Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + Collection<Dependency> dependencies, + Collection<Dependency> managedDependencies, + TransformableFilter filter ) throws DependencyResolverException; +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolverException.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolverException.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolverException.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/DependencyResolverException.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,43 @@ +package org.apache.maven.shared.dependency.resolve; + +/* + * 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. + */ + +/** + * + */ +public class DependencyResolverException + extends Exception +{ + + /** + * + */ + private static final long serialVersionUID = 8971449574933775007L; + + /** + * @param message The message for the exception. + * @param e The exception itself. + */ + public DependencyResolverException( String message, Exception e ) + { + super( message, e ); + } + +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/DefaultDependencyResolver.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/DefaultDependencyResolver.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/DefaultDependencyResolver.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/DefaultDependencyResolver.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,121 @@ +package org.apache.maven.shared.dependency.resolve.internal; + +/* + * 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.Collection; + +import org.apache.maven.model.Dependency; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter; +import org.apache.maven.shared.artifact.resolve.ArtifactResult; +import org.apache.maven.shared.dependency.DependencyCoordinate; +import org.apache.maven.shared.dependency.resolve.DependencyResolver; +import org.apache.maven.shared.dependency.resolve.DependencyResolverException; +import org.codehaus.plexus.PlexusConstants; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.context.Context; +import org.codehaus.plexus.context.ContextException; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; + +/** + * + */ +@Component( role = DependencyResolver.class, hint = "default" ) +public class DefaultDependencyResolver + implements DependencyResolver, Contextualizable +{ + private PlexusContainer container; + + @Override + public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + Collection<Dependency> coordinates, + Collection<Dependency> managedDependencies, + TransformableFilter filter ) + throws DependencyResolverException + { + try + { + String hint = isMaven31() ? "maven31" : "maven3"; + + DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint ); + + return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinates, null, filter ); + } + catch ( ComponentLookupException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + @Override + public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + DependencyCoordinate coordinate, TransformableFilter filter ) + throws DependencyResolverException + { + try + { + String hint = isMaven31() ? "maven31" : "maven3"; + + DependencyResolver effectiveArtifactResolver = container.lookup( DependencyResolver.class, hint ); + + return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter ); + } + catch ( ComponentLookupException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + /** + * @return true if the current Maven version is Maven 3.1. + */ + protected static boolean isMaven31() + { + return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific + } + + private static boolean canFindCoreClass( String className ) + { + try + { + Thread.currentThread().getContextClassLoader().loadClass( className ); + + return true; + } + catch ( ClassNotFoundException e ) + { + return false; + } + } + + /** + * Injects the Plexus content. + * + * @param context Plexus context to inject. + * @throws ContextException if the PlexusContainer could not be located. + */ + public void contextualize( Context context ) + throws ContextException + { + container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Invoker.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Invoker.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Invoker.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Invoker.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,136 @@ +package org.apache.maven.shared.dependency.resolve.internal; + +/* + * 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.lang.reflect.InvocationTargetException; + +import org.apache.maven.shared.dependency.resolve.DependencyResolverException; + +/** + * Invokes method on objects using reflection. + */ +final class Invoker +{ + private Invoker() + { + // do not instantiate + } + + public static Object invoke( Object object, String method ) + throws DependencyResolverException + { + return invoke( object.getClass(), object, method ); + } + + public static Object invoke( Class<?> objectClazz, Object object, String method ) + throws DependencyResolverException + { + try + { + return objectClazz.getMethod( method ).invoke( object ); + } + catch ( IllegalAccessException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + public static Object invoke( Object object, String method, Class<?> argClazz, Object arg ) + throws DependencyResolverException + { + try + { + final Class<?> objectClazz = object.getClass(); + return objectClazz.getMethod( method, argClazz ).invoke( object, arg ); + } + catch ( IllegalAccessException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?> argClazz, Object arg ) + throws DependencyResolverException + { + try + { + return objectClazz.getMethod( staticMethod, argClazz ).invoke( null, arg ); + } + catch ( IllegalAccessException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + /** + * <strong>Note:</strong> Ensure that argClasses and args have the same number of elements + * + * @param objectClazz the class of the static method + * @param staticMethod the static method to call + * @param argClasses the classes of the argument, used to select the right static method + * @param args the actual arguments to be passed + * @return the result of the method invocation + * @throws ArtifactResolverException if any checked exception occurs + */ + public static Object invoke( Class<?> objectClazz, String staticMethod, Class<?>[] argClasses, Object[] args ) + throws DependencyResolverException + { + try + { + return objectClazz.getMethod( staticMethod, argClasses ).invoke( null, args ); + } + catch ( IllegalAccessException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( InvocationTargetException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( NoSuchMethodException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30ArtifactResult.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30ArtifactResult.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30ArtifactResult.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30ArtifactResult.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,59 @@ +package org.apache.maven.shared.dependency.resolve.internal; + +/* + * 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 org.apache.maven.RepositoryUtils; +import org.apache.maven.shared.dependency.resolve.DependencyResolverException; +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.resolution.ArtifactResult; + +/** + * {@link org.apache.maven.shared.artifact.resolve.ArtifactResult} wrapper for {@link ArtifactResult} + * + * @author Robert Scholte + * @since 3.0 + */ +public class Maven30ArtifactResult + implements org.apache.maven.shared.artifact.resolve.ArtifactResult +{ + private final ArtifactResult artifactResult; + + /** + * @param artifactResult {@link ArtifactResult} + */ + public Maven30ArtifactResult( ArtifactResult artifactResult ) + { + this.artifactResult = artifactResult; + } + + @Override + public org.apache.maven.artifact.Artifact getArtifact() + { + try + { + return (org.apache.maven.artifact.Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact", + Artifact.class, artifactResult.getArtifact() ); + } + catch ( DependencyResolverException e ) + { + throw new RuntimeException( e ); + } + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30DependencyResolver.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30DependencyResolver.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30DependencyResolver.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven30DependencyResolver.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,224 @@ +package org.apache.maven.shared.dependency.resolve.internal; + +/* + * 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.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter; +import org.apache.maven.shared.artifact.filter.resolve.transform.SonatypeAetherFilterTransformer; +import org.apache.maven.shared.dependency.DependencyCoordinate; +import org.apache.maven.shared.dependency.resolve.DependencyResolver; +import org.apache.maven.shared.dependency.resolve.DependencyResolverException; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; + +import org.sonatype.aether.RepositorySystem; +import org.sonatype.aether.RepositorySystemSession; +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.artifact.ArtifactType; +import org.sonatype.aether.artifact.ArtifactTypeRegistry; +import org.sonatype.aether.collection.CollectRequest; +import org.sonatype.aether.collection.DependencyCollectionException; +import org.sonatype.aether.graph.Dependency; +import org.sonatype.aether.graph.DependencyFilter; +import org.sonatype.aether.repository.RemoteRepository; +import org.sonatype.aether.resolution.ArtifactRequest; +import org.sonatype.aether.resolution.ArtifactResolutionException; +import org.sonatype.aether.resolution.ArtifactResult; +import org.sonatype.aether.util.artifact.DefaultArtifact; +import org.sonatype.aether.util.artifact.DefaultArtifactType; + +/** + * + */ +@Component( role = DependencyResolver.class, hint = "maven3" ) +public class Maven30DependencyResolver + implements DependencyResolver +{ + @Requirement + private RepositorySystem repositorySystem; + + @Requirement + private ArtifactHandlerManager artifactHandlerManager; + + @Override + // CHECKSTYLE_OFF: LineLength + public Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + DependencyCoordinate coordinate, + TransformableFilter dependencyFilter ) + // CHECKSTYLE_ON: + // LineLength + throws DependencyResolverException + { + ArtifactTypeRegistry typeRegistry = + (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry", + ArtifactHandlerManager.class, artifactHandlerManager ); + + Dependency aetherRoot = toDependency( coordinate, typeRegistry ); + + @SuppressWarnings( "unchecked" ) + List<RemoteRepository> aetherRepositories = + (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos", List.class, + buildingRequest.getRemoteRepositories() ); + + CollectRequest request = new CollectRequest( aetherRoot, aetherRepositories ); + + return resolveDependencies( buildingRequest, aetherRepositories, dependencyFilter, request ); + } + + @Override + // CHECKSTYLE_OFF: LineLength + public Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + Collection<org.apache.maven.model.Dependency> mavenDependencies, + Collection<org.apache.maven.model.Dependency> managedMavenDependencies, + TransformableFilter filter ) + // CHECKSTYLE_ON: + // LineLength + throws DependencyResolverException + { + ArtifactTypeRegistry typeRegistry = + (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry", + ArtifactHandlerManager.class, artifactHandlerManager ); + + List<Dependency> aetherDependencies = new ArrayList<Dependency>( mavenDependencies.size() ); + + final Class<?>[] argClasses = + new Class<?>[] { org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class }; + + for ( org.apache.maven.model.Dependency mavenDependency : mavenDependencies ) + { + Object[] args = new Object[] { mavenDependency, typeRegistry }; + + Dependency aetherDependency = + (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args ); + + aetherDependencies.add( aetherDependency ); + } + + List<Dependency> aetherManagedDependencies = new ArrayList<Dependency>( managedMavenDependencies.size() ); + + for ( org.apache.maven.model.Dependency mavenDependency : managedMavenDependencies ) + { + Object[] args = new Object[] { mavenDependency, typeRegistry }; + + Dependency aetherDependency = + (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args ); + + aetherManagedDependencies.add( aetherDependency ); + } + + @SuppressWarnings( "unchecked" ) + List<RemoteRepository> aetherRepositories = + (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos", List.class, + buildingRequest.getRemoteRepositories() ); + + CollectRequest request = + new CollectRequest( aetherDependencies, aetherManagedDependencies, aetherRepositories ); + + return resolveDependencies( buildingRequest, aetherRepositories, filter, request ); + } + + // CHECKSTYLE_OFF: LineLength + private Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + List<RemoteRepository> aetherRepositories, + TransformableFilter dependencyFilter, + CollectRequest request ) + throws DependencyResolverException + // CHECKSTYLE_ON :LineLength + { + try + { + DependencyFilter depFilter = null; + if ( dependencyFilter != null ) + { + depFilter = dependencyFilter.transform( new SonatypeAetherFilterTransformer() ); + } + + RepositorySystemSession session = + (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + + List<ArtifactResult> dependencyResults = + repositorySystem.resolveDependencies( session, request, depFilter ); + + Collection<ArtifactRequest> artifactRequests = new ArrayList<ArtifactRequest>( dependencyResults.size() ); + + for ( ArtifactResult artifactResult : dependencyResults ) + { + artifactRequests.add( new ArtifactRequest( artifactResult.getArtifact(), aetherRepositories, null ) ); + } + + final List<ArtifactResult> artifactResults = repositorySystem.resolveArtifacts( session, artifactRequests ); + + // Keep it lazy! Often artifactsResults aren't used, so transforming up front is too expensive + return new Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult>() + { + @Override + public Iterator<org.apache.maven.shared.artifact.resolve.ArtifactResult> iterator() + { + Collection<org.apache.maven.shared.artifact.resolve.ArtifactResult> artResults = + new ArrayList<org.apache.maven.shared.artifact.resolve.ArtifactResult>( artifactResults.size() ); + + for ( ArtifactResult artifactResult : artifactResults ) + { + artResults.add( new Maven30ArtifactResult( artifactResult ) ); + } + + return artResults.iterator(); + } + }; + } + catch ( ArtifactResolutionException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( DependencyCollectionException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + /** + * Based on RepositoryUtils#toDependency(org.apache.maven.model.Dependency, ArtifactTypeRegistry) + * + * @param coordinate + * @param stereotypes + * @return as Aether Dependency + */ + private static Dependency toDependency( DependencyCoordinate coordinate, ArtifactTypeRegistry stereotypes ) + { + ArtifactType stereotype = stereotypes.get( coordinate.getType() ); + if ( stereotype == null ) + { + stereotype = new DefaultArtifactType( coordinate.getType() ); + } + + Artifact artifact = + new DefaultArtifact( coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getClassifier(), null, + coordinate.getVersion(), null, stereotype ); + + return new Dependency( artifact, null ); + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31ArtifactResult.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31ArtifactResult.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31ArtifactResult.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31ArtifactResult.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,59 @@ +package org.apache.maven.shared.dependency.resolve.internal; + +/* + * 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 org.apache.maven.RepositoryUtils; +import org.apache.maven.shared.dependency.resolve.DependencyResolverException; +import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.artifact.Artifact; + +/** + * {@link org.apache.maven.shared.artifact.resolve.ArtifactResult} wrapper for {@link ArtifactResult} + * + * @author Robert Scholte + * @since 3.0 + */ +public class Maven31ArtifactResult + implements org.apache.maven.shared.artifact.resolve.ArtifactResult +{ + private final ArtifactResult artifactResult; + + /** + * @param artifactResult {@link ArtifactResult} + */ + public Maven31ArtifactResult( ArtifactResult artifactResult ) + { + this.artifactResult = artifactResult; + } + + @Override + public org.apache.maven.artifact.Artifact getArtifact() + { + try + { + return (org.apache.maven.artifact.Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact", + Artifact.class, artifactResult.getArtifact() ); + } + catch ( DependencyResolverException e ) + { + throw new RuntimeException( e ); + } + } +} Added: maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31DependencyResolver.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31DependencyResolver.java?rev=1721735&view=auto ============================================================================== --- maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31DependencyResolver.java (added) +++ maven/shared/trunk/maven-artifact-transfer/src/main/java/org/apache/maven/shared/dependency/resolve/internal/Maven31DependencyResolver.java Sat Dec 26 15:48:15 2015 @@ -0,0 +1,226 @@ +package org.apache.maven.shared.dependency.resolve.internal; + +/* + * 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 java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter; +import org.apache.maven.shared.artifact.filter.resolve.transform.EclipseAetherFilterTransformer; +import org.apache.maven.shared.dependency.DependencyCoordinate; +import org.apache.maven.shared.dependency.resolve.DependencyResolver; +import org.apache.maven.shared.dependency.resolve.DependencyResolverException; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.ArtifactType; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.artifact.DefaultArtifactType; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.graph.DependencyFilter; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.resolution.DependencyRequest; +import org.eclipse.aether.resolution.DependencyResolutionException; +import org.eclipse.aether.resolution.DependencyResult; + +/** + * + */ +@Component( role = DependencyResolver.class, hint = "maven31" ) +public class Maven31DependencyResolver + implements DependencyResolver +{ + @Requirement + private RepositorySystem repositorySystem; + + @Requirement + private ArtifactHandlerManager artifactHandlerManager; + + @Override + // CHECKSTYLE_OFF: LineLength + public Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + DependencyCoordinate coordinate, + TransformableFilter dependencyFilter ) + throws DependencyResolverException + // CHECKSTYLE_ON: LineLength + { + ArtifactTypeRegistry typeRegistry = + (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry", + ArtifactHandlerManager.class, artifactHandlerManager ); + + Dependency aetherRoot = toDependency( coordinate, typeRegistry ); + + @SuppressWarnings( "unchecked" ) + List<RemoteRepository> aetherRepositories = + (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos", List.class, + buildingRequest.getRemoteRepositories() ); + + CollectRequest request = new CollectRequest( aetherRoot, aetherRepositories ); + + return resolveDependencies( buildingRequest, aetherRepositories, dependencyFilter, request ); + } + + @Override + // CHECKSTYLE_OFF: LineLength + public Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + Collection<org.apache.maven.model.Dependency> mavenDependencies, + Collection<org.apache.maven.model.Dependency> managedMavenDependencies, + TransformableFilter filter ) + throws DependencyResolverException + // CHECKSTYLE_ON: LineLength + { + ArtifactTypeRegistry typeRegistry = + (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry", + ArtifactHandlerManager.class, artifactHandlerManager ); + + List<Dependency> aetherDeps = new ArrayList<Dependency>( mavenDependencies.size() ); + + final Class<?>[] argClasses = + new Class<?>[] { org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class }; + + for ( org.apache.maven.model.Dependency mavenDependency : mavenDependencies ) + { + Object[] args = new Object[] { mavenDependency, typeRegistry }; + + Dependency aetherDependency = + (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args ); + + aetherDeps.add( aetherDependency ); + } + + List<Dependency> aetherManagedDeps = new ArrayList<Dependency>( managedMavenDependencies.size() ); + + for ( org.apache.maven.model.Dependency mavenDependency : managedMavenDependencies ) + { + Object[] args = new Object[] { mavenDependency, typeRegistry }; + + Dependency aetherDependency = + (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args ); + + aetherManagedDeps.add( aetherDependency ); + } + + @SuppressWarnings( "unchecked" ) + List<RemoteRepository> aetherRepos = + (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos", List.class, + buildingRequest.getRemoteRepositories() ); + + CollectRequest request = new CollectRequest( aetherDeps, aetherManagedDeps, aetherRepos ); + + return resolveDependencies( buildingRequest, aetherRepos, filter, request ); + } + + // CHECKSTYLE_OFF: LineLength + private Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest, + List<RemoteRepository> aetherRepositories, + TransformableFilter dependencyFilter, + CollectRequest request ) + throws DependencyResolverException + // CHECKSTYLE_ON: LineLength + { + try + { + DependencyFilter depFilter = null; + if ( dependencyFilter != null ) + { + depFilter = dependencyFilter.transform( new EclipseAetherFilterTransformer() ); + } + + DependencyRequest depRequest = new DependencyRequest( request, depFilter ); + + RepositorySystemSession session = + (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" ); + + DependencyResult dependencyResults = repositorySystem.resolveDependencies( session, depRequest ); + + Collection<ArtifactRequest> artifactRequests = + new ArrayList<ArtifactRequest>( dependencyResults.getArtifactResults().size() ); + + for ( ArtifactResult artifactResult : dependencyResults.getArtifactResults() ) + { + artifactRequests.add( new ArtifactRequest( artifactResult.getArtifact(), aetherRepositories, null ) ); + } + + final List<ArtifactResult> artifactResults = repositorySystem.resolveArtifacts( session, artifactRequests ); + + // Keep it lazy! Often artifactsResults aren't used, so transforming up front is too expensive + return new Iterable<org.apache.maven.shared.artifact.resolve.ArtifactResult>() + { + @Override + public Iterator<org.apache.maven.shared.artifact.resolve.ArtifactResult> iterator() + { + // CHECKSTYLE_OFF: LineLength + Collection<org.apache.maven.shared.artifact.resolve.ArtifactResult> artResults = + new ArrayList<org.apache.maven.shared.artifact.resolve.ArtifactResult>( artifactResults.size() ); + // CHECKSTYLE_ON: LineLength + + for ( ArtifactResult artifactResult : artifactResults ) + { + artResults.add( new Maven31ArtifactResult( artifactResult ) ); + } + + return artResults.iterator(); + } + }; + } + catch ( ArtifactResolutionException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + catch ( DependencyResolutionException e ) + { + throw new DependencyResolverException( e.getMessage(), e ); + } + } + + /** + * Based on RepositoryUtils#toDependency(org.apache.maven.model.Dependency, ArtifactTypeRegistry) + * + * @param coordinate {@link DependencyCoordinate} + * @param stereotypes {@link ArtifactTypeRegistry + * @return as Aether Dependency + */ + private static Dependency toDependency( DependencyCoordinate coordinate, ArtifactTypeRegistry stereotypes ) + { + ArtifactType stereotype = stereotypes.get( coordinate.getType() ); + if ( stereotype == null ) + { + stereotype = new DefaultArtifactType( coordinate.getType() ); + } + + Artifact artifact = + new DefaultArtifact( coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getClassifier(), null, + coordinate.getVersion(), null, stereotype ); + + return new Dependency( artifact, null ); + } +}