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