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