1 package org.apache.maven.plugin.invoker;
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.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Map;
29
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.ArtifactUtils;
32 import org.apache.maven.artifact.factory.ArtifactFactory;
33 import org.apache.maven.artifact.installer.ArtifactInstaller;
34 import org.apache.maven.artifact.repository.ArtifactRepository;
35 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
36 import org.apache.maven.plugin.AbstractMojo;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.project.MavenProject;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class InstallMojo
54 extends AbstractMojo
55 {
56
57
58
59
60
61
62 private ArtifactInstaller installer;
63
64
65
66
67
68
69 private ArtifactFactory artifactFactory;
70
71
72
73
74
75
76 private ArtifactRepositoryFactory repositoryFactory;
77
78
79
80
81
82
83 private ArtifactRepository localRepository;
84
85
86
87
88
89
90
91
92
93 private File localRepositoryPath;
94
95
96
97
98
99
100
101
102 private MavenProject project;
103
104
105
106
107
108
109
110 private Collection reactorProjects;
111
112
113
114
115
116
117 public void execute()
118 throws MojoExecutionException
119 {
120 ArtifactRepository testRepository = createTestRepository();
121
122 installProjectArtifacts( project, testRepository );
123 installProjectParents( project, testRepository );
124 installProjectDependencies( project, reactorProjects, testRepository );
125 }
126
127
128
129
130
131
132
133
134
135
136 private ArtifactRepository createTestRepository()
137 throws MojoExecutionException
138 {
139 ArtifactRepository testRepository = localRepository;
140
141 if ( localRepositoryPath != null )
142 {
143 try
144 {
145 if ( !localRepositoryPath.exists() && !localRepositoryPath.mkdirs() )
146 {
147 throw new IOException( "Failed to create directory: " + localRepositoryPath );
148 }
149
150 testRepository =
151 repositoryFactory.createArtifactRepository( localRepository.getId(),
152 localRepositoryPath.toURL().toExternalForm(),
153 localRepository.getLayout(),
154 localRepository.getSnapshots(),
155 localRepository.getReleases() );
156 }
157 catch ( Exception e )
158 {
159 throw new MojoExecutionException( "Failed to create local repository: " + localRepositoryPath, e );
160 }
161 }
162
163 return testRepository;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177 private void installArtifact( File file, Artifact artifact, ArtifactRepository testRepository )
178 throws MojoExecutionException
179 {
180 try
181 {
182 if ( file == null )
183 {
184 throw new IllegalStateException( "Artifact has no associated file: " + file );
185 }
186 if ( !file.isFile() )
187 {
188 throw new IllegalStateException( "Artifact is not fully assembled: " + file );
189 }
190 installer.install( file, artifact, testRepository );
191 }
192 catch ( Exception e )
193 {
194 throw new MojoExecutionException( "Failed to install artifact: " + artifact, e );
195 }
196 }
197
198
199
200
201
202
203
204
205 private void installProjectArtifacts( MavenProject mvnProject, ArtifactRepository testRepository )
206 throws MojoExecutionException
207 {
208 try
209 {
210 installProjectPom( mvnProject, testRepository );
211
212
213 Artifact mainArtifact = mvnProject.getArtifact();
214 if ( mainArtifact.getFile() != null )
215 {
216 installArtifact( mainArtifact.getFile(), mainArtifact, testRepository );
217 }
218
219
220 Collection attachedArtifacts = mvnProject.getAttachedArtifacts();
221 for ( Iterator artifactIter = attachedArtifacts.iterator(); artifactIter.hasNext(); )
222 {
223 Artifact attachedArtifact = (Artifact) artifactIter.next();
224 installArtifact( attachedArtifact.getFile(), attachedArtifact, testRepository );
225 }
226 }
227 catch ( Exception e )
228 {
229 throw new MojoExecutionException( "Failed to install project artifacts: " + mvnProject, e );
230 }
231 }
232
233
234
235
236
237
238
239
240
241 private void installProjectParents( MavenProject mvnProject, ArtifactRepository testRepository )
242 throws MojoExecutionException
243 {
244 try
245 {
246 for ( MavenProject parent = mvnProject.getParent(); parent != null; parent = parent.getParent() )
247 {
248 if ( parent.getFile() == null )
249 {
250 break;
251 }
252 installProjectPom( parent, testRepository );
253 }
254 }
255 catch ( Exception e )
256 {
257 throw new MojoExecutionException( "Failed to install project parents: " + mvnProject, e );
258 }
259 }
260
261
262
263
264
265
266
267
268 private void installProjectPom( MavenProject mvnProject, ArtifactRepository testRepository )
269 throws MojoExecutionException
270 {
271 try
272 {
273 Artifact pomArtifact = null;
274 if ( "pom".equals( mvnProject.getPackaging() ) )
275 {
276 pomArtifact = mvnProject.getArtifact();
277 }
278 if ( pomArtifact == null )
279 {
280 pomArtifact =
281 artifactFactory.createProjectArtifact( mvnProject.getGroupId(), mvnProject.getArtifactId(),
282 mvnProject.getVersion() );
283 }
284 installArtifact( mvnProject.getFile(), pomArtifact, testRepository );
285 }
286 catch ( Exception e )
287 {
288 throw new MojoExecutionException( "Failed to install POM: " + mvnProject, e );
289 }
290 }
291
292
293
294
295
296
297
298
299
300
301 private void installProjectDependencies( MavenProject mvnProject, Collection reactorProjects,
302 ArtifactRepository testRepository )
303 throws MojoExecutionException
304 {
305
306 Map projects = new HashMap();
307 for ( Iterator it = reactorProjects.iterator(); it.hasNext(); )
308 {
309 MavenProject reactorProject = (MavenProject) it.next();
310 String id = reactorProject.getGroupId() + ':' + reactorProject.getArtifactId();
311 projects.put( id, reactorProject );
312 }
313
314
315 Collection dependencies = new HashSet();
316 for ( Iterator it = mvnProject.getRuntimeArtifacts().iterator(); it.hasNext(); )
317 {
318 Artifact artifact = (Artifact) it.next();
319 String id = ArtifactUtils.versionlessKey( artifact );
320
321 dependencies.add( id );
322 }
323
324
325 try
326 {
327
328 for ( Iterator it = dependencies.iterator(); it.hasNext(); )
329 {
330 String id = (String) it.next();
331 MavenProject requiredProject = (MavenProject) projects.remove( id );
332 if ( requiredProject != null )
333 {
334 it.remove();
335 installProjectArtifacts( requiredProject, testRepository );
336 installProjectParents( requiredProject, testRepository );
337 }
338 }
339
340
341 for ( Iterator it = mvnProject.getRuntimeArtifacts().iterator(); it.hasNext(); )
342 {
343 Artifact artifact = (Artifact) it.next();
344 String id = ArtifactUtils.versionlessKey( artifact );
345
346 if ( dependencies.contains( id ) )
347 {
348 File artifactFile = artifact.getFile();
349
350 installArtifact( artifactFile, artifact, testRepository );
351
352 Artifact pomArtifact =
353 artifactFactory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(),
354 artifact.getVersion(), null, "pom" );
355
356 File pomFile = new File( localRepository.getBasedir(), localRepository.pathOf( pomArtifact ) );
357
358 if ( pomFile.exists() )
359 {
360 installArtifact( pomFile, pomArtifact, testRepository );
361 }
362 }
363 }
364 }
365 catch ( Exception e )
366 {
367 throw new MojoExecutionException( "Failed to install project dependencies: " + mvnProject, e );
368 }
369 }
370
371 }