001package org.apache.maven; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.Collection; 023import java.util.Collections; 024import java.util.HashSet; 025import java.util.Iterator; 026import java.util.LinkedHashSet; 027import java.util.Set; 028 029import org.apache.maven.artifact.Artifact; 030import org.apache.maven.artifact.ArtifactUtils; 031import org.apache.maven.artifact.resolver.ArtifactNotFoundException; 032import org.apache.maven.artifact.resolver.ArtifactResolutionException; 033import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; 034import org.apache.maven.artifact.resolver.ArtifactResolutionResult; 035import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException; 036import org.apache.maven.artifact.resolver.ResolutionErrorHandler; 037import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter; 038import org.apache.maven.execution.MavenSession; 039import org.apache.maven.project.MavenProject; 040import org.apache.maven.project.artifact.ProjectArtifact; 041import org.apache.maven.repository.RepositorySystem; 042import org.codehaus.plexus.component.annotations.Component; 043import org.codehaus.plexus.component.annotations.Requirement; 044 045@Deprecated 046@Component( role = ProjectDependenciesResolver.class ) 047public class DefaultProjectDependenciesResolver 048 implements ProjectDependenciesResolver 049{ 050 051 @Requirement 052 private RepositorySystem repositorySystem; 053 054 @Requirement 055 private ResolutionErrorHandler resolutionErrorHandler; 056 057 public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session ) 058 throws ArtifactResolutionException, ArtifactNotFoundException 059 { 060 return resolve( Collections.singleton( project ), scopesToResolve, session ); 061 } 062 063 public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect, 064 Collection<String> scopesToResolve, MavenSession session ) 065 throws ArtifactResolutionException, ArtifactNotFoundException 066 { 067 Set<MavenProject> mavenProjects = Collections.singleton( project ); 068 return resolveImpl( mavenProjects, scopesToCollect, scopesToResolve, session, 069 getIgnorableArtifacts( mavenProjects ) ); 070 } 071 072 public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToResolve, 073 MavenSession session ) 074 throws ArtifactResolutionException, ArtifactNotFoundException 075 { 076 return resolveImpl( projects, null, scopesToResolve, session, getIgnorableArtifacts( projects ) ); 077 } 078 079 public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect, 080 Collection<String> scopesToResolve, MavenSession session, 081 Set<Artifact> ignoreableArtifacts ) 082 throws ArtifactResolutionException, ArtifactNotFoundException 083 { 084 return resolveImpl( Collections.singleton( project ), scopesToCollect, scopesToResolve, session, 085 getIgnorableArtifacts( ignoreableArtifacts ) ); 086 } 087 088 089 private Set<Artifact> resolveImpl( Collection<? extends MavenProject> projects, Collection<String> scopesToCollect, 090 Collection<String> scopesToResolve, MavenSession session, 091 Set<String> projectIds ) 092 throws ArtifactResolutionException, ArtifactNotFoundException 093 { 094 Set<Artifact> resolved = new LinkedHashSet<Artifact>(); 095 096 if ( projects == null || projects.isEmpty() ) 097 { 098 return resolved; 099 } 100 101 if ( ( scopesToCollect == null || scopesToCollect.isEmpty() ) 102 && ( scopesToResolve == null || scopesToResolve.isEmpty() ) ) 103 { 104 return resolved; 105 } 106 107 /* 108 109 Logic for transitive global exclusions 110 111 List<String> exclusions = new ArrayList<String>(); 112 113 for ( Dependency d : project.getDependencies() ) 114 { 115 if ( d.getExclusions() != null ) 116 { 117 for ( Exclusion e : d.getExclusions() ) 118 { 119 exclusions.add( e.getGroupId() + ":" + e.getArtifactId() ); 120 } 121 } 122 } 123 124 ArtifactFilter scopeFilter = new ScopeArtifactFilter( scope ); 125 126 ArtifactFilter filter; 127 128 if ( ! exclusions.isEmpty() ) 129 { 130 filter = new AndArtifactFilter( Arrays.asList( new ArtifactFilter[]{ new ExcludesArtifactFilter( exclusions ), scopeFilter } ) ); 131 } 132 else 133 { 134 filter = scopeFilter; 135 } 136 */ 137 138 CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter( scopesToResolve ); 139 140 CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter( scopesToCollect ); 141 collectionScopeFilter = new CumulativeScopeArtifactFilter( collectionScopeFilter, resolutionScopeFilter ); 142 143 ArtifactResolutionRequest request = 144 new ArtifactResolutionRequest().setResolveRoot( false ).setResolveTransitively( true ).setCollectionFilter( 145 collectionScopeFilter ).setResolutionFilter( resolutionScopeFilter ).setLocalRepository( 146 session.getLocalRepository() ).setOffline( session.isOffline() ).setForceUpdate( 147 session.getRequest().isUpdateSnapshots() ); 148 request.setServers( session.getRequest().getServers() ); 149 request.setMirrors( session.getRequest().getMirrors() ); 150 request.setProxies( session.getRequest().getProxies() ); 151 152 for ( MavenProject project : projects ) 153 { 154 request.setArtifact( new ProjectArtifact( project ) ); 155 request.setArtifactDependencies( project.getDependencyArtifacts() ); 156 request.setManagedVersionMap( project.getManagedVersionMap() ); 157 request.setRemoteRepositories( project.getRemoteArtifactRepositories() ); 158 159 ArtifactResolutionResult result = repositorySystem.resolve( request ); 160 161 try 162 { 163 resolutionErrorHandler.throwErrors( request, result ); 164 } 165 catch ( MultipleArtifactsNotFoundException e ) 166 { 167 168 Collection<Artifact> missing = new HashSet<Artifact>( e.getMissingArtifacts() ); 169 170 for ( Iterator<Artifact> it = missing.iterator(); it.hasNext(); ) 171 { 172 String key = ArtifactUtils.key( it.next() ); 173 if ( projectIds.contains( key ) ) 174 { 175 it.remove(); 176 } 177 } 178 179 if ( !missing.isEmpty() ) 180 { 181 throw e; 182 } 183 } 184 185 resolved.addAll( result.getArtifacts() ); 186 } 187 188 return resolved; 189 } 190 191 192 private Set<String> getIgnorableArtifacts( Collection<? extends MavenProject> projects ) 193 { 194 Set<String> projectIds = new HashSet<String>( projects.size() * 2 ); 195 196 for ( MavenProject p : projects ) 197 { 198 String key = ArtifactUtils.key( p.getGroupId(), p.getArtifactId(), p.getVersion() ); 199 projectIds.add( key ); 200 } 201 return projectIds; 202 } 203 204 private Set<String> getIgnorableArtifacts( Iterable<Artifact> artifactIterable ) 205 { 206 Set<String> projectIds = new HashSet<String>(); 207 208 for ( Artifact artifact : artifactIterable ) 209 { 210 String key = ArtifactUtils.key( artifact ); 211 projectIds.add( key ); 212 } 213 return projectIds; 214 } 215 216}