1 package org.apache.maven.plugin.source;
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.archiver.MavenArchiveConfiguration;
23 import org.apache.maven.archiver.MavenArchiver;
24 import org.apache.maven.artifact.DependencyResolutionRequiredException;
25 import org.apache.maven.model.Resource;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.project.MavenProjectHelper;
30 import org.codehaus.plexus.archiver.Archiver;
31 import org.codehaus.plexus.archiver.ArchiverException;
32 import org.codehaus.plexus.archiver.jar.JarArchiver;
33 import org.codehaus.plexus.archiver.jar.ManifestException;
34 import org.codehaus.plexus.util.FileUtils;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Iterator;
41 import java.util.List;
42
43
44
45
46
47
48
49 public abstract class AbstractSourceJarMojo
50 extends AbstractMojo
51 {
52 private static final String[] DEFAULT_INCLUDES = new String[] { "**/*" };
53
54 private static final String[] DEFAULT_EXCLUDES = new String[] {};
55
56
57
58
59
60
61
62
63 private String[] includes;
64
65
66
67
68
69
70
71
72 private String[] excludes;
73
74
75
76
77
78
79
80
81 private boolean useDefaultExcludes;
82
83
84
85
86
87
88
89
90 protected MavenProject project;
91
92
93
94
95
96
97 private JarArchiver jarArchiver;
98
99
100
101
102
103
104
105
106 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
107
108
109
110
111
112
113
114
115
116
117 private File defaultManifestFile;
118
119
120
121
122
123
124
125 private boolean useDefaultManifestFile;
126
127
128
129
130
131
132 private boolean attach;
133
134
135
136
137
138
139
140
141
142 protected boolean excludeResources;
143
144
145
146
147
148
149
150 protected boolean includePom;
151
152
153
154
155
156
157 private MavenProjectHelper projectHelper;
158
159
160
161
162
163
164 protected File outputDirectory;
165
166
167
168
169
170
171
172
173 protected String finalName;
174
175
176
177
178
179
180
181 protected List reactorProjects;
182
183
184
185
186
187
188
189
190
191 private boolean forceCreation;
192
193
194
195
196
197
198 public void execute()
199 throws MojoExecutionException
200 {
201 packageSources( project );
202 }
203
204
205
206
207
208
209
210
211 protected abstract String getClassifier();
212
213
214
215
216
217 protected abstract List getSources( MavenProject p )
218 throws MojoExecutionException;
219
220
221
222
223
224 protected abstract List getResources( MavenProject p )
225 throws MojoExecutionException;
226
227 protected void packageSources( MavenProject p )
228 throws MojoExecutionException
229 {
230 if ( !"pom".equals( p.getPackaging() ) )
231 {
232 packageSources( Arrays.asList( new Object[] { p } ) );
233 }
234 }
235
236 protected void packageSources( List projects )
237 throws MojoExecutionException
238 {
239 if ( project.getArtifact().getClassifier() != null )
240 {
241 getLog().warn( "NOT adding sources to artifacts with classifier as Maven only supports one classifier "
242 + "per artifact. Current artifact [" + project.getArtifact().getId() + "] has a ["
243 + project.getArtifact().getClassifier() + "] classifier." );
244
245 return;
246 }
247
248 MavenArchiver archiver = createArchiver();
249
250 for ( Iterator i = projects.iterator(); i.hasNext(); )
251 {
252 MavenProject subProject = getProject( (MavenProject) i.next() );
253
254 if ( "pom".equals( subProject.getPackaging() ) )
255 {
256 continue;
257 }
258
259 archiveProjectContent( subProject, archiver.getArchiver() );
260 }
261
262 if(!archiver.getArchiver().getFiles().isEmpty()){
263
264 if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null )
265 {
266 getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() );
267 archive.setManifestFile( defaultManifestFile );
268 }
269
270 File outputFile = new File( outputDirectory, finalName + "-" + getClassifier() + getExtension() );
271
272 try
273 {
274 archiver.setOutputFile( outputFile );
275
276 archive.setAddMavenDescriptor( false );
277 archive.setForced( forceCreation );
278
279 archiver.createArchive( project, archive );
280 }
281 catch ( IOException e )
282 {
283 throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
284 }
285 catch ( ArchiverException e )
286 {
287 throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
288 }
289 catch ( DependencyResolutionRequiredException e )
290 {
291 throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
292 }
293 catch ( ManifestException e )
294 {
295 throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
296 }
297
298 if ( attach )
299 {
300 projectHelper.attachArtifact( project, getType(), getClassifier(), outputFile );
301 }
302 else
303 {
304 getLog().info( "NOT adding java-sources to attached artifacts list." );
305 }
306 }
307 else
308 {
309 getLog().info( "No sources in project. Archive not created." );
310 }
311 }
312
313 protected void archiveProjectContent( MavenProject p, Archiver archiver )
314 throws MojoExecutionException
315 {
316 if ( includePom )
317 {
318 try
319 {
320 archiver.addFile( p.getFile(), p.getFile().getName() );
321 }
322 catch ( ArchiverException e )
323 {
324 throw new MojoExecutionException( "Error adding POM file to target jar file.", e );
325 }
326 }
327
328 for ( Iterator i = getSources( p ).iterator(); i.hasNext(); )
329 {
330 String s = (String) i.next();
331
332 File sourceDirectory = new File( s );
333
334 if ( sourceDirectory.exists() )
335 {
336 addDirectory( archiver, sourceDirectory, getCombinedIncludes( null ), getCombinedExcludes( null ) );
337 }
338 }
339
340
341 for ( Iterator i = getResources( p ).iterator(); i.hasNext(); )
342 {
343 Resource resource = (Resource) i.next();
344
345 File sourceDirectory = new File( resource.getDirectory() );
346
347 if ( !sourceDirectory.exists() )
348 {
349 continue;
350 }
351
352 List resourceIncludes = resource.getIncludes();
353
354 String[] combinedIncludes = getCombinedIncludes( resourceIncludes );
355
356 List resourceExcludes = resource.getExcludes();
357
358 String[] combinedExcludes = getCombinedExcludes( resourceExcludes );
359
360 String targetPath = resource.getTargetPath();
361 if ( targetPath != null )
362 {
363 if ( !targetPath.trim().endsWith( "/" ) )
364 {
365 targetPath += "/";
366 }
367 addDirectory( archiver, sourceDirectory, targetPath, combinedIncludes, combinedExcludes );
368 }
369 else
370 {
371 addDirectory( archiver, sourceDirectory, combinedIncludes, combinedExcludes );
372 }
373 }
374 }
375
376 protected MavenArchiver createArchiver()
377 throws MojoExecutionException
378 {
379 MavenArchiver archiver = new MavenArchiver();
380 archiver.setArchiver( jarArchiver );
381
382 if ( project.getBuild() != null )
383 {
384 List resources = project.getBuild().getResources();
385
386 for ( Iterator i = resources.iterator(); i.hasNext(); )
387 {
388 Resource r = (Resource) i.next();
389
390 if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) )
391 {
392 addDirectory( archiver.getArchiver(), new File( r.getDirectory() ), getCombinedIncludes( null ),
393 getCombinedExcludes( null ) );
394 }
395 }
396 }
397
398 return archiver;
399 }
400
401 protected void addDirectory( Archiver archiver, File sourceDirectory, String[] includes, String[] excludes )
402 throws MojoExecutionException
403 {
404 try
405 {
406 archiver.addDirectory( sourceDirectory, includes, excludes );
407 }
408 catch ( ArchiverException e )
409 {
410 throw new MojoExecutionException( "Error adding directory to source archive.", e );
411 }
412 }
413
414 protected void addDirectory( Archiver archiver, File sourceDirectory, String prefix, String[] includes,
415 String[] excludes )
416 throws MojoExecutionException
417 {
418 try
419 {
420 archiver.addDirectory( sourceDirectory, prefix, includes, excludes );
421 }
422 catch ( ArchiverException e )
423 {
424 throw new MojoExecutionException( "Error adding directory to source archive.", e );
425 }
426 }
427
428 protected String getExtension()
429 {
430 return ".jar";
431 }
432
433 protected MavenProject getProject( MavenProject p )
434 {
435 if ( p.getExecutionProject() != null )
436 {
437 return p.getExecutionProject();
438 }
439
440 return p;
441 }
442
443 protected String getType()
444 {
445 return "java-source";
446 }
447
448
449
450
451
452
453
454
455 private String[] getCombinedIncludes( List additionalIncludes )
456 {
457 ArrayList combinedIncludes = new ArrayList();
458
459 if ( includes != null && includes.length > 0 )
460 {
461 combinedIncludes.addAll( Arrays.asList( includes ) );
462 }
463
464 if ( additionalIncludes != null && additionalIncludes.size() > 0 )
465 {
466 combinedIncludes.addAll( additionalIncludes );
467 }
468
469
470 if ( combinedIncludes.size() == 0 )
471 {
472 combinedIncludes.addAll( Arrays.asList( DEFAULT_INCLUDES ) );
473 }
474
475 return (String[]) combinedIncludes.toArray( new String[combinedIncludes.size()] );
476 }
477
478
479
480
481
482
483
484
485
486 private String[] getCombinedExcludes( List additionalExcludes )
487 {
488 ArrayList combinedExcludes = new ArrayList();
489
490 if ( useDefaultExcludes )
491 {
492 combinedExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
493 }
494
495 if ( excludes != null && excludes.length > 0 )
496 {
497 combinedExcludes.addAll( Arrays.asList( excludes ) );
498 }
499
500 if ( additionalExcludes != null && additionalExcludes.size() > 0 )
501 {
502 combinedExcludes.addAll( additionalExcludes );
503 }
504
505 if ( combinedExcludes.size() == 0 )
506 {
507 combinedExcludes.addAll( Arrays.asList( DEFAULT_EXCLUDES ) );
508 }
509
510 return (String[]) combinedExcludes.toArray( new String[combinedExcludes.size()] );
511 }
512 }