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