1 package org.apache.archiva.repository.maven.dependency.tree;
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
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.NetworkProxy;
23 import org.apache.archiva.admin.model.beans.ProxyConnector;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
26 import org.apache.archiva.common.utils.VersionUtil;
27 import org.apache.archiva.maven2.model.TreeEntry;
28 import org.apache.archiva.metadata.maven.MavenMetadataReader;
29 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
30 import org.apache.archiva.model.ArchivaRepositoryMetadata;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.RemoteRepository;
33 import org.apache.archiva.repository.RepositoryRegistry;
34 import org.apache.archiva.repository.maven.MavenSystemManager;
35 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
36 import org.apache.archiva.repository.metadata.base.MetadataTools;
37 import org.apache.archiva.repository.storage.StorageAsset;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.commons.lang3.reflect.FieldUtils;
40 import org.apache.maven.artifact.Artifact;
41 import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
42 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
43 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
44 import org.apache.maven.bridge.MavenRepositorySystem;
45 import org.eclipse.aether.RepositorySystem;
46 import org.eclipse.aether.RepositorySystemSession;
47 import org.eclipse.aether.artifact.DefaultArtifact;
48 import org.eclipse.aether.collection.CollectRequest;
49 import org.eclipse.aether.collection.CollectResult;
50 import org.eclipse.aether.collection.DependencyCollectionException;
51 import org.eclipse.aether.graph.Dependency;
52 import org.eclipse.aether.graph.DependencyVisitor;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.stereotype.Service;
57 import javax.annotation.PostConstruct;
58 import javax.inject.Inject;
59 import javax.inject.Named;
60 import java.util.ArrayList;
61 import java.util.HashMap;
62 import java.util.List;
66 * @author Olivier Lamy
69 @Service("dependencyTreeBuilder#maven3")
70 public class Maven3DependencyTreeBuilder
71 implements DependencyTreeBuilder
73 private Logger log = LoggerFactory.getLogger( Maven3DependencyTreeBuilder.class );
75 private MavenRepositorySystem mavenRepositorySystem;
78 @Named( "repositoryPathTranslator#maven2" )
79 private RepositoryPathTranslator pathTranslator;
82 @Named("metadataReader#maven")
83 private MavenMetadataReader metadataReader;
86 private ProxyConnectorAdmin proxyConnectorAdmin;
89 private NetworkProxyAdmin networkProxyAdmin;
92 RepositoryRegistry repositoryRegistry;
95 MavenSystemManager mavenSystemManager;
99 public void initialize()
100 throws RuntimeException
104 mavenRepositorySystem = initMaven( );
106 catch ( IllegalAccessException e )
108 throw new RuntimeException( "Could not initialize maven" );
112 MavenRepositorySystem initMaven() throws IllegalAccessException
114 MavenRepositorySystem system = new MavenRepositorySystem( );
115 DefaultArtifactHandlerManager afm = new DefaultArtifactHandlerManager( );
116 DefaultRepositoryLayout layout = new DefaultRepositoryLayout( );
117 FieldUtils.writeField( system, "artifactHandlerManager", afm, true);
118 Map<String, ArtifactRepositoryLayout> map = new HashMap<>( );
119 map.put( "defaultRepositoryLayout", layout );
120 FieldUtils.writeField( system, "layouts", map, true);
126 public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
127 DependencyVisitor dependencyVisitor )
128 throws DependencyTreeBuilderException
131 Artifact projectArtifact = mavenRepositorySystem.createProjectArtifact(groupId, artifactId, version);
132 ManagedRepository repository = findArtifactInRepositories( repositoryIds, projectArtifact );
134 if ( repository == null )
136 // metadata could not be resolved
137 log.info("Did not find repository with artifact {}/{}/{}", groupId, artifactId, version);
141 List<org.apache.archiva.repository.RemoteRepository> remoteRepositories = new ArrayList<>();
142 Map<String, NetworkProxy> networkProxies = new HashMap<>();
147 // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
148 // handled there. It doesn't cache anything locally!
150 Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
151 List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
152 if ( proxyConnectors != null )
154 for ( ProxyConnector proxyConnector : proxyConnectors )
156 remoteRepositories.add(
157 repositoryRegistry.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
159 NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
161 if ( networkProxyConfig != null )
163 // key/value: remote repo ID/proxy info
164 networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
169 catch ( RepositoryAdminException e )
171 throw new DependencyTreeBuilderException( e.getMessage(), e );
174 // FIXME take care of relative path
175 ResolveRequest resolveRequest = new ResolveRequest();
176 resolveRequest.dependencyVisitor = dependencyVisitor;
177 resolveRequest.localRepoDir = repository.getRoot().getFilePath().toAbsolutePath().toString();
178 resolveRequest.groupId = groupId;
179 resolveRequest.artifactId = artifactId;
180 resolveRequest.version = version;
181 resolveRequest.remoteRepositories = remoteRepositories;
182 resolveRequest.networkProxies = networkProxies;
183 resolve( resolveRequest );
188 public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId,
190 throws DependencyTreeBuilderException
193 List<TreeEntry> treeEntries = new ArrayList<>();
194 TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
196 buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor );
198 log.debug( "treeEntries: {}", treeEntries );
202 private static class ResolveRequest
204 String localRepoDir, groupId, artifactId, version;
206 DependencyVisitor dependencyVisitor;
208 List<org.apache.archiva.repository.RemoteRepository> remoteRepositories;
210 Map<String, NetworkProxy> networkProxies;
215 private void resolve( ResolveRequest resolveRequest )
218 RepositorySystem system = mavenSystemManager.getRepositorySystem();
219 RepositorySystemSession session = MavenSystemManager.newRepositorySystemSession( resolveRequest.localRepoDir );
221 org.eclipse.aether.artifact.Artifact artifact = new DefaultArtifact(
222 resolveRequest.groupId + ":" + resolveRequest.artifactId + ":" + resolveRequest.version );
224 CollectRequest collectRequest = new CollectRequest();
225 collectRequest.setRoot( new Dependency( artifact, "" ) );
227 // add remote repositories
228 for ( RemoteRepository remoteRepository : resolveRequest.remoteRepositories )
230 org.eclipse.aether.repository.RemoteRepository repo = new org.eclipse.aether.repository.RemoteRepository.Builder( remoteRepository.getId( ), "default", remoteRepository.getLocation( ).toString() ).build( );
231 collectRequest.addRepository(repo);
233 collectRequest.setRequestContext( "project" );
235 //collectRequest.addRepository( repo );
239 CollectResult collectResult = system.collectDependencies( session, collectRequest );
240 collectResult.getRoot().accept( resolveRequest.dependencyVisitor );
241 log.debug("Collected dependency results for resolve");
243 catch ( DependencyCollectionException e )
245 log.error( "Error while collecting dependencies (resolve): {}", e.getMessage(), e );
252 private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact ) {
253 for ( String repoId : repositoryIds )
255 ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repoId);
256 StorageAsset repoDir = managedRepo.getRoot();
258 StorageAsset file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
259 projectArtifact.getBaseVersion(),
260 projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
267 // try with snapshot version
268 if ( StringUtils.endsWith( projectArtifact.getBaseVersion(), VersionUtil.SNAPSHOT ) )
270 StorageAsset metadataFile = file.getParent().resolve( MetadataTools.MAVEN_METADATA );
271 if ( metadataFile.exists() )
275 ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read( metadataFile);
276 int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
277 String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
278 // rebuild file name with timestamped version and build number
279 String timeStampFileName =
280 new StringBuilder( projectArtifact.getArtifactId() ).append( '-' ).append(
281 StringUtils.remove( projectArtifact.getBaseVersion(),
282 "-" + VersionUtil.SNAPSHOT ) ).append( '-' ).append(
283 timeStamp ).append( '-' ).append( Integer.toString( buildNumber ) ).append(
285 StorageAsset timeStampFile = file.getParent().resolve( timeStampFileName );
286 log.debug( "try to find timestamped snapshot version file: {}", timeStampFile);
287 if ( timeStampFile.exists() )
292 catch ( RepositoryMetadataException e )
294 log.warn( "skip fail to find timestamped snapshot pom: {}", e.getMessage() );