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