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