001: //========================================================================
002: //$Id: RuntimeDependencyResolver.java 397 2006-03-23 18:44:41Z janb $
003: //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.plugin;
017:
018: import java.net.MalformedURLException;
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: import org.apache.maven.artifact.Artifact;
026: import org.apache.maven.artifact.factory.ArtifactFactory;
027: import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
028: import org.apache.maven.artifact.repository.ArtifactRepository;
029: import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
030: import org.apache.maven.artifact.resolver.ArtifactResolutionException;
031: import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
032: import org.apache.maven.artifact.resolver.ArtifactResolver;
033: import org.apache.maven.artifact.resolver.ResolutionListener;
034: import org.apache.maven.artifact.versioning.VersionRange;
035: import org.apache.maven.project.MavenProject;
036: import org.apache.maven.project.MavenProjectBuilder;
037: import org.apache.maven.project.ProjectBuildingException;
038: import org.apache.maven.project.artifact.InvalidDependencyVersionException;
039: import org.apache.maven.project.artifact.MavenMetadataSource;
040: import org.mortbay.jetty.plugin.util.PluginLog;
041:
042: /**
043: * RuntimeDependencyResolver
044: *
045: * This class is able to pull down a remote pom, find all of it's
046: * dependencies and transitively resolve them.
047: *
048: *
049: */
050: public class RuntimeDependencyResolver {
051: private ArtifactFactory artifactFactory;
052: private ArtifactResolver artifactResolver;
053: private ArtifactMetadataSource metadataSource;
054: private ArtifactRepository localRepository;
055: private List remoteRepositories;
056:
057: /**
058: * RuntimeResolutionListener
059: *
060: * Just for debug printing of transitive resolution steps
061: *
062: */
063: class RuntimeResolutionListener implements ResolutionListener {
064: public void testArtifact(Artifact arg0) {
065: PluginLog.getLog().debug("TESTING ARTIFACT " + arg0);
066: }
067:
068: public void startProcessChildren(Artifact arg0) {
069: PluginLog.getLog().debug("STARTING CHILDREN " + arg0);
070: }
071:
072: public void endProcessChildren(Artifact arg0) {
073: PluginLog.getLog().debug("ENDING CHILDREN " + arg0);
074: }
075:
076: public void includeArtifact(Artifact arg0) {
077: PluginLog.getLog().debug("INCLUDE ARTIFACT " + arg0);
078: }
079:
080: public void omitForNearer(Artifact arg0, Artifact arg1) {
081: PluginLog.getLog().debug(
082: "OMITTING " + arg0 + " for NEARER " + arg1);
083: }
084:
085: public void updateScope(Artifact arg0, String arg1) {
086: PluginLog.getLog().debug(
087: "UPDATE of SCOPE " + arg0 + "=" + arg1);
088: }
089:
090: public void manageArtifact(Artifact arg0, Artifact arg1) {
091: PluginLog.getLog().debug(
092: "MANAGE ARTIFACT " + arg0 + " and " + arg1);
093: }
094:
095: public void omitForCycle(Artifact arg0) {
096: PluginLog.getLog().debug("OMIT FOR CYCLE " + arg0);
097: }
098:
099: public void updateScopeCurrentPom(Artifact arg0, String arg1) {
100: PluginLog.getLog().debug(
101: "UPDATE SCOPE CURRENT POM " + arg0 + "=" + arg1);
102: }
103:
104: public void selectVersionFromRange(Artifact arg0) {
105: PluginLog.getLog().debug(
106: "SELECT VERSION FROM RANGE " + arg0);
107: }
108:
109: public void restrictRange(Artifact arg0, Artifact arg1,
110: VersionRange arg2) {
111: PluginLog.getLog().debug(
112: "RESTRICT RANGE " + arg0 + " " + arg1 + " range="
113: + arg2);
114: }
115:
116: }
117:
118: public RuntimeDependencyResolver(ArtifactFactory artifactFactory,
119: ArtifactResolver artifactResolver,
120: ArtifactMetadataSource metadataSource,
121: ArtifactRepository localRepository, List remoteRepositories) {
122: this .artifactFactory = artifactFactory;
123: this .artifactResolver = artifactResolver;
124: this .metadataSource = metadataSource;
125: this .localRepository = localRepository;
126: this .remoteRepositories = new ArrayList(remoteRepositories);
127: }
128:
129: /**
130: * Download (if necessary) a pom, and load it as a MavenProject, transitively resolving any
131: * dependencies therein.
132: *
133: * @param projectBuilder
134: * @param groupId
135: * @param artifactId
136: * @param versionId
137: * @return a Set of Artifacts representing the transitively resolved dependencies.
138: *
139: * @throws MalformedURLException
140: * @throws ProjectBuildingException
141: * @throws InvalidDependencyVersionException
142: * @throws ArtifactResolutionException
143: * @throws ArtifactNotFoundException
144: */
145: public Set transitivelyResolvePomDependencies(
146: MavenProjectBuilder projectBuilder, String groupId,
147: String artifactId, String versionId,
148: boolean resolveProjectArtifact)
149: throws MalformedURLException, ProjectBuildingException,
150: InvalidDependencyVersionException,
151: ArtifactResolutionException, ArtifactNotFoundException {
152:
153: Artifact pomArtifact = getPomArtifact(groupId, artifactId,
154: versionId);
155: MavenProject project = loadPomAsProject(projectBuilder,
156: pomArtifact);
157: List dependencies = project.getDependencies();
158:
159: Set dependencyArtifacts = MavenMetadataSource.createArtifacts(
160: artifactFactory, dependencies, null, null, null);
161: dependencyArtifacts.add(project.getArtifact());
162:
163: List listeners = Collections.EMPTY_LIST;
164:
165: if (PluginLog.getLog().isDebugEnabled()) {
166: listeners = new ArrayList();
167: listeners.add(new RuntimeResolutionListener());
168: }
169:
170: ArtifactResolutionResult result = artifactResolver
171: .resolveTransitively(dependencyArtifacts, pomArtifact,
172: Collections.EMPTY_MAP, localRepository,
173: remoteRepositories, metadataSource, null,
174: listeners);
175:
176: Set artifacts = result.getArtifacts();
177:
178: if (PluginLog.getLog().isDebugEnabled()) {
179: PluginLog.getLog().debug(
180: "RESOLVED " + artifacts.size() + " ARTIFACTS");
181: Iterator itor = artifacts.iterator();
182: while (itor.hasNext()) {
183: Artifact a = (Artifact) itor.next();
184: PluginLog.getLog()
185: .debug(a.getFile().toURL().toString());
186: }
187: }
188: return artifacts;
189: }
190:
191: public MavenProject loadPomAsProject(
192: MavenProjectBuilder projectBuilder, Artifact pomArtifact)
193: throws ProjectBuildingException {
194: return projectBuilder.buildFromRepository(pomArtifact,
195: remoteRepositories, localRepository);
196: }
197:
198: public Artifact getArtifact(String groupId, String artifactId,
199: String versionId, String type) {
200: return this .artifactFactory.createBuildArtifact(groupId,
201: artifactId, versionId, type);
202: }
203:
204: public Artifact getPomArtifact(String groupId, String artifactId,
205: String versionId) {
206: return this .artifactFactory.createBuildArtifact(groupId,
207: artifactId, versionId, "pom");
208: }
209:
210: public void removeDependency(Set artifacts, String groupId,
211: String artifactId, String versionId, String type) {
212: if ((artifacts == null) || artifacts.isEmpty())
213: return;
214:
215: Iterator itor = artifacts.iterator();
216: while (itor.hasNext()) {
217: Artifact a = (Artifact) itor.next();
218: if (a.getGroupId().equals(groupId)
219: && a.getArtifactId().equals(artifactId)
220: && a.getType().equals(type)) {
221: //remove if the versions match, or there was no version specified
222: if (versionId == null)
223: itor.remove();
224: else if (a.getVersion().equals(versionId))
225: itor.remove();
226: }
227: }
228: }
229:
230: public void addDependency(Set artifacts, String groupId,
231: String artifactId, String versionId, String type)
232: throws ArtifactResolutionException,
233: ArtifactNotFoundException {
234: Artifact a = getArtifact(groupId, artifactId, versionId, type);
235: artifactResolver
236: .resolve(a, remoteRepositories, localRepository);
237: artifacts.add(a);
238: }
239:
240: }
|