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 | 0 | 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 | 0 | if ( pomFile != null ) |
221 | |
{ |
222 | 0 | generatePom = false; |
223 | |
|
224 | 0 | Model model = readModel( pomFile ); |
225 | |
|
226 | 0 | processModel( model ); |
227 | |
} |
228 | 0 | } |
229 | |
|
230 | |
public void execute() |
231 | |
throws MojoExecutionException, MojoFailureException |
232 | |
{ |
233 | 0 | GpgSigner signer = newSigner( null ); |
234 | 0 | signer.setOutputDirectory( ascDirectory ); |
235 | 0 | signer.setBaseDirectory( new File( "" ).getAbsoluteFile() ); |
236 | |
|
237 | 0 | if ( offline ) |
238 | |
{ |
239 | 0 | throw new MojoFailureException( "Cannot deploy artifacts when Maven is in offline mode" ); |
240 | |
} |
241 | |
|
242 | 0 | initProperties(); |
243 | |
|
244 | 0 | validateArtifactInformation(); |
245 | |
|
246 | 0 | if ( !file.exists() ) |
247 | |
{ |
248 | 0 | throw new MojoFailureException( file.getPath() + " not found." ); |
249 | |
} |
250 | |
|
251 | 0 | ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( repositoryLayout ); |
252 | 0 | if ( layout == null ) |
253 | |
{ |
254 | 0 | throw new MojoFailureException( "Invalid repository layout: " + repositoryLayout ); |
255 | |
} |
256 | |
|
257 | 0 | ArtifactRepository deploymentRepository = |
258 | |
repositoryFactory.createDeploymentArtifactRepository( repositoryId, url, layout, uniqueVersion ); |
259 | |
|
260 | 0 | if ( StringUtils.isEmpty( deploymentRepository.getProtocol() ) ) |
261 | |
{ |
262 | 0 | throw new MojoFailureException( "No transfer protocol found." ); |
263 | |
} |
264 | |
|
265 | 0 | Artifact artifact = |
266 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier ); |
267 | |
|
268 | 0 | if ( file.equals( getLocalRepoFile( artifact ) ) ) |
269 | |
{ |
270 | 0 | throw new MojoFailureException( "Cannot deploy artifact from the local repository: " + file ); |
271 | |
} |
272 | |
|
273 | 0 | File fileSig = signer.generateSignatureForArtifact( file ); |
274 | 0 | ArtifactMetadata metadata = new AscArtifactMetadata( artifact, fileSig, false ); |
275 | 0 | artifact.addMetadata( metadata ); |
276 | |
|
277 | 0 | if ( !"pom".equals( packaging ) ) |
278 | |
{ |
279 | 0 | if ( pomFile == null && generatePom ) |
280 | |
{ |
281 | 0 | pomFile = generatePomFile(); |
282 | |
} |
283 | 0 | if ( pomFile != null ) |
284 | |
{ |
285 | 0 | metadata = new ProjectArtifactMetadata( artifact, pomFile ); |
286 | 0 | artifact.addMetadata( metadata ); |
287 | |
|
288 | 0 | fileSig = signer.generateSignatureForArtifact( pomFile ); |
289 | 0 | metadata = new AscArtifactMetadata( artifact, fileSig, true ); |
290 | 0 | artifact.addMetadata( metadata ); |
291 | |
} |
292 | |
} |
293 | |
|
294 | |
try |
295 | |
{ |
296 | 0 | deployer.deploy( file, artifact, deploymentRepository, localRepository ); |
297 | |
} |
298 | 0 | catch ( ArtifactDeploymentException e ) |
299 | |
{ |
300 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
301 | 0 | } |
302 | 0 | } |
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
private File getLocalRepoFile( Artifact artifact ) |
312 | |
{ |
313 | 0 | String path = localRepository.pathOf( artifact ); |
314 | 0 | return new File( localRepository.getBasedir(), path ); |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
private void processModel( Model model ) |
323 | |
{ |
324 | 0 | Parent parent = model.getParent(); |
325 | |
|
326 | 0 | if ( this.groupId == null ) |
327 | |
{ |
328 | 0 | this.groupId = model.getGroupId(); |
329 | 0 | if ( this.groupId == null && parent != null ) |
330 | |
{ |
331 | 0 | this.groupId = parent.getGroupId(); |
332 | |
} |
333 | |
} |
334 | 0 | if ( this.artifactId == null ) |
335 | |
{ |
336 | 0 | this.artifactId = model.getArtifactId(); |
337 | |
} |
338 | 0 | if ( this.version == null ) |
339 | |
{ |
340 | 0 | this.version = model.getVersion(); |
341 | 0 | if ( this.version == null && parent != null ) |
342 | |
{ |
343 | 0 | this.version = parent.getVersion(); |
344 | |
} |
345 | |
} |
346 | 0 | if ( this.packaging == null ) |
347 | |
{ |
348 | 0 | this.packaging = model.getPackaging(); |
349 | |
} |
350 | 0 | } |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
private Model readModel( File pomFile ) |
360 | |
throws MojoExecutionException |
361 | |
{ |
362 | 0 | Reader reader = null; |
363 | |
try |
364 | |
{ |
365 | 0 | reader = ReaderFactory.newXmlReader( pomFile ); |
366 | 0 | return new MavenXpp3Reader().read( reader ); |
367 | |
} |
368 | 0 | catch ( FileNotFoundException e ) |
369 | |
{ |
370 | 0 | throw new MojoExecutionException( "POM not found " + pomFile, e ); |
371 | |
} |
372 | 0 | catch ( IOException e ) |
373 | |
{ |
374 | 0 | throw new MojoExecutionException( "Error reading POM " + pomFile, e ); |
375 | |
} |
376 | 0 | catch ( XmlPullParserException e ) |
377 | |
{ |
378 | 0 | throw new MojoExecutionException( "Error parsing POM " + pomFile, e ); |
379 | |
} |
380 | |
finally |
381 | |
{ |
382 | 0 | IOUtil.close( reader ); |
383 | |
} |
384 | |
} |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
private File generatePomFile() |
393 | |
throws MojoExecutionException |
394 | |
{ |
395 | 0 | Model model = generateModel(); |
396 | |
|
397 | 0 | Writer fw = null; |
398 | |
try |
399 | |
{ |
400 | 0 | File tempFile = File.createTempFile( "mvndeploy", ".pom" ); |
401 | 0 | tempFile.deleteOnExit(); |
402 | |
|
403 | 0 | fw = WriterFactory.newXmlWriter( tempFile ); |
404 | 0 | new MavenXpp3Writer().write( fw, model ); |
405 | |
|
406 | 0 | return tempFile; |
407 | |
} |
408 | 0 | catch ( IOException e ) |
409 | |
{ |
410 | 0 | throw new MojoExecutionException( "Error writing temporary pom file: " + e.getMessage(), e ); |
411 | |
} |
412 | |
finally |
413 | |
{ |
414 | 0 | IOUtil.close( fw ); |
415 | |
} |
416 | |
} |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
private Model generateModel() |
424 | |
{ |
425 | 0 | Model model = new Model(); |
426 | |
|
427 | 0 | model.setModelVersion( "4.0.0" ); |
428 | |
|
429 | 0 | model.setGroupId( groupId ); |
430 | 0 | model.setArtifactId( artifactId ); |
431 | 0 | model.setVersion( version ); |
432 | 0 | model.setPackaging( packaging ); |
433 | |
|
434 | 0 | model.setDescription( description ); |
435 | |
|
436 | 0 | return model; |
437 | |
} |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
private void validateArtifactInformation() |
445 | |
throws MojoFailureException |
446 | |
{ |
447 | 0 | Model model = generateModel(); |
448 | |
|
449 | 0 | ModelValidationResult result = modelValidator.validate( model ); |
450 | |
|
451 | 0 | if ( result.getMessageCount() > 0 ) |
452 | |
{ |
453 | 0 | throw new MojoFailureException( "The artifact information is incomplete or not valid:\n" |
454 | |
+ result.render( " " ) ); |
455 | |
} |
456 | 0 | } |
457 | |
|
458 | |
} |