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