1 package org.apache.maven.plugin.install;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.installer.ArtifactInstallationException;
24 import org.apache.maven.artifact.metadata.ArtifactMetadata;
25 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
26 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27 import org.apache.maven.model.Model;
28 import org.apache.maven.model.Parent;
29 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
30 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
34 import org.apache.maven.project.validation.ModelValidationResult;
35 import org.apache.maven.project.validation.ModelValidator;
36 import org.codehaus.plexus.util.IOUtil;
37 import org.codehaus.plexus.util.ReaderFactory;
38 import org.codehaus.plexus.util.WriterFactory;
39 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40
41 import java.io.File;
42 import java.io.FileNotFoundException;
43 import java.io.IOException;
44 import java.io.Reader;
45 import java.io.Writer;
46 import java.net.MalformedURLException;
47 import java.util.Map;
48
49
50
51
52
53
54
55
56
57
58
59 public class InstallFileMojo
60 extends AbstractInstallMojo
61 {
62
63
64
65
66
67
68 protected String groupId;
69
70
71
72
73
74
75 protected String artifactId;
76
77
78
79
80
81
82 protected String version;
83
84
85
86
87
88
89 protected String packaging;
90
91
92
93
94
95
96
97
98 protected String classifier;
99
100
101
102
103
104
105
106 private File file;
107
108
109
110
111
112
113
114 private File javadoc;
115
116
117
118
119
120
121
122 private File sources;
123
124
125
126
127
128
129
130
131 private File pomFile;
132
133
134
135
136
137
138
139
140 private Boolean generatePom;
141
142
143
144
145
146
147
148
149
150 private String repositoryLayout;
151
152
153
154
155
156
157 private Map repositoryLayouts;
158
159
160
161
162
163
164
165
166 private File localRepositoryPath;
167
168
169
170
171
172
173 private ModelValidator modelValidator;
174
175
176
177
178 public void execute()
179 throws MojoExecutionException, MojoFailureException
180 {
181
182
183
184 if ( localRepositoryPath != null )
185 {
186 try
187 {
188 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( repositoryLayout );
189 getLog().debug( "Layout: " + layout.getClass() );
190
191 localRepository =
192 new DefaultArtifactRepository( localRepository.getId(), localRepositoryPath.toURL().toString(),
193 layout );
194 }
195 catch ( MalformedURLException e )
196 {
197 throw new MojoExecutionException( "MalformedURLException: " + e.getMessage(), e );
198 }
199 }
200
201 if ( pomFile != null )
202 {
203 processModel( readModel( pomFile ) );
204 }
205
206 validateArtifactInformation();
207
208 Artifact artifact =
209 artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
210
211 if ( file.equals( getLocalRepoFile( artifact ) ) )
212 {
213 throw new MojoFailureException( "Cannot install artifact. "
214 + "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n" );
215 }
216
217 File generatedPomFile = null;
218
219 if ( !"pom".equals( packaging ) )
220 {
221 if ( pomFile != null )
222 {
223 ArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, pomFile );
224 artifact.addMetadata( pomMetadata );
225 }
226 else
227 {
228 generatedPomFile = generatePomFile();
229 ArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, generatedPomFile );
230 if ( Boolean.TRUE.equals( generatePom )
231 || ( generatePom == null && !getLocalRepoFile( pomMetadata ).exists() ) )
232 {
233 getLog().debug( "Installing generated POM" );
234 artifact.addMetadata( pomMetadata );
235 }
236 else if ( generatePom == null )
237 {
238 getLog().debug( "Skipping installation of generated POM, already present in local repository" );
239 }
240 }
241 }
242
243 if ( updateReleaseInfo )
244 {
245 artifact.setRelease( true );
246 }
247
248
249
250 try
251 {
252 installer.install( file, artifact, localRepository );
253 installChecksums( artifact );
254 }
255 catch ( ArtifactInstallationException e )
256 {
257 throw new MojoExecutionException( "Error installing artifact '" + artifact.getDependencyConflictId()
258 + "': " + e.getMessage(), e );
259 }
260 finally
261 {
262 if ( generatedPomFile != null )
263 {
264 generatedPomFile.delete();
265 }
266 }
267
268 if ( sources != null )
269 {
270 artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", "sources" );
271 try
272 {
273 installer.install( sources, artifact, localRepository );
274 installChecksums( artifact );
275 }
276 catch ( ArtifactInstallationException e )
277 {
278 throw new MojoExecutionException( "Error installing sources " + sources + ": " + e.getMessage(), e );
279 }
280 }
281
282 if ( javadoc != null )
283 {
284 artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", "javadoc" );
285 try
286 {
287 installer.install( javadoc, artifact, localRepository );
288 installChecksums( artifact );
289 }
290 catch ( ArtifactInstallationException e )
291 {
292 throw new MojoExecutionException( "Error installing API docs " + javadoc + ": " + e.getMessage(), e );
293 }
294 }
295 }
296
297
298
299
300
301
302
303
304 private Model readModel( File pomFile )
305 throws MojoExecutionException
306 {
307 Reader reader = null;
308 try
309 {
310 reader = ReaderFactory.newXmlReader( pomFile );
311 return new MavenXpp3Reader().read( reader );
312 }
313 catch ( FileNotFoundException e )
314 {
315 throw new MojoExecutionException( "File not found " + pomFile, e );
316 }
317 catch ( IOException e )
318 {
319 throw new MojoExecutionException( "Error reading POM " + pomFile, e );
320 }
321 catch ( XmlPullParserException e )
322 {
323 throw new MojoExecutionException( "Error parsing POM " + pomFile, e );
324 }
325 finally
326 {
327 IOUtil.close( reader );
328 }
329 }
330
331
332
333
334
335
336 private void processModel( Model model )
337 {
338 Parent parent = model.getParent();
339
340 if ( this.groupId == null )
341 {
342 this.groupId = model.getGroupId();
343 if ( this.groupId == null && parent != null )
344 {
345 this.groupId = parent.getGroupId();
346 }
347 }
348 if ( this.artifactId == null )
349 {
350 this.artifactId = model.getArtifactId();
351 }
352 if ( this.version == null )
353 {
354 this.version = model.getVersion();
355 if ( this.version == null && parent != null )
356 {
357 this.version = parent.getVersion();
358 }
359 }
360 if ( this.packaging == null )
361 {
362 this.packaging = model.getPackaging();
363 }
364 }
365
366
367
368
369
370
371 private void validateArtifactInformation()
372 throws MojoExecutionException
373 {
374 Model model = generateModel();
375
376 ModelValidationResult result = modelValidator.validate( model );
377
378 if ( result.getMessageCount() > 0 )
379 {
380 throw new MojoExecutionException( "The artifact information is incomplete or not valid:\n"
381 + result.render( " " ) );
382 }
383 }
384
385
386
387
388
389
390 private Model generateModel()
391 {
392 Model model = new Model();
393
394 model.setModelVersion( "4.0.0" );
395
396 model.setGroupId( groupId );
397 model.setArtifactId( artifactId );
398 model.setVersion( version );
399 model.setPackaging( packaging );
400
401 model.setDescription( "POM was created from install:install-file" );
402
403 return model;
404 }
405
406
407
408
409
410
411
412
413 private File generatePomFile()
414 throws MojoExecutionException
415 {
416 Model model = generateModel();
417
418 Writer writer = null;
419 try
420 {
421 File pomFile = File.createTempFile( "mvninstall", ".pom" );
422
423 writer = WriterFactory.newXmlWriter( pomFile );
424 new MavenXpp3Writer().write( writer, model );
425
426 return pomFile;
427 }
428 catch ( IOException e )
429 {
430 throw new MojoExecutionException( "Error writing temporary POM file: " + e.getMessage(), e );
431 }
432 finally
433 {
434 IOUtil.close( writer );
435 }
436 }
437
438
439
440
441 public File getLocalRepositoryPath()
442 {
443 return this.localRepositoryPath;
444 }
445
446
447
448
449 public void setLocalRepositoryPath( File theLocalRepositoryPath )
450 {
451 this.localRepositoryPath = theLocalRepositoryPath;
452 }
453
454 }