001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.genesis.dependency; 021 022 import java.util.Map; 023 import java.util.Collections; 024 import java.util.HashMap; 025 import java.util.Iterator; 026 027 import org.apache.maven.artifact.metadata.ArtifactMetadataSource; 028 import org.apache.maven.artifact.resolver.ArtifactCollector; 029 import org.apache.maven.artifact.resolver.ArtifactResolver; 030 import org.apache.maven.artifact.resolver.ArtifactResolutionException; 031 import org.apache.maven.artifact.repository.ArtifactRepository; 032 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; 033 import org.apache.maven.artifact.factory.ArtifactFactory; 034 import org.apache.maven.artifact.versioning.VersionRange; 035 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 036 import org.apache.maven.artifact.Artifact; 037 import org.apache.maven.project.MavenProject; 038 import org.apache.maven.project.ProjectBuildingException; 039 import org.apache.maven.project.artifact.InvalidDependencyVersionException; 040 import org.apache.maven.model.DependencyManagement; 041 import org.apache.maven.model.Dependency; 042 043 import org.codehaus.plexus.PlexusContainer; 044 import org.codehaus.plexus.PlexusConstants; 045 import org.codehaus.plexus.context.Context; 046 import org.codehaus.plexus.context.ContextException; 047 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; 048 049 /** 050 * ??? 051 * 052 * @version $Rev: 454266 $ $Date: 2006-10-08 20:20:03 -0700 (Sun, 08 Oct 2006) $ 053 */ 054 public class DependencyHelper 055 implements Contextualizable 056 { 057 private ArtifactRepositoryFactory artifactRepositoryFactory = null; 058 059 private ArtifactMetadataSource artifactMetadataSource = null; 060 061 private ArtifactCollector artifactCollector = null; 062 063 private ArtifactFactory artifactFactory = null; 064 065 private ArtifactResolver artifactResolver = null; 066 067 private PlexusContainer container; 068 069 private ArtifactRepository repository; 070 071 // 072 // TODO: Figure out how to get ${localRepository} injected so we don't need it passed in. 073 // 074 075 public void setArtifactRepository(final ArtifactRepository repository) { 076 this.repository = repository; 077 } 078 079 private ArtifactRepository getArtifactRepository() { 080 if (repository == null) { 081 throw new IllegalStateException("Not initialized; missing ArtifactRepository"); 082 } 083 return repository; 084 } 085 086 public DependencyTree getDependencies(final MavenProject project) 087 throws ProjectBuildingException, InvalidDependencyVersionException, ArtifactResolutionException 088 { 089 assert project != null; 090 091 Map managedVersions = getManagedVersionMap(project, artifactFactory); 092 DependencyResolutionListener listener = new DependencyResolutionListener(); 093 094 if (project.getDependencyArtifacts() == null) { 095 project.setDependencyArtifacts(project.createArtifacts(artifactFactory, null, null)); 096 } 097 098 artifactCollector.collect( 099 project.getDependencyArtifacts(), 100 project.getArtifact(), 101 managedVersions, 102 getArtifactRepository(), 103 project.getRemoteArtifactRepositories(), 104 artifactMetadataSource, 105 null, 106 Collections.singletonList(listener)); 107 108 return listener.getDependencyTree(); 109 } 110 111 public static Map getManagedVersionMap(final MavenProject project, final ArtifactFactory factory) throws ProjectBuildingException { 112 assert project != null; 113 assert factory != null; 114 115 DependencyManagement dependencyManagement = project.getDependencyManagement(); 116 Map managedVersionMap; 117 118 if (dependencyManagement != null && dependencyManagement.getDependencies() != null) { 119 managedVersionMap = new HashMap(); 120 Iterator iter = dependencyManagement.getDependencies().iterator(); 121 122 while (iter.hasNext()) { 123 Dependency d = (Dependency) iter.next(); 124 125 try { 126 VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion()); 127 Artifact artifact = factory.createDependencyArtifact( 128 d.getGroupId(), 129 d.getArtifactId(), 130 versionRange, 131 d.getType(), 132 d.getClassifier(), 133 d.getScope()); 134 managedVersionMap.put(d.getManagementKey(), artifact); 135 } 136 catch (InvalidVersionSpecificationException e) { 137 throw new ProjectBuildingException(project.getId(), 138 "Unable to parse version '" + d.getVersion() + 139 "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e); 140 } 141 } 142 } 143 else { 144 managedVersionMap = Collections.EMPTY_MAP; 145 } 146 147 return managedVersionMap; 148 } 149 150 // 151 // Contextualizable 152 // 153 154 public void contextualize(final Context context) throws ContextException { 155 container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY); 156 } 157 158 // 159 // Component Access 160 // 161 162 public ArtifactResolver getArtifactResolver() { 163 return artifactResolver; 164 } 165 166 public ArtifactRepositoryFactory getArtifactRepositoryFactory() { 167 return artifactRepositoryFactory; 168 } 169 170 public ArtifactMetadataSource getArtifactMetadataSource() { 171 return artifactMetadataSource; 172 } 173 174 public ArtifactCollector getArtifactCollector() { 175 return artifactCollector; 176 } 177 178 public ArtifactFactory getArtifactFactory() { 179 return artifactFactory; 180 } 181 }