1 package org.apache.maven.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.DefaultArtifact;
31 import org.apache.maven.artifact.InvalidRepositoryException;
32 import org.apache.maven.artifact.factory.ArtifactFactory;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
35 import org.apache.maven.artifact.repository.MavenArtifactRepository;
36 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
38 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
39 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
40 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
41 import org.apache.maven.artifact.versioning.VersionRange;
42 import org.apache.maven.model.Dependency;
43 import org.apache.maven.model.Model;
44 import org.apache.maven.model.Plugin;
45 import org.apache.maven.model.Repository;
46 import org.apache.maven.model.io.ModelReader;
47 import org.apache.maven.project.artifact.ArtifactWithDependencies;
48 import org.apache.maven.settings.Mirror;
49 import org.apache.maven.settings.Proxy;
50 import org.apache.maven.settings.Server;
51 import org.codehaus.plexus.component.annotations.Component;
52 import org.codehaus.plexus.component.annotations.Requirement;
53 import org.codehaus.plexus.util.FileUtils;
54 import org.codehaus.plexus.util.StringUtils;
55 import org.eclipse.aether.RepositorySystemSession;
56
57
58
59
60 @Component( role = RepositorySystem.class )
61 public class TestRepositorySystem
62 implements RepositorySystem
63 {
64
65 @Requirement
66 private ModelReader modelReader;
67
68 @Requirement
69 private ArtifactFactory artifactFactory;
70
71 public ArtifactRepository buildArtifactRepository( Repository repository )
72 throws InvalidRepositoryException
73 {
74 return new MavenArtifactRepository( repository.getId(), repository.getUrl(), new DefaultRepositoryLayout(),
75 new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
76 }
77
78 public Artifact createArtifact( String groupId, String artifactId, String version, String packaging )
79 {
80 return createArtifact( groupId, artifactId, version, null, packaging );
81 }
82
83 public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
84 {
85 return new DefaultArtifact( groupId, artifactId, version, scope, type, null, new TestArtifactHandler( type ) );
86 }
87
88 public ArtifactRepository createArtifactRepository( String id, String url,
89 ArtifactRepositoryLayout repositoryLayout,
90 ArtifactRepositoryPolicy snapshots,
91 ArtifactRepositoryPolicy releases )
92 {
93 return new MavenArtifactRepository( id, url, repositoryLayout, snapshots, releases );
94 }
95
96 public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type,
97 String classifier )
98 {
99 return new DefaultArtifact( groupId, artifactId, version, null, type, classifier,
100 new TestArtifactHandler( type ) );
101 }
102
103 public ArtifactRepository createDefaultLocalRepository()
104 throws InvalidRepositoryException
105 {
106 return createLocalRepository( new File( System.getProperty( "basedir", "" ), "target/local-repo" ).getAbsoluteFile() );
107 }
108
109 public ArtifactRepository createDefaultRemoteRepository()
110 throws InvalidRepositoryException
111 {
112 return new MavenArtifactRepository( DEFAULT_REMOTE_REPO_ID, "file://"
113 + new File( System.getProperty( "basedir", "" ), "src/test/remote-repo" ).toURI().getPath(),
114 new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
115 new ArtifactRepositoryPolicy() );
116 }
117
118 public Artifact createDependencyArtifact( Dependency dependency )
119 {
120 Artifact artifact =
121 new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(),
122 dependency.getScope(), dependency.getType(), dependency.getClassifier(),
123 new TestArtifactHandler( dependency.getType() ) );
124
125 if ( Artifact.SCOPE_SYSTEM.equals( dependency.getScope() ) )
126 {
127 artifact.setFile( new File( dependency.getSystemPath() ) );
128 artifact.setResolved( true );
129 }
130
131 return artifact;
132 }
133
134 public ArtifactRepository createLocalRepository( File localRepository )
135 throws InvalidRepositoryException
136 {
137 return new MavenArtifactRepository( DEFAULT_LOCAL_REPO_ID, "file://" + localRepository.toURI().getPath(),
138 new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(),
139 new ArtifactRepositoryPolicy() );
140 }
141
142 public Artifact createPluginArtifact( Plugin plugin )
143 {
144 VersionRange versionRange;
145 try
146 {
147 String version = plugin.getVersion();
148 if ( StringUtils.isEmpty( version ) )
149 {
150 version = "RELEASE";
151 }
152 versionRange = VersionRange.createFromVersionSpec( version );
153 }
154 catch ( InvalidVersionSpecificationException e )
155 {
156 return null;
157 }
158
159 return artifactFactory.createPluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), versionRange );
160 }
161
162 public Artifact createProjectArtifact( String groupId, String artifactId, String version )
163 {
164 return createArtifact( groupId, artifactId, version, "pom" );
165 }
166
167 public List<ArtifactRepository> getEffectiveRepositories( List<ArtifactRepository> repositories )
168 {
169 return repositories;
170 }
171
172 public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
173 {
174 return null;
175 }
176
177 public void injectAuthentication( List<ArtifactRepository> repositories, List<Server> servers )
178 {
179 }
180
181 public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors )
182 {
183 }
184
185 public void injectProxy( List<ArtifactRepository> repositories, List<Proxy> proxies )
186 {
187 }
188
189 public void publish( ArtifactRepository repository, File source, String remotePath,
190 ArtifactTransferListener transferListener )
191 throws ArtifactTransferFailedException
192 {
193
194
195 }
196
197 public ArtifactResolutionResult resolve( ArtifactResolutionRequest request )
198 {
199 ArtifactResolutionResult result = new ArtifactResolutionResult();
200
201 if ( request.isResolveRoot() )
202 {
203 try
204 {
205 resolve( request.getArtifact(), request );
206 result.addArtifact( request.getArtifact() );
207 }
208 catch ( IOException e )
209 {
210 result.addMissingArtifact( request.getArtifact() );
211 }
212 }
213
214 if ( request.isResolveTransitively() )
215 {
216 Map<String, Artifact> artifacts = new LinkedHashMap<>();
217
218 if ( request.getArtifactDependencies() != null )
219 {
220 for ( Artifact artifact : request.getArtifactDependencies() )
221 {
222 artifacts.put( artifact.getDependencyConflictId(), artifact );
223 }
224 }
225
226 List<Dependency> dependencies = new ArrayList<>();
227 if ( request.getArtifact() instanceof ArtifactWithDependencies )
228 {
229 dependencies = ( (ArtifactWithDependencies) request.getArtifact() ).getDependencies();
230 }
231 else
232 {
233 Artifact pomArtifact =
234 createProjectArtifact( request.getArtifact().getGroupId(), request.getArtifact().getArtifactId(),
235 request.getArtifact().getVersion() );
236 File pomFile =
237 new File( request.getLocalRepository().getBasedir(),
238 request.getLocalRepository().pathOf( pomArtifact ) );
239
240 try
241 {
242 Model model = modelReader.read( pomFile, null );
243
244 dependencies = model.getDependencies();
245 }
246 catch ( IOException e )
247 {
248 e.printStackTrace();
249 }
250 }
251
252 for ( Dependency dependency : dependencies )
253 {
254 Artifact artifact = createDependencyArtifact( dependency );
255 if ( !artifacts.containsKey( artifact.getDependencyConflictId() ) )
256 {
257 artifacts.put( artifact.getDependencyConflictId(), artifact );
258 }
259 }
260
261 for ( Artifact artifact : artifacts.values() )
262 {
263 try
264 {
265 resolve( artifact, request );
266 result.addArtifact( artifact );
267 }
268 catch ( IOException e )
269 {
270 result.addMissingArtifact( artifact );
271 }
272 }
273 }
274
275 return result;
276 }
277
278 private void resolve( Artifact artifact, ArtifactResolutionRequest request )
279 throws IOException
280 {
281 if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
282 {
283 return;
284 }
285
286 ArtifactRepository localRepo = request.getLocalRepository();
287
288 File localFile = new File( localRepo.getBasedir(), localRepo.pathOf( artifact ) );
289
290 artifact.setFile( localFile );
291
292 if ( !localFile.exists() )
293 {
294 if ( request.getRemoteRepositories().isEmpty() )
295 {
296 throw new IOException( localFile + " does not exist and no remote repositories are configured" );
297 }
298
299 ArtifactRepository remoteRepo = request.getRemoteRepositories().get( 0 );
300
301 File remoteFile = new File( remoteRepo.getBasedir(), remoteRepo.pathOf( artifact ) );
302
303 FileUtils.copyFile( remoteFile, localFile );
304 }
305
306 artifact.setResolved( true );
307 }
308
309 public void retrieve( ArtifactRepository repository, File destination, String remotePath,
310 ArtifactTransferListener transferListener )
311 throws ArtifactTransferFailedException, ArtifactDoesNotExistException
312 {
313
314
315 }
316
317 public void injectMirror( RepositorySystemSession session, List<ArtifactRepository> repositories )
318 {
319 }
320
321 public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories )
322 {
323 }
324
325 public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories )
326 {
327 }
328
329 }