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