summaryrefslogtreecommitdiffstats
path: root/archiva-modules/plugins/maven2-repository/src/main
diff options
context:
space:
mode:
authorOlivier Lamy <olamy@apache.org>2012-08-02 13:19:23 +0000
committerOlivier Lamy <olamy@apache.org>2012-08-02 13:19:23 +0000
commitc31afc5c1c56430d97517d8f99881d10914a7eb8 (patch)
tree00d7b57cdfcaff2d42205d88be98602929e51248 /archiva-modules/plugins/maven2-repository/src/main
parent0e22d86115c97ce7069162fafc8e934f081e8339 (diff)
downloadarchiva-c31afc5c1c56430d97517d8f99881d10914a7eb8.tar.gz
archiva-c31afc5c1c56430d97517d8f99881d10914a7eb8.zip
add a tree builder with maven3/aether api
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1368456 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-modules/plugins/maven2-repository/src/main')
-rw-r--r--archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java468
1 files changed, 468 insertions, 0 deletions
diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java
new file mode 100644
index 000000000..30822d244
--- /dev/null
+++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java
@@ -0,0 +1,468 @@
+package org.apache.archiva.dependency.tree.maven2;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import org.apache.archiva.admin.model.RepositoryAdminException;
+import org.apache.archiva.admin.model.beans.ManagedRepository;
+import org.apache.archiva.admin.model.beans.NetworkProxy;
+import org.apache.archiva.admin.model.beans.ProxyConnector;
+import org.apache.archiva.admin.model.beans.RemoteRepository;
+import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
+import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
+import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
+import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
+import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
+import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
+import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
+import org.apache.archiva.metadata.repository.storage.maven2.RepositoryModelResolver;
+import org.apache.archiva.proxy.common.WagonFactory;
+import org.apache.maven.RepositoryUtils;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.model.DependencyManagement;
+import org.apache.maven.model.Exclusion;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.building.DefaultModelBuilderFactory;
+import org.apache.maven.model.building.DefaultModelBuildingRequest;
+import org.apache.maven.model.building.ModelBuilder;
+import org.apache.maven.model.building.ModelBuildingException;
+import org.apache.maven.model.building.ModelBuildingRequest;
+import org.apache.maven.model.resolution.UnresolvableModelException;
+import org.apache.maven.project.DefaultDependencyResolutionRequest;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.DependencyResolutionException;
+import org.apache.maven.project.DependencyResolutionResult;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonatype.aether.RepositorySystem;
+import org.sonatype.aether.RepositorySystemSession;
+import org.sonatype.aether.RequestTrace;
+import org.sonatype.aether.artifact.ArtifactType;
+import org.sonatype.aether.artifact.ArtifactTypeRegistry;
+import org.sonatype.aether.collection.CollectRequest;
+import org.sonatype.aether.collection.DependencyCollectionException;
+import org.sonatype.aether.graph.Dependency;
+import org.sonatype.aether.graph.DependencyFilter;
+import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
+import org.sonatype.aether.resolution.DependencyRequest;
+import org.sonatype.aether.util.DefaultRepositorySystemSession;
+import org.sonatype.aether.util.DefaultRequestTrace;
+import org.sonatype.aether.util.artifact.ArtifacIdUtils;
+import org.sonatype.aether.util.artifact.JavaScopes;
+import org.sonatype.aether.version.VersionConstraint;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.inject.Named;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Olivier Lamy
+ */
+@Service( "dependencyTreeBuilder#maven3" )
+public class Maven3DependencyTreeBuilder
+{
+ private Logger log = LoggerFactory.getLogger( getClass() );
+
+ @Inject
+ private PlexusSisuBridge plexusSisuBridge;
+
+ @Inject
+ @Named( value = "repositoryPathTranslator#maven2" )
+ private RepositoryPathTranslator pathTranslator;
+
+ @Inject
+ private WagonFactory wagonFactory;
+
+ @Inject
+ private ManagedRepositoryAdmin managedRepositoryAdmin;
+
+ @Inject
+ private ProxyConnectorAdmin proxyConnectorAdmin;
+
+ @Inject
+ private NetworkProxyAdmin networkProxyAdmin;
+
+ @Inject
+ private RemoteRepositoryAdmin remoteRepositoryAdmin;
+
+ private ArtifactFactory factory;
+
+ private ModelBuilder builder;
+
+
+ private RepositorySystem repoSystem;
+
+ @PostConstruct
+ public void initialize()
+ throws PlexusSisuBridgeException
+ {
+ factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" );
+
+ repoSystem = plexusSisuBridge.lookup( RepositorySystem.class );
+ DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory();
+ builder = defaultModelBuilderFactory.newInstance();
+ }
+
+ public DependencyResolutionResult buildDependencyTree( List<String> repositoryIds, String groupId,
+ String artifactId, String version )
+ throws Exception
+ {
+ Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
+ ManagedRepository repository = null;
+ try
+ {
+ repository = findArtifactInRepositories( repositoryIds, projectArtifact );
+ }
+ catch ( RepositoryAdminException e )
+ {
+ // FIXME better exception
+ throw new Exception( "Cannot build project dependency tree " + e.getMessage(), e );
+ }
+
+ if ( repository == null )
+ {
+ // metadata could not be resolved
+ return new DefaultDependencyResolutionResult();
+ }
+
+ // MRM-1411
+ // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
+ // handled there. It doesn't cache anything locally!
+ List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
+ Map<String, NetworkProxy> networkProxies = new HashMap<String, NetworkProxy>();
+
+ Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
+ List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( repository.getId() );
+ if ( proxyConnectors != null )
+ {
+ for ( ProxyConnector proxyConnector : proxyConnectors )
+ {
+ remoteRepositories.add( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) );
+
+ NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
+
+ if ( networkProxyConfig != null )
+ {
+ // key/value: remote repo ID/proxy info
+ networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
+ }
+ }
+ }
+
+ Model model = buildProject(
+ new RepositoryModelResolver( repository, pathTranslator, wagonFactory, remoteRepositories, networkProxies,
+ repository ), groupId, artifactId, version );
+
+ MavenProject project = new MavenProject( model );
+
+ DefaultRepositorySystemSession repositorySystemSession = new DefaultRepositorySystemSession();
+
+ // FIXME take care of relative path for getLocation
+ repositorySystemSession.setLocalRepositoryManager(
+ new SimpleLocalRepositoryManager( new File( repository.getLocation() ) ) );
+
+ DefaultProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest();
+
+ project.setProjectBuildingRequest( projectBuildingRequest );
+
+ projectBuildingRequest.setRepositorySession( repositorySystemSession );
+
+ DefaultDependencyResolutionRequest request =
+ new DefaultDependencyResolutionRequest( project, projectBuildingRequest.getRepositorySession() );
+
+ //DependencyFilter dependencyFilter
+ //request.setResolutionFilter( )
+
+ //DependencyResolutionResult result = projectDependenciesResolver.resolve( request );
+
+ //DependencyNode dependencyNode = buildDependencyNode( null, result.getDependencyGraph(), projectArtifact, null );
+ /*DependencyNode dependencyNode = dependencyGraphBuilder.buildDependencyGraph( project, new ArtifactFilter()
+ {
+ public boolean include( Artifact artifact )
+ {
+ return true;
+ }
+ } );*/
+
+ DependencyResolutionResult resolutionResult = resolve( request );
+
+ log.debug( "dependency graph build" );
+
+ return resolutionResult;
+ }
+
+ private DependencyResolutionResult resolve( DefaultDependencyResolutionRequest request )
+ throws DependencyResolutionException
+ {
+
+ RequestTrace trace = DefaultRequestTrace.newChild( null, request );
+
+ DefaultDependencyResolutionResult result = new DefaultDependencyResolutionResult();
+
+ MavenProject project = request.getMavenProject();
+ RepositorySystemSession session = request.getRepositorySession();
+ DependencyFilter filter = request.getResolutionFilter();
+
+ ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
+
+ CollectRequest collect = new CollectRequest();
+ collect.setRequestContext( "project" );
+ collect.setRepositories( project.getRemoteProjectRepositories() );
+
+ if ( project.getDependencyArtifacts() == null )
+ {
+ for ( org.apache.maven.model.Dependency dependency : project.getDependencies() )
+ {
+ if ( StringUtils.isEmpty( dependency.getGroupId() ) || StringUtils.isEmpty( dependency.getArtifactId() )
+ || StringUtils.isEmpty( dependency.getVersion() ) )
+ {
+ // guard against case where best-effort resolution for invalid models is requested
+ continue;
+ }
+ collect.addDependency( RepositoryUtils.toDependency( dependency, stereotypes ) );
+ }
+ }
+ else
+ {
+ Map<String, org.apache.maven.model.Dependency> dependencies =
+ new HashMap<String, org.apache.maven.model.Dependency>();
+ for ( org.apache.maven.model.Dependency dependency : project.getDependencies() )
+ {
+ String classifier = dependency.getClassifier();
+ if ( classifier == null )
+ {
+ ArtifactType type = stereotypes.get( dependency.getType() );
+ if ( type != null )
+ {
+ classifier = type.getClassifier();
+ }
+ }
+ String key = ArtifacIdUtils.toVersionlessId( dependency.getGroupId(), dependency.getArtifactId(),
+ dependency.getType(), classifier );
+ dependencies.put( key, dependency );
+ }
+ for ( Artifact artifact : project.getDependencyArtifacts() )
+ {
+ String key = artifact.getDependencyConflictId();
+ org.apache.maven.model.Dependency dependency = dependencies.get( key );
+ Collection<Exclusion> exclusions = dependency != null ? dependency.getExclusions() : null;
+ org.sonatype.aether.graph.Dependency dep = RepositoryUtils.toDependency( artifact, exclusions );
+ if ( !JavaScopes.SYSTEM.equals( dep.getScope() ) && dep.getArtifact().getFile() != null )
+ {
+ // enable re-resolution
+ org.sonatype.aether.artifact.Artifact art = dep.getArtifact();
+ art = art.setFile( null ).setVersion( art.getBaseVersion() );
+ dep = dep.setArtifact( art );
+ }
+ collect.addDependency( dep );
+ }
+ }
+
+ DependencyManagement depMngt = project.getDependencyManagement();
+ if ( depMngt != null )
+ {
+ for ( org.apache.maven.model.Dependency dependency : depMngt.getDependencies() )
+ {
+ collect.addManagedDependency( RepositoryUtils.toDependency( dependency, stereotypes ) );
+ }
+ }
+
+ DependencyRequest depRequest = new DependencyRequest( collect, filter );
+ depRequest.setTrace( trace );
+
+ org.sonatype.aether.graph.DependencyNode node;
+ try
+ {
+ collect.setTrace( DefaultRequestTrace.newChild( trace, depRequest ) );
+ node = repoSystem.collectDependencies( session, collect ).getRoot();
+ result.setDependencyGraph( node );
+ }
+ catch ( DependencyCollectionException e )
+ {
+ result.setDependencyGraph( e.getResult().getRoot() );
+ result.setCollectionErrors( e.getResult().getExceptions() );
+
+ throw new DependencyResolutionException( result,
+ "Could not resolve dependencies for project " + project.getId()
+ + ": " + e.getMessage(), e );
+ }
+
+ depRequest.setRoot( node );
+
+ return result;
+ }
+
+ private String getVersionSelectedFromRange( VersionConstraint constraint )
+ {
+ if ( ( constraint == null ) || ( constraint.getVersion() != null ) )
+ {
+ return null;
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for ( org.sonatype.aether.version.VersionRange range : constraint.getRanges() )
+ {
+ if ( sb.length() > 0 )
+ {
+ sb.append( ',' );
+ }
+ sb.append( range );
+ }
+
+ return sb.toString();
+ }
+
+ private Artifact getDependencyArtifact( Dependency dep )
+ {
+ org.sonatype.aether.artifact.Artifact artifact = dep.getArtifact();
+
+ return factory.createDependencyArtifact( artifact.getGroupId(), artifact.getArtifactId(),
+ VersionRange.createFromVersion( artifact.getVersion() ),
+ artifact.getExtension(), artifact.getClassifier(), dep.getScope(),
+ dep.isOptional() );
+ }
+
+ private Model buildProject( RepositoryModelResolver modelResolver, String groupId, String artifactId,
+ String version )
+ throws ModelBuildingException, UnresolvableModelException
+ {
+ DefaultModelBuildingRequest req = new DefaultModelBuildingRequest();
+ req.setProcessPlugins( false );
+ req.setModelSource( modelResolver.resolveModel( groupId, artifactId, version ) );
+ req.setModelResolver( modelResolver );
+ req.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
+ //MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm
+ req.setSystemProperties( System.getProperties() );
+
+ return builder.build( req ).getEffectiveModel();
+ }
+
+ private ManagedRepository findArtifactInRepositories( List<String> repositoryIds, Artifact projectArtifact )
+ throws RepositoryAdminException
+ {
+ for ( String repoId : repositoryIds )
+ {
+ ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId );
+
+ File repoDir = new File( managedRepository.getLocation() );
+ File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(),
+ projectArtifact.getBaseVersion(),
+ projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion()
+ + ".pom" );
+
+ if ( file.exists() )
+ {
+ return managedRepository;
+ }
+ }
+ return null;
+ }
+
+ public static class DefaultDependencyResolutionResult
+ implements DependencyResolutionResult
+ {
+
+ private org.sonatype.aether.graph.DependencyNode root;
+
+ private List<Dependency> dependencies = new ArrayList<Dependency>();
+
+ private List<Dependency> resolvedDependencies = new ArrayList<Dependency>();
+
+ private List<Dependency> unresolvedDependencies = new ArrayList<Dependency>();
+
+ private List<Exception> collectionErrors = new ArrayList<Exception>();
+
+ private Map<Dependency, List<Exception>> resolutionErrors = new IdentityHashMap<Dependency, List<Exception>>();
+
+ public org.sonatype.aether.graph.DependencyNode getDependencyGraph()
+ {
+ return root;
+ }
+
+ public void setDependencyGraph( org.sonatype.aether.graph.DependencyNode root )
+ {
+ this.root = root;
+ }
+
+ public List<Dependency> getDependencies()
+ {
+ return dependencies;
+ }
+
+ public List<Dependency> getResolvedDependencies()
+ {
+ return resolvedDependencies;
+ }
+
+ public void addResolvedDependency( Dependency dependency )
+ {
+ dependencies.add( dependency );
+ resolvedDependencies.add( dependency );
+ }
+
+ public List<Dependency> getUnresolvedDependencies()
+ {
+ return unresolvedDependencies;
+ }
+
+ public List<Exception> getCollectionErrors()
+ {
+ return collectionErrors;
+ }
+
+ public void setCollectionErrors( List<Exception> exceptions )
+ {
+ if ( exceptions != null )
+ {
+ this.collectionErrors = exceptions;
+ }
+ else
+ {
+ this.collectionErrors = new ArrayList<Exception>();
+ }
+ }
+
+ public List<Exception> getResolutionErrors( Dependency dependency )
+ {
+ List<Exception> errors = resolutionErrors.get( dependency );
+ return ( errors != null ) ? errors : Collections.<Exception>emptyList();
+ }
+
+ public void setResolutionErrors( Dependency dependency, List<Exception> errors )
+ {
+ dependencies.add( dependency );
+ unresolvedDependencies.add( dependency );
+ resolutionErrors.put( dependency, errors );
+ }
+
+ }
+}