1 package org.apache.archiva.dependency.tree.maven2;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
25 import org.apache.archiva.admin.model.beans.ProxyConnector;
26 import org.apache.archiva.admin.model.beans.RemoteRepository;
27 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
28 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
29 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
30 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
31 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
32 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
33 import org.apache.archiva.maven2.model.TreeEntry;
34 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
35 import org.apache.archiva.proxy.common.WagonFactory;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.factory.ArtifactFactory;
38 import org.apache.maven.model.building.DefaultModelBuilderFactory;
39 import org.apache.maven.model.building.ModelBuilder;
40 import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
41 import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
42 import org.apache.maven.repository.internal.DefaultVersionResolver;
43 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.sonatype.aether.RepositorySystem;
47 import org.sonatype.aether.RepositorySystemSession;
48 import org.sonatype.aether.collection.CollectRequest;
49 import org.sonatype.aether.collection.CollectResult;
50 import org.sonatype.aether.collection.DependencyCollectionException;
51 import org.sonatype.aether.collection.DependencySelector;
52 import org.sonatype.aether.graph.Dependency;
53 import org.sonatype.aether.graph.DependencyVisitor;
54 import org.sonatype.aether.impl.ArtifactDescriptorReader;
55 import org.sonatype.aether.impl.VersionRangeResolver;
56 import org.sonatype.aether.impl.VersionResolver;
57 import org.sonatype.aether.impl.internal.DefaultServiceLocator;
58 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
59 import org.sonatype.aether.repository.LocalRepository;
60 import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
61 import org.sonatype.aether.util.artifact.DefaultArtifact;
62 import org.sonatype.aether.util.graph.selector.AndDependencySelector;
63 import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
64 import org.springframework.stereotype.Service;
66 import javax.annotation.PostConstruct;
67 import javax.inject.Inject;
68 import javax.inject.Named;
70 import java.util.ArrayList;
71 import java.util.HashMap;
72 import java.util.List;
76 * @author Olivier Lamy
79 @Service( "dependencyTreeBuilder#maven3" )
80 public class Maven3DependencyTreeBuilder
81 implements DependencyTreeBuilder
83 private Logger log = LoggerFactory.getLogger( getClass() );
86 private PlexusSisuBridge plexusSisuBridge;
89 @Named( value = "repositoryPathTranslator#maven2" )
90 private RepositoryPathTranslator pathTranslator;
93 private WagonFactory wagonFactory;
96 private ManagedRepositoryAdmin managedRepositoryAdmin;
99 private ProxyConnectorAdmin proxyConnectorAdmin;
102 private NetworkProxyAdmin networkProxyAdmin;
105 private RemoteRepositoryAdmin remoteRepositoryAdmin;
107 private ArtifactFactory factory;
109 private ModelBuilder builder;
112 private RepositorySystem repoSystem;
115 public void initialize()
116 throws PlexusSisuBridgeException
118 factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" );
120 repoSystem = plexusSisuBridge.lookup( RepositorySystem.class );
121 DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory();
122 builder = defaultModelBuilderFactory.newInstance();
125 public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
126 DependencyVisitor dependencyVisitor )
127 throws DependencyTreeBuilderException
129 Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
130 ManagedRepository repository = null;
133 repository = findArtifactInRepositories( repositoryIds, projectArtifact );
135 catch ( RepositoryAdminException e )
137 // FIXME better exception
138 throw new DependencyTreeBuilderException( "Cannot build project dependency tree " + e.getMessage(), e );
141 if ( repository == null )
143 // metadata could not be resolved
150 // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
151 // handled there. It doesn't cache anything locally!
152 List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
153 Map<String, NetworkProxy> networkProxies = new HashMap<String, NetworkProxy>();
155 Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
156 List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
157 if ( proxyConnectors != null )
159 for ( ProxyConnector proxyConnector : proxyConnectors )
161 remoteRepositories.add( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
163 NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
165 if ( networkProxyConfig != null )
167 // key/value: remote repo ID/proxy info
168 networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
173 catch ( RepositoryAdminException e )
175 throw new DependencyTreeBuilderException( e.getMessage(), e );
178 // FIXME take care of relative path
179 resolve( repository.getLocation(), groupId, artifactId, version, dependencyVisitor );
183 public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId,
185 throws DependencyTreeBuilderException
188 List<TreeEntry> treeEntries = new ArrayList<TreeEntry>();
189 TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
191 buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor );
193 log.debug( "treeEntrie: {}", treeEntries );
198 private void resolve( String localRepoDir, String groupId, String artifactId, String version,
199 DependencyVisitor dependencyVisitor )
202 RepositorySystem system = newRepositorySystem();
204 RepositorySystemSession session = newRepositorySystemSession( system, localRepoDir );
206 org.sonatype.aether.artifact.Artifact artifact =
207 new DefaultArtifact( groupId + ":" + artifactId + ":" + version );
209 CollectRequest collectRequest = new CollectRequest();
210 collectRequest.setRoot( new Dependency( artifact, "" ) );
212 // add remote repositories ?
213 //collectRequest.addRepository( )
215 collectRequest.setRequestContext( "project" );
217 //collectRequest.addRepository( repo );
221 CollectResult collectResult = system.collectDependencies( session, collectRequest );
222 collectResult.getRoot().accept( dependencyVisitor );
225 catch ( DependencyCollectionException e )
227 log.error( e.getMessage(), e );
233 public static RepositorySystem newRepositorySystem()
235 DefaultServiceLocator locator = new DefaultServiceLocator();
236 locator.addService( RepositoryConnectorFactory.class,
237 ArchivaRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class );
238 locator.addService( VersionResolver.class, DefaultVersionResolver.class );
239 locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
240 locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
241 //locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
242 //locator.setServices( WagonProvider.class, );
244 return locator.getService( RepositorySystem.class );
247 public static RepositorySystemSession newRepositorySystemSession( RepositorySystem system, String localRepoDir )
249 MavenRepositorySystemSession session = new MavenRepositorySystemSession();
251 DependencySelector depFilter = new AndDependencySelector( new ExclusionDependencySelector() );
252 session.setDependencySelector( depFilter );
254 LocalRepository localRepo = new LocalRepository( localRepoDir );
255 session.setLocalRepositoryManager(
256 new SimpleLocalRepositoryManager( localRepoDir ) );// system.newLocalRepositoryManager( localRepo ) );
258 //session.setTransferListener( );
259 //session.setRepositoryListener( n );
265 private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact )
266 throws RepositoryAdminException
268 for ( String repoId : repositoryIds )
270 ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
272 File repoDir = new File( managedRepository.getLocation() );
273 File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
274 projectArtifact.getBaseVersion(),
275 projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
280 return managedRepository;