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 | 16 | 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 | 14 | if ( localRepositoryPath != null ) |
185 | |
{ |
186 | |
try |
187 | |
{ |
188 | 2 | ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( repositoryLayout ); |
189 | 2 | getLog().debug( "Layout: " + layout.getClass() ); |
190 | |
|
191 | 2 | localRepository = |
192 | |
new DefaultArtifactRepository( localRepository.getId(), localRepositoryPath.toURL().toString(), |
193 | |
layout ); |
194 | |
} |
195 | 0 | catch ( MalformedURLException e ) |
196 | |
{ |
197 | 0 | throw new MojoExecutionException( "MalformedURLException: " + e.getMessage(), e ); |
198 | 2 | } |
199 | |
} |
200 | |
|
201 | 14 | if ( pomFile != null ) |
202 | |
{ |
203 | 2 | processModel( readModel( pomFile ) ); |
204 | |
} |
205 | |
|
206 | 14 | validateArtifactInformation(); |
207 | |
|
208 | 14 | Artifact artifact = |
209 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier ); |
210 | |
|
211 | 14 | if ( file.equals( getLocalRepoFile( artifact ) ) ) |
212 | |
{ |
213 | 0 | throw new MojoFailureException( "Cannot install artifact. " |
214 | |
+ "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n" ); |
215 | |
} |
216 | |
|
217 | 14 | File generatedPomFile = null; |
218 | |
|
219 | 14 | if ( !"pom".equals( packaging ) ) |
220 | |
{ |
221 | 12 | if ( pomFile != null ) |
222 | |
{ |
223 | 2 | ArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, pomFile ); |
224 | 2 | artifact.addMetadata( pomMetadata ); |
225 | 2 | } |
226 | |
else |
227 | |
{ |
228 | 10 | generatedPomFile = generatePomFile(); |
229 | 10 | ArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, generatedPomFile ); |
230 | 10 | if ( Boolean.TRUE.equals( generatePom ) |
231 | |
|| ( generatePom == null && !getLocalRepoFile( pomMetadata ).exists() ) ) |
232 | |
{ |
233 | 10 | getLog().debug( "Installing generated POM" ); |
234 | 10 | artifact.addMetadata( pomMetadata ); |
235 | |
} |
236 | 0 | else if ( generatePom == null ) |
237 | |
{ |
238 | 0 | getLog().debug( "Skipping installation of generated POM, already present in local repository" ); |
239 | |
} |
240 | |
} |
241 | |
} |
242 | |
|
243 | 14 | if ( updateReleaseInfo ) |
244 | |
{ |
245 | 0 | artifact.setRelease( true ); |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
try |
251 | |
{ |
252 | 14 | installer.install( file, artifact, localRepository ); |
253 | 14 | installChecksums( artifact ); |
254 | |
} |
255 | 0 | catch ( ArtifactInstallationException e ) |
256 | |
{ |
257 | 0 | throw new MojoExecutionException( "Error installing artifact '" + artifact.getDependencyConflictId() |
258 | |
+ "': " + e.getMessage(), e ); |
259 | |
} |
260 | |
finally |
261 | |
{ |
262 | 14 | if ( generatedPomFile != null ) |
263 | |
{ |
264 | 10 | generatedPomFile.delete(); |
265 | |
} |
266 | |
} |
267 | |
|
268 | 14 | if ( sources != null ) |
269 | |
{ |
270 | 0 | artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", "sources" ); |
271 | |
try |
272 | |
{ |
273 | 0 | installer.install( sources, artifact, localRepository ); |
274 | 0 | installChecksums( artifact ); |
275 | |
} |
276 | 0 | catch ( ArtifactInstallationException e ) |
277 | |
{ |
278 | 0 | throw new MojoExecutionException( "Error installing sources " + sources + ": " + e.getMessage(), e ); |
279 | 0 | } |
280 | |
} |
281 | |
|
282 | 14 | if ( javadoc != null ) |
283 | |
{ |
284 | 0 | artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", "javadoc" ); |
285 | |
try |
286 | |
{ |
287 | 0 | installer.install( javadoc, artifact, localRepository ); |
288 | 0 | installChecksums( artifact ); |
289 | |
} |
290 | 0 | catch ( ArtifactInstallationException e ) |
291 | |
{ |
292 | 0 | throw new MojoExecutionException( "Error installing API docs " + javadoc + ": " + e.getMessage(), e ); |
293 | 0 | } |
294 | |
} |
295 | 14 | } |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
private Model readModel( File pomFile ) |
305 | |
throws MojoExecutionException |
306 | |
{ |
307 | 2 | Reader reader = null; |
308 | |
try |
309 | |
{ |
310 | 2 | reader = ReaderFactory.newXmlReader( pomFile ); |
311 | 2 | return new MavenXpp3Reader().read( reader ); |
312 | |
} |
313 | 0 | catch ( FileNotFoundException e ) |
314 | |
{ |
315 | 0 | throw new MojoExecutionException( "File not found " + pomFile, e ); |
316 | |
} |
317 | 0 | catch ( IOException e ) |
318 | |
{ |
319 | 0 | throw new MojoExecutionException( "Error reading POM " + pomFile, e ); |
320 | |
} |
321 | 0 | catch ( XmlPullParserException e ) |
322 | |
{ |
323 | 0 | throw new MojoExecutionException( "Error parsing POM " + pomFile, e ); |
324 | |
} |
325 | |
finally |
326 | |
{ |
327 | 2 | IOUtil.close( reader ); |
328 | |
} |
329 | |
} |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
private void processModel( Model model ) |
337 | |
{ |
338 | 2 | Parent parent = model.getParent(); |
339 | |
|
340 | 2 | if ( this.groupId == null ) |
341 | |
{ |
342 | 0 | this.groupId = model.getGroupId(); |
343 | 0 | if ( this.groupId == null && parent != null ) |
344 | |
{ |
345 | 0 | this.groupId = parent.getGroupId(); |
346 | |
} |
347 | |
} |
348 | 2 | if ( this.artifactId == null ) |
349 | |
{ |
350 | 0 | this.artifactId = model.getArtifactId(); |
351 | |
} |
352 | 2 | if ( this.version == null ) |
353 | |
{ |
354 | 0 | this.version = model.getVersion(); |
355 | 0 | if ( this.version == null && parent != null ) |
356 | |
{ |
357 | 0 | this.version = parent.getVersion(); |
358 | |
} |
359 | |
} |
360 | 2 | if ( this.packaging == null ) |
361 | |
{ |
362 | 0 | this.packaging = model.getPackaging(); |
363 | |
} |
364 | 2 | } |
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
private void validateArtifactInformation() |
372 | |
throws MojoExecutionException |
373 | |
{ |
374 | 14 | Model model = generateModel(); |
375 | |
|
376 | 14 | ModelValidationResult result = modelValidator.validate( model ); |
377 | |
|
378 | 14 | if ( result.getMessageCount() > 0 ) |
379 | |
{ |
380 | 0 | throw new MojoExecutionException( "The artifact information is incomplete or not valid:\n" |
381 | |
+ result.render( " " ) ); |
382 | |
} |
383 | 14 | } |
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
private Model generateModel() |
391 | |
{ |
392 | 24 | Model model = new Model(); |
393 | |
|
394 | 24 | model.setModelVersion( "4.0.0" ); |
395 | |
|
396 | 24 | model.setGroupId( groupId ); |
397 | 24 | model.setArtifactId( artifactId ); |
398 | 24 | model.setVersion( version ); |
399 | 24 | model.setPackaging( packaging ); |
400 | |
|
401 | 24 | model.setDescription( "POM was created from install:install-file" ); |
402 | |
|
403 | 24 | return model; |
404 | |
} |
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
private File generatePomFile() |
414 | |
throws MojoExecutionException |
415 | |
{ |
416 | 10 | Model model = generateModel(); |
417 | |
|
418 | 10 | Writer writer = null; |
419 | |
try |
420 | |
{ |
421 | 10 | File pomFile = File.createTempFile( "mvninstall", ".pom" ); |
422 | |
|
423 | 10 | writer = WriterFactory.newXmlWriter( pomFile ); |
424 | 10 | new MavenXpp3Writer().write( writer, model ); |
425 | |
|
426 | 10 | return pomFile; |
427 | |
} |
428 | 0 | catch ( IOException e ) |
429 | |
{ |
430 | 0 | throw new MojoExecutionException( "Error writing temporary POM file: " + e.getMessage(), e ); |
431 | |
} |
432 | |
finally |
433 | |
{ |
434 | 10 | IOUtil.close( writer ); |
435 | |
} |
436 | |
} |
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
public File getLocalRepositoryPath() |
442 | |
{ |
443 | 0 | return this.localRepositoryPath; |
444 | |
} |
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public void setLocalRepositoryPath( File theLocalRepositoryPath ) |
450 | |
{ |
451 | 2 | this.localRepositoryPath = theLocalRepositoryPath; |
452 | 2 | } |
453 | |
|
454 | |
} |