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.common.utils.VersionUtil;
34 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
35 import org.apache.archiva.maven2.model.TreeEntry;
36 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
37 import org.apache.archiva.model.ArchivaRepositoryMetadata;
38 import org.apache.archiva.proxy.common.WagonFactory;
39 import org.apache.archiva.repository.metadata.MetadataTools;
40 import org.apache.archiva.xml.XMLException;
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.maven.artifact.Artifact;
43 import org.apache.maven.artifact.factory.ArtifactFactory;
44 import org.apache.maven.model.building.DefaultModelBuilderFactory;
45 import org.apache.maven.model.building.ModelBuilder;
46 import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
47 import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
48 import org.apache.maven.repository.internal.DefaultVersionResolver;
49 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.sonatype.aether.RepositorySystem;
53 import org.sonatype.aether.RepositorySystemSession;
54 import org.sonatype.aether.collection.CollectRequest;
55 import org.sonatype.aether.collection.CollectResult;
56 import org.sonatype.aether.collection.DependencyCollectionException;
57 import org.sonatype.aether.collection.DependencySelector;
58 import org.sonatype.aether.graph.Dependency;
59 import org.sonatype.aether.graph.DependencyVisitor;
60 import org.sonatype.aether.impl.ArtifactDescriptorReader;
61 import org.sonatype.aether.impl.VersionRangeResolver;
62 import org.sonatype.aether.impl.VersionResolver;
63 import org.sonatype.aether.impl.internal.DefaultServiceLocator;
64 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
65 import org.sonatype.aether.repository.LocalRepository;
66 import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
67 import org.sonatype.aether.util.artifact.DefaultArtifact;
68 import org.sonatype.aether.util.graph.selector.AndDependencySelector;
69 import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
70 import org.springframework.stereotype.Service;
72 import javax.annotation.PostConstruct;
73 import javax.inject.Inject;
74 import javax.inject.Named;
76 import java.util.ArrayList;
77 import java.util.HashMap;
78 import java.util.List;
82 * @author Olivier Lamy
85 @Service("dependencyTreeBuilder#maven3")
86 public class Maven3DependencyTreeBuilder
87 implements DependencyTreeBuilder
89 private Logger log = LoggerFactory.getLogger( getClass() );
92 private PlexusSisuBridge plexusSisuBridge;
95 @Named( "repositoryPathTranslator#maven2" )
96 private RepositoryPathTranslator pathTranslator;
99 private WagonFactory wagonFactory;
102 private ManagedRepositoryAdmin managedRepositoryAdmin;
105 private ProxyConnectorAdmin proxyConnectorAdmin;
108 private NetworkProxyAdmin networkProxyAdmin;
111 private RemoteRepositoryAdmin remoteRepositoryAdmin;
113 private ArtifactFactory factory;
115 private ModelBuilder builder;
118 public void initialize()
119 throws PlexusSisuBridgeException
121 factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" );
123 DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory();
124 builder = defaultModelBuilderFactory.newInstance();
128 public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
129 DependencyVisitor dependencyVisitor )
130 throws DependencyTreeBuilderException
132 Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
133 ManagedRepository repository = null;
136 repository = findArtifactInRepositories( repositoryIds, projectArtifact );
138 catch ( RepositoryAdminException e )
140 // FIXME better exception
141 throw new DependencyTreeBuilderException( "Cannot build project dependency tree " + e.getMessage(), e );
144 if ( repository == null )
146 // metadata could not be resolved
150 List<RemoteRepository> remoteRepositories = new ArrayList<>();
151 Map<String, NetworkProxy> networkProxies = new HashMap<>();
156 // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
157 // handled there. It doesn't cache anything locally!
159 Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
160 List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
161 if ( proxyConnectors != null )
163 for ( ProxyConnector proxyConnector : proxyConnectors )
165 remoteRepositories.add(
166 remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
168 NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
170 if ( networkProxyConfig != null )
172 // key/value: remote repo ID/proxy info
173 networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
178 catch ( RepositoryAdminException e )
180 throw new DependencyTreeBuilderException( e.getMessage(), e );
183 // FIXME take care of relative path
184 ResolveRequest resolveRequest = new ResolveRequest();
185 resolveRequest.dependencyVisitor = dependencyVisitor;
186 resolveRequest.localRepoDir = repository.getLocation();
187 resolveRequest.groupId = groupId;
188 resolveRequest.artifactId = artifactId;
189 resolveRequest.version = version;
190 resolveRequest.remoteRepositories = remoteRepositories;
191 resolveRequest.networkProxies = networkProxies;
192 resolve( resolveRequest );
197 public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId,
199 throws DependencyTreeBuilderException
202 List<TreeEntry> treeEntries = new ArrayList<>();
203 TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
205 buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor );
207 log.debug( "treeEntrie: {}", treeEntries );
211 private static class ResolveRequest
213 String localRepoDir, groupId, artifactId, version;
215 DependencyVisitor dependencyVisitor;
217 List<RemoteRepository> remoteRepositories;
219 Map<String, NetworkProxy> networkProxies;
224 private void resolve( ResolveRequest resolveRequest )
227 RepositorySystem system = newRepositorySystem();
229 RepositorySystemSession session = newRepositorySystemSession( system, resolveRequest.localRepoDir );
231 org.sonatype.aether.artifact.Artifact artifact = new DefaultArtifact(
232 resolveRequest.groupId + ":" + resolveRequest.artifactId + ":" + resolveRequest.version );
234 CollectRequest collectRequest = new CollectRequest();
235 collectRequest.setRoot( new Dependency( artifact, "" ) );
237 // add remote repositories
238 for ( RemoteRepository remoteRepository : resolveRequest.remoteRepositories )
240 collectRequest.addRepository(
241 new org.sonatype.aether.repository.RemoteRepository( remoteRepository.getId(), "default",
242 remoteRepository.getUrl() ) );
244 collectRequest.setRequestContext( "project" );
246 //collectRequest.addRepository( repo );
250 CollectResult collectResult = system.collectDependencies( session, collectRequest );
251 collectResult.getRoot().accept( resolveRequest.dependencyVisitor );
254 catch ( DependencyCollectionException e )
256 log.error( e.getMessage(), e );
262 private RepositorySystem newRepositorySystem()
264 DefaultServiceLocator locator = new DefaultServiceLocator();
265 locator.addService( RepositoryConnectorFactory.class,
266 ArchivaRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class );
267 locator.addService( VersionResolver.class, DefaultVersionResolver.class );
268 locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
269 locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
270 //locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
271 //locator.setServices( WagonProvider.class, );
273 return locator.getService( RepositorySystem.class );
276 private RepositorySystemSession newRepositorySystemSession( RepositorySystem system, String localRepoDir )
278 MavenRepositorySystemSession session = new MavenRepositorySystemSession();
280 DependencySelector depFilter = new AndDependencySelector( new ExclusionDependencySelector() );
281 session.setDependencySelector( depFilter );
283 LocalRepository localRepo = new LocalRepository( localRepoDir );
284 session.setLocalRepositoryManager(
285 new SimpleLocalRepositoryManager( localRepoDir ) );// system.newLocalRepositoryManager( localRepo ) );
287 //session.setTransferListener( );
288 //session.setRepositoryListener( n );
294 private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact )
295 throws RepositoryAdminException
297 for ( String repoId : repositoryIds )
299 ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
301 File repoDir = new File( managedRepository.getLocation() );
302 File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
303 projectArtifact.getBaseVersion(),
304 projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
309 return managedRepository;
311 // try with snapshot version
312 if ( StringUtils.endsWith( projectArtifact.getBaseVersion(), VersionUtil.SNAPSHOT ) )
314 File metadataFile = new File( file.getParent(), MetadataTools.MAVEN_METADATA );
315 if ( metadataFile.exists() )
319 ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read( metadataFile );
320 int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
321 String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
322 // rebuild file name with timestamped version and build number
323 String timeStampFileName =
324 new StringBuilder( projectArtifact.getArtifactId() ).append( '-' ).append(
325 StringUtils.remove( projectArtifact.getBaseVersion(),
326 "-" + VersionUtil.SNAPSHOT ) ).append( '-' ).append(
327 timeStamp ).append( '-' ).append( Integer.toString( buildNumber ) ).append(
329 File timeStampFile = new File( file.getParent(), timeStampFileName );
330 log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.getPath() );
331 if ( timeStampFile.exists() )
333 return managedRepository;
336 catch ( XMLException e )
338 log.warn( "skip fail to find timestamped snapshot pom: {}", e.getMessage() );