This project has retired. For details please refer to its
Attic page.
Maven3DependencyTreeBuilder xref
1 package org.apache.archiva.dependency.tree.maven2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
71
72 import javax.annotation.PostConstruct;
73 import javax.inject.Inject;
74 import javax.inject.Named;
75 import java.io.File;
76 import java.util.ArrayList;
77 import java.util.HashMap;
78 import java.util.List;
79 import java.util.Map;
80
81
82
83
84
85 @Service("dependencyTreeBuilder#maven3")
86 public class Maven3DependencyTreeBuilder
87 implements DependencyTreeBuilder
88 {
89 private Logger log = LoggerFactory.getLogger( getClass() );
90
91 @Inject
92 private PlexusSisuBridge plexusSisuBridge;
93
94 @Inject
95 @Named( "repositoryPathTranslator#maven2" )
96 private RepositoryPathTranslator pathTranslator;
97
98 @Inject
99 private WagonFactory wagonFactory;
100
101 @Inject
102 private ManagedRepositoryAdmin managedRepositoryAdmin;
103
104 @Inject
105 private ProxyConnectorAdmin proxyConnectorAdmin;
106
107 @Inject
108 private NetworkProxyAdmin networkProxyAdmin;
109
110 @Inject
111 private RemoteRepositoryAdmin remoteRepositoryAdmin;
112
113 private ArtifactFactory factory;
114
115 private ModelBuilder builder;
116
117 @PostConstruct
118 public void initialize()
119 throws PlexusSisuBridgeException
120 {
121 factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" );
122
123 DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory();
124 builder = defaultModelBuilderFactory.newInstance();
125 }
126
127 @Override
128 public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
129 DependencyVisitor dependencyVisitor )
130 throws DependencyTreeBuilderException
131 {
132 Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
133 ManagedRepository repository = null;
134 try
135 {
136 repository = findArtifactInRepositories( repositoryIds, projectArtifact );
137 }
138 catch ( RepositoryAdminException e )
139 {
140
141 throw new DependencyTreeBuilderException( "Cannot build project dependency tree " + e.getMessage(), e );
142 }
143
144 if ( repository == null )
145 {
146
147 return;
148 }
149
150 List<RemoteRepository> remoteRepositories = new ArrayList<>();
151 Map<String, NetworkProxy> networkProxies = new HashMap<>();
152
153 try
154 {
155
156
157
158
159 Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
160 List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
161 if ( proxyConnectors != null )
162 {
163 for ( ProxyConnector proxyConnector : proxyConnectors )
164 {
165 remoteRepositories.add(
166 remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
167
168 NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
169
170 if ( networkProxyConfig != null )
171 {
172
173 networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
174 }
175 }
176 }
177 }
178 catch ( RepositoryAdminException e )
179 {
180 throw new DependencyTreeBuilderException( e.getMessage(), e );
181 }
182
183
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 );
193 }
194
195
196 @Override
197 public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId,
198 String version )
199 throws DependencyTreeBuilderException
200 {
201
202 List<TreeEntry> treeEntries = new ArrayList<>();
203 TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
204
205 buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor );
206
207 log.debug( "treeEntrie: {}", treeEntries );
208 return treeEntries;
209 }
210
211 private static class ResolveRequest
212 {
213 String localRepoDir, groupId, artifactId, version;
214
215 DependencyVisitor dependencyVisitor;
216
217 List<RemoteRepository> remoteRepositories;
218
219 Map<String, NetworkProxy> networkProxies;
220
221 }
222
223
224 private void resolve( ResolveRequest resolveRequest )
225 {
226
227 RepositorySystem system = newRepositorySystem();
228
229 RepositorySystemSession session = newRepositorySystemSession( system, resolveRequest.localRepoDir );
230
231 org.sonatype.aether.artifact.Artifact artifact = new DefaultArtifact(
232 resolveRequest.groupId + ":" + resolveRequest.artifactId + ":" + resolveRequest.version );
233
234 CollectRequest collectRequest = new CollectRequest();
235 collectRequest.setRoot( new Dependency( artifact, "" ) );
236
237
238 for ( RemoteRepository remoteRepository : resolveRequest.remoteRepositories )
239 {
240 collectRequest.addRepository(
241 new org.sonatype.aether.repository.RemoteRepository( remoteRepository.getId(), "default",
242 remoteRepository.getUrl() ) );
243 }
244 collectRequest.setRequestContext( "project" );
245
246
247
248 try
249 {
250 CollectResult collectResult = system.collectDependencies( session, collectRequest );
251 collectResult.getRoot().accept( resolveRequest.dependencyVisitor );
252 log.debug( "test" );
253 }
254 catch ( DependencyCollectionException e )
255 {
256 log.error( e.getMessage(), e );
257 }
258
259
260 }
261
262 private RepositorySystem newRepositorySystem()
263 {
264 DefaultServiceLocator locator = new DefaultServiceLocator();
265 locator.addService( RepositoryConnectorFactory.class,
266 ArchivaRepositoryConnectorFactory.class );
267 locator.addService( VersionResolver.class, DefaultVersionResolver.class );
268 locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
269 locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
270
271
272
273 return locator.getService( RepositorySystem.class );
274 }
275
276 private RepositorySystemSession newRepositorySystemSession( RepositorySystem system, String localRepoDir )
277 {
278 MavenRepositorySystemSession session = new MavenRepositorySystemSession();
279
280 DependencySelector depFilter = new AndDependencySelector( new ExclusionDependencySelector() );
281 session.setDependencySelector( depFilter );
282
283 session.setLocalRepositoryManager(
284 new SimpleLocalRepositoryManager( localRepoDir ) );
285
286 return session;
287 }
288
289
290 private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact )
291 throws RepositoryAdminException
292 {
293 for ( String repoId : repositoryIds )
294 {
295 ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
296
297 File repoDir = new File( managedRepository.getLocation() );
298 File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
299 projectArtifact.getBaseVersion(),
300 projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
301 + ".pom" );
302
303 if ( file.exists() )
304 {
305 return managedRepository;
306 }
307
308 if ( StringUtils.endsWith( projectArtifact.getBaseVersion(), VersionUtil.SNAPSHOT ) )
309 {
310 File metadataFile = new File( file.getParent(), MetadataTools.MAVEN_METADATA );
311 if ( metadataFile.exists() )
312 {
313 try
314 {
315 ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read( metadataFile );
316 int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
317 String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
318
319 String timeStampFileName =
320 new StringBuilder( projectArtifact.getArtifactId() ).append( '-' ).append(
321 StringUtils.remove( projectArtifact.getBaseVersion(),
322 "-" + VersionUtil.SNAPSHOT ) ).append( '-' ).append(
323 timeStamp ).append( '-' ).append( Integer.toString( buildNumber ) ).append(
324 ".pom" ).toString();
325 File timeStampFile = new File( file.getParent(), timeStampFileName );
326 log.debug( "try to find timestamped snapshot version file: {}", timeStampFile.getPath() );
327 if ( timeStampFile.exists() )
328 {
329 return managedRepository;
330 }
331 }
332 catch ( XMLException e )
333 {
334 log.warn( "skip fail to find timestamped snapshot pom: {}", e.getMessage() );
335 }
336 }
337 }
338 }
339 return null;
340 }
341
342 }