1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mortbay.jetty.plugin;
18
19
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
34 import org.apache.maven.artifact.resolver.ArtifactResolver;
35 import org.apache.maven.artifact.resolver.ResolutionListener;
36 import org.apache.maven.artifact.versioning.VersionRange;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.MavenProjectBuilder;
39 import org.apache.maven.project.ProjectBuildingException;
40 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
41 import org.apache.maven.project.artifact.MavenMetadataSource;
42 import org.mortbay.jetty.plugin.util.PluginLog;
43
44
45
46
47
48
49
50
51
52 public class RuntimeDependencyResolver
53 {
54 private ArtifactFactory artifactFactory;
55 private ArtifactResolver artifactResolver;
56 private ArtifactMetadataSource metadataSource;
57 private ArtifactRepository localRepository;
58 private List remoteRepositories;
59
60
61
62
63
64
65
66
67 class RuntimeResolutionListener implements ResolutionListener
68 {
69 public void testArtifact(Artifact arg0) { PluginLog.getLog().debug ("TESTING ARTIFACT "+arg0);}
70 public void startProcessChildren(Artifact arg0) {PluginLog.getLog().debug("STARTING CHILDREN "+arg0);}
71 public void endProcessChildren(Artifact arg0) {PluginLog.getLog().debug("ENDING CHILDREN "+arg0);}
72 public void includeArtifact(Artifact arg0) {PluginLog.getLog().debug("INCLUDE ARTIFACT "+arg0);}
73 public void omitForNearer(Artifact arg0, Artifact arg1) {PluginLog.getLog().debug("OMITTING "+arg0+" for NEARER "+arg1);}
74 public void updateScope(Artifact arg0, String arg1) {PluginLog.getLog().debug("UPDATE of SCOPE "+arg0+ "="+arg1);}
75 public void manageArtifact(Artifact arg0, Artifact arg1) {PluginLog.getLog().debug("MANAGE ARTIFACT "+arg0+" and "+arg1); }
76 public void omitForCycle(Artifact arg0) {PluginLog.getLog().debug("OMIT FOR CYCLE "+arg0);}
77 public void updateScopeCurrentPom(Artifact arg0, String arg1) {PluginLog.getLog().debug("UPDATE SCOPE CURRENT POM "+arg0+"="+arg1);}
78 public void selectVersionFromRange(Artifact arg0) {PluginLog.getLog().debug("SELECT VERSION FROM RANGE "+arg0);}
79 public void restrictRange(Artifact arg0, Artifact arg1, VersionRange arg2) {PluginLog.getLog().debug("RESTRICT RANGE "+arg0+" "+arg1+" range="+arg2);}
80
81 }
82
83
84 public RuntimeDependencyResolver (ArtifactFactory artifactFactory, ArtifactResolver artifactResolver,
85 ArtifactMetadataSource metadataSource, ArtifactRepository localRepository, List remoteRepositories)
86 {
87 this.artifactFactory = artifactFactory;
88 this.artifactResolver = artifactResolver;
89 this.metadataSource = metadataSource;
90 this.localRepository = localRepository;
91 this.remoteRepositories = new ArrayList(remoteRepositories);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public Set transitivelyResolvePomDependencies (MavenProjectBuilder projectBuilder, String groupId, String artifactId, String versionId, boolean resolveProjectArtifact)
112 throws MalformedURLException, ProjectBuildingException, InvalidDependencyVersionException, ArtifactResolutionException, ArtifactNotFoundException
113 {
114
115 Artifact pomArtifact = getPomArtifact(groupId, artifactId, versionId);
116 MavenProject project = loadPomAsProject(projectBuilder, pomArtifact);
117 List dependencies = project.getDependencies();
118
119
120 Set dependencyArtifacts = MavenMetadataSource.createArtifacts( artifactFactory, dependencies, null, null, null );
121 dependencyArtifacts.add(project.getArtifact());
122
123 List listeners = Collections.EMPTY_LIST;
124
125 if (PluginLog.getLog().isDebugEnabled())
126 {
127 listeners = new ArrayList();
128 listeners.add(new RuntimeResolutionListener());
129 }
130
131 ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, pomArtifact,
132 Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, null, listeners);
133
134 Set artifacts = result.getArtifacts();
135
136 if (PluginLog.getLog().isDebugEnabled())
137 {
138 PluginLog.getLog().debug("RESOLVED "+artifacts.size()+" ARTIFACTS");
139 Iterator itor = artifacts.iterator();
140 while (itor.hasNext())
141 {
142 Artifact a = (Artifact)itor.next();
143 PluginLog.getLog().debug(a.getFile().toURL().toString());
144 }
145 }
146 return artifacts;
147 }
148
149
150
151 public MavenProject loadPomAsProject (MavenProjectBuilder projectBuilder, Artifact pomArtifact)
152 throws ProjectBuildingException
153 {
154 return projectBuilder.buildFromRepository(pomArtifact, remoteRepositories,localRepository);
155 }
156
157
158 public Artifact getArtifact (String groupId, String artifactId, String versionId, String type)
159 {
160 return this.artifactFactory.createBuildArtifact(groupId, artifactId, versionId, type);
161 }
162
163
164 public Artifact getPomArtifact (String groupId, String artifactId, String versionId)
165 {
166 return this.artifactFactory.createBuildArtifact(groupId, artifactId, versionId, "pom");
167 }
168
169 public void removeDependency (Set artifacts, String groupId, String artifactId, String versionId, String type)
170 {
171 if ((artifacts == null) || artifacts.isEmpty())
172 return;
173
174 Iterator itor = artifacts.iterator();
175 while (itor.hasNext())
176 {
177 Artifact a = (Artifact)itor.next();
178 if (a.getGroupId().equals(groupId) && a.getArtifactId().equals(artifactId) && a.getType().equals(type))
179 {
180
181 if (versionId == null)
182 itor.remove();
183 else if (a.getVersion().equals(versionId))
184 itor.remove();
185 }
186 }
187 }
188
189 public void addDependency (Set artifacts, String groupId, String artifactId, String versionId, String type)
190 throws ArtifactResolutionException, ArtifactNotFoundException
191 {
192 Artifact a = getArtifact(groupId, artifactId, versionId, type);
193 artifactResolver.resolve(a, remoteRepositories, localRepository);
194 artifacts.add(a);
195 }
196
197 }