1 package org.apache.maven.plugins.repository;
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.factory.ArtifactFactory;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
26 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
27 import org.apache.maven.artifact.resolver.ArtifactResolver;
28 import org.apache.maven.model.License;
29 import org.apache.maven.model.Model;
30 import org.apache.maven.model.Scm;
31 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
32 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.settings.Settings;
36 import org.codehaus.plexus.archiver.ArchiverException;
37 import org.codehaus.plexus.archiver.jar.JarArchiver;
38 import org.codehaus.plexus.components.interactivity.InputHandler;
39 import org.codehaus.plexus.util.ReaderFactory;
40 import org.codehaus.plexus.util.WriterFactory;
41 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
42
43 import java.io.File;
44 import java.io.FileNotFoundException;
45 import java.io.IOException;
46 import java.util.Collections;
47 import java.util.List;
48
49
50
51
52
53
54
55
56
57
58
59
60 public class BundlePackMojo
61 extends AbstractMojo
62 {
63 public static final String POM = "pom.xml";
64
65
66
67
68
69
70 protected JarArchiver jarArchiver;
71
72
73
74
75
76
77 protected ArtifactResolver artifactResolver;
78
79
80
81
82
83
84 protected ArtifactFactory artifactFactory;
85
86
87
88
89
90
91
92
93 protected ArtifactRepository localRepository;
94
95
96
97
98 protected InputHandler inputHandler;
99
100
101
102
103
104
105
106 protected String basedir;
107
108
109
110
111
112
113 protected String groupId;
114
115
116
117
118
119
120 protected String artifactId;
121
122
123
124
125
126
127 protected String version;
128
129
130
131
132
133 protected String scmUrl;
134
135
136
137
138
139
140
141
142
143
144 protected String scmConnection;
145
146
147
148
149
150 protected Settings settings;
151
152
153
154
155
156
157
158 private boolean disableMaterialization;
159
160 @SuppressWarnings( "unchecked" )
161 public void execute()
162 throws MojoExecutionException
163 {
164 readArtifactDataFromUser();
165
166 Artifact artifact = artifactFactory.createProjectArtifact( groupId, artifactId, version );
167
168 try
169 {
170 artifactResolver.resolve( artifact, Collections.EMPTY_LIST, localRepository );
171 }
172 catch ( ArtifactResolutionException e )
173 {
174 throw new MojoExecutionException( "Unable to resolve artifact " + artifact.getId(), e );
175 }
176 catch ( ArtifactNotFoundException e )
177 {
178 throw new MojoExecutionException( "Artifact " + artifact.getId() + " not found in local repository", e );
179 }
180
181 File pom = artifact.getFile();
182
183 File dir = pom.getParentFile();
184
185 Model model = readPom( pom );
186
187 boolean rewrite = false;
188 try
189 {
190
191 if ( model.getPackaging() == null )
192 {
193 model.setPackaging( "jar" );
194 rewrite = true;
195 }
196 if ( model.getName() == null )
197 {
198 getLog().info( "Project name is missing, please type the project name [" + artifactId + "]:" );
199 model.setName( inputHandler.readLine() );
200 if ( model.getName() == null )
201 {
202 model.setName( artifactId );
203 }
204 rewrite = true;
205 }
206 if ( model.getDescription() == null )
207 {
208 getLog().info( "Project description is missing, please type the project description:" );
209 model.setDescription( inputHandler.readLine() );
210 rewrite = true;
211 }
212 if ( model.getUrl() == null )
213 {
214 getLog().info( "Project URL is missing, please type the project URL:" );
215 model.setUrl( inputHandler.readLine() );
216 rewrite = true;
217 }
218
219 List<License> licenses = model.getLicenses();
220 if ( licenses.isEmpty() )
221 {
222 License license = new License();
223
224 getLog().info( "License name is missing, please type the license name:" );
225 license.setName( inputHandler.readLine() );
226 getLog().info( "License URL is missing, please type the license URL:" );
227 license.setUrl( inputHandler.readLine() );
228 licenses.add( license );
229 rewrite = true;
230 }
231
232 if ( disableMaterialization )
233 {
234 getLog().warn( "Validations to confirm support for project materialization have been DISABLED." +
235 "\n\nYour project may not provide the POM elements necessary to allow users to retrieve sources on-demand," +
236 "\nor to easily checkout your project in an IDE. THIS CAN SERIOUSLY INCONVENIENCE YOUR USERS." +
237 "\n\nContinue? [y/N]" );
238
239 try
240 {
241 if ( 'y' != inputHandler.readLine().toLowerCase().charAt( 0 ) )
242 {
243 disableMaterialization = false;
244 }
245 }
246 catch ( IOException e )
247 {
248 getLog().debug( "Error reading confirmation: " + e.getMessage(), e );
249 }
250
251 }
252
253 if ( !disableMaterialization )
254 {
255 Scm scm = model.getScm();
256 if ( scm == null )
257 {
258 scm = new Scm();
259 model.setScm( scm );
260 }
261
262 if ( scm.getUrl() == null )
263 {
264 if ( scmUrl != null )
265 {
266 scm.setUrl( scmUrl );
267 }
268 else
269 {
270 getLog().info( "SCM view URL is missing, please type the URL for the viewable SCM interface:" );
271 scm.setUrl( inputHandler.readLine() );
272 rewrite = true;
273 }
274 }
275
276 if ( scm.getConnection() == null )
277 {
278 if ( scmConnection != null )
279 {
280 scm.setConnection( scmConnection );
281 }
282 else
283 {
284 getLog().info( "SCM read-only connection URL is missing, please type the read-only SCM URL:" );
285 scm.setConnection( inputHandler.readLine() );
286 rewrite = true;
287 }
288 }
289 }
290 }
291 catch ( IOException e )
292 {
293 throw new MojoExecutionException( e.getMessage(), e );
294 }
295
296 try
297 {
298 if ( rewrite )
299 {
300 new MavenXpp3Writer().write( WriterFactory.newXmlWriter( pom ), model );
301 }
302
303 String finalName = null;
304
305 if ( model.getBuild() != null )
306 {
307 finalName = model.getBuild().getFinalName();
308 }
309 if ( finalName == null )
310 {
311 finalName = model.getArtifactId() + "-" + model.getVersion();
312 }
313
314 boolean batchMode = settings == null ? false : !settings.isInteractiveMode();
315 List<File> files = BundleUtils.selectProjectFiles( dir, inputHandler, finalName, pom, getLog(), batchMode );
316
317 File bundle = new File( basedir, finalName + "-bundle.jar" );
318
319 jarArchiver.addFile( pom, POM );
320
321 boolean artifactChecks = !"pom".equals( model.getPackaging() );
322 boolean sourcesFound = false;
323 boolean javadocsFound = false;
324
325 for ( File f : files )
326 {
327 if ( artifactChecks && f.getName().endsWith( finalName + "-sources.jar" ) )
328 {
329 sourcesFound = true;
330 }
331 else if ( artifactChecks && f.getName().equals( finalName + "-javadoc.jar" ) )
332 {
333 javadocsFound = true;
334 }
335
336 jarArchiver.addFile( f, f.getName() );
337 }
338
339 if ( artifactChecks && !sourcesFound )
340 {
341 getLog().warn( "Sources not included in upload bundle." );
342 }
343
344 if ( artifactChecks && !javadocsFound )
345 {
346 getLog().warn( "Javadoc not included in upload bundle." );
347 }
348
349 jarArchiver.setDestFile( bundle );
350
351 jarArchiver.createArchive();
352
353 }
354 catch ( IOException e )
355 {
356 throw new MojoExecutionException( e.getMessage(), e );
357 }
358 catch ( ArchiverException e )
359 {
360 throw new MojoExecutionException( e.getMessage(), e );
361 }
362
363 }
364
365
366
367
368
369
370
371 private void readArtifactDataFromUser()
372 throws MojoExecutionException
373 {
374 try
375 {
376 if ( groupId == null )
377 {
378 getLog().info( "groupId? " );
379
380 groupId = inputHandler.readLine();
381
382 }
383
384 if ( artifactId == null )
385 {
386 getLog().info( "artifactId? " );
387 artifactId = inputHandler.readLine();
388 }
389
390 if ( version == null )
391 {
392 getLog().info( "version? " );
393 version = inputHandler.readLine();
394 }
395 }
396 catch ( IOException e )
397 {
398 throw new MojoExecutionException( e.getMessage(), e );
399 }
400 }
401
402
403
404
405
406
407
408
409 private Model readPom( File pom )
410 throws MojoExecutionException
411 {
412 Model model;
413 try
414 {
415 model = new MavenXpp3Reader().read( ReaderFactory.newXmlReader( pom ) );
416 }
417 catch ( XmlPullParserException e )
418 {
419 throw new MojoExecutionException( "Unable to parse POM at " + pom.getAbsolutePath() + ": " + e.getMessage(),
420 e );
421 }
422 catch ( FileNotFoundException e )
423 {
424 throw new MojoExecutionException( "Unable to read POM at " + pom.getAbsolutePath() + ": " + e.getMessage(),
425 e );
426 }
427 catch ( IOException e )
428 {
429 throw new MojoExecutionException( "Unable to read POM at " + pom.getAbsolutePath() + ": " + e.getMessage(),
430 e );
431 }
432 return model;
433 }
434
435 }