001: package org.apache.maven.project;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.maven.artifact.Artifact;
023: import org.apache.maven.artifact.InvalidRepositoryException;
024: import org.apache.maven.artifact.factory.ArtifactFactory;
025: import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
026: import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
027: import org.apache.maven.artifact.metadata.ResolutionGroup;
028: import org.apache.maven.artifact.repository.ArtifactRepository;
029: import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
030: import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
031: import org.apache.maven.artifact.resolver.ArtifactResolutionException;
032: import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
033: import org.apache.maven.artifact.resolver.DefaultArtifactResolver;
034: import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
035: import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
036: import org.apache.maven.artifact.versioning.VersionRange;
037: import org.apache.maven.model.Dependency;
038: import org.apache.maven.model.Model;
039: import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
040: import org.codehaus.plexus.PlexusConstants;
041: import org.codehaus.plexus.PlexusContainer;
042: import org.codehaus.plexus.context.Context;
043: import org.codehaus.plexus.context.ContextException;
044: import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
045: import org.codehaus.plexus.util.IOUtil;
046: import org.codehaus.plexus.util.StringUtils;
047: import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
048:
049: import java.io.File;
050: import java.io.IOException;
051: import java.io.InputStreamReader;
052: import java.util.HashSet;
053: import java.util.Iterator;
054: import java.util.List;
055: import java.util.Set;
056:
057: public class TestArtifactResolver extends DefaultArtifactResolver
058: implements Contextualizable {
059: public static final String ROLE = TestArtifactResolver.class
060: .getName();
061:
062: private ArtifactRepositoryFactory repositoryFactory;
063:
064: private PlexusContainer container;
065:
066: static class Source implements ArtifactMetadataSource {
067: private ArtifactFactory artifactFactory;
068:
069: private final ArtifactRepositoryFactory repositoryFactory;
070:
071: private final PlexusContainer container;
072:
073: public Source(ArtifactFactory artifactFactory,
074: ArtifactRepositoryFactory repositoryFactory,
075: PlexusContainer container) {
076: this .artifactFactory = artifactFactory;
077: this .repositoryFactory = repositoryFactory;
078: this .container = container;
079: }
080:
081: public ResolutionGroup retrieve(Artifact artifact,
082: ArtifactRepository localRepository,
083: List remoteRepositories)
084: throws ArtifactMetadataRetrievalException {
085: Model model = null;
086: InputStreamReader r = null;
087: try {
088: String scope = artifact.getArtifactId().substring(
089: "scope-".length());
090: if ("maven-test".equals(artifact.getGroupId())) {
091: String name = "/projects/scope/transitive-" + scope
092: + "-dep.xml";
093: r = new InputStreamReader(getClass()
094: .getResourceAsStream(name));
095: MavenXpp3Reader reader = new MavenXpp3Reader();
096: model = reader.read(r);
097: } else {
098: model = new Model();
099: }
100: model.setGroupId(artifact.getGroupId());
101: model.setArtifactId(artifact.getArtifactId());
102: } catch (IOException e) {
103: throw new ArtifactMetadataRetrievalException(e);
104: } catch (XmlPullParserException e) {
105: throw new ArtifactMetadataRetrievalException(e);
106: } finally {
107: IOUtil.close(r);
108: }
109:
110: Set artifacts;
111: try {
112: artifacts = createArtifacts(model.getDependencies(),
113: artifact.getScope());
114: } catch (InvalidVersionSpecificationException e) {
115: throw new ArtifactMetadataRetrievalException(e);
116: }
117:
118: List artifactRepositories;
119: try {
120: artifactRepositories = ProjectUtils
121: .buildArtifactRepositories(model
122: .getRepositories(), repositoryFactory,
123: container);
124: } catch (InvalidRepositoryException e) {
125: throw new ArtifactMetadataRetrievalException(e);
126: }
127:
128: return new ResolutionGroup(artifact, artifacts,
129: artifactRepositories);
130: }
131:
132: public List retrieveAvailableVersions(Artifact artifact,
133: ArtifactRepository localRepository,
134: List remoteRepositories) {
135: throw new UnsupportedOperationException(
136: "Cannot get available versions in this test case");
137: }
138:
139: protected Set createArtifacts(List dependencies,
140: String inheritedScope)
141: throws InvalidVersionSpecificationException {
142: Set projectArtifacts = new HashSet();
143:
144: for (Iterator i = dependencies.iterator(); i.hasNext();) {
145: Dependency d = (Dependency) i.next();
146:
147: String scope = d.getScope();
148:
149: if (StringUtils.isEmpty(scope)) {
150: scope = Artifact.SCOPE_COMPILE;
151:
152: d.setScope(scope);
153: }
154:
155: VersionRange versionRange = VersionRange
156: .createFromVersionSpec(d.getVersion());
157: Artifact artifact = artifactFactory
158: .createDependencyArtifact(d.getGroupId(), d
159: .getArtifactId(), versionRange, d
160: .getType(), d.getClassifier(), scope,
161: inheritedScope);
162: if (artifact != null) {
163: projectArtifacts.add(artifact);
164: }
165: }
166:
167: return projectArtifacts;
168: }
169: }
170:
171: public Source source() {
172: return new Source(artifactFactory, repositoryFactory, container);
173: }
174:
175: /**
176: * @noinspection RefusedBequest
177: */
178: public void resolve(Artifact artifact, List remoteRepositories,
179: ArtifactRepository localRepository)
180: throws ArtifactResolutionException {
181: artifact.setFile(new File("dummy"));
182: }
183:
184: public ArtifactResolutionResult resolveTransitively(Set artifacts,
185: Artifact originatingArtifact,
186: ArtifactRepository localRepository,
187: List remoteRepositories, ArtifactMetadataSource source,
188: ArtifactFilter filter) throws ArtifactResolutionException,
189: ArtifactNotFoundException {
190: return super .resolveTransitively(artifacts,
191: originatingArtifact, localRepository,
192: remoteRepositories, new Source(artifactFactory,
193: repositoryFactory, container), filter);
194: }
195:
196: public ArtifactResolutionResult resolveTransitively(Set artifacts,
197: Artifact originatingArtifact, List remoteRepositories,
198: ArtifactRepository localRepository,
199: ArtifactMetadataSource source)
200: throws ArtifactResolutionException,
201: ArtifactNotFoundException {
202: return super .resolveTransitively(artifacts,
203: originatingArtifact, remoteRepositories,
204: localRepository, new Source(artifactFactory,
205: repositoryFactory, container));
206: }
207:
208: public void contextualize(Context context) throws ContextException {
209: this .container = (PlexusContainer) context
210: .get(PlexusConstants.PLEXUS_KEY);
211: }
212:
213: }
|