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 | 16 | public abstract class AbstractSourceJarMojo |
50 | |
extends AbstractMojo |
51 | |
{ |
52 | 2 | private static final String[] DEFAULT_INCLUDES = new String[] { "**/*" }; |
53 | |
|
54 | 2 | 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 | 16 | 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 | 16 | packageSources( project ); |
202 | 16 | } |
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 | 16 | if ( !"pom".equals( p.getPackaging() ) ) |
231 | |
{ |
232 | 12 | packageSources( Arrays.asList( new Object[] { p } ) ); |
233 | |
} |
234 | 16 | } |
235 | |
|
236 | |
protected void packageSources( List projects ) |
237 | |
throws MojoExecutionException |
238 | |
{ |
239 | 12 | if ( project.getArtifact().getClassifier() != null ) |
240 | |
{ |
241 | 0 | 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 | 0 | return; |
246 | |
} |
247 | |
|
248 | 12 | MavenArchiver archiver = createArchiver(); |
249 | |
|
250 | 12 | for ( Iterator i = projects.iterator(); i.hasNext(); ) |
251 | |
{ |
252 | 12 | MavenProject subProject = getProject( (MavenProject) i.next() ); |
253 | |
|
254 | 12 | if ( "pom".equals( subProject.getPackaging() ) ) |
255 | |
{ |
256 | 0 | continue; |
257 | |
} |
258 | |
|
259 | 12 | archiveProjectContent( subProject, archiver.getArchiver() ); |
260 | 12 | } |
261 | |
|
262 | 12 | if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null ) |
263 | |
{ |
264 | 0 | getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() ); |
265 | 0 | archive.setManifestFile( defaultManifestFile ); |
266 | |
} |
267 | |
|
268 | 12 | File outputFile = new File( outputDirectory, finalName + "-" + getClassifier() + getExtension() ); |
269 | |
|
270 | |
try |
271 | |
{ |
272 | 12 | archiver.setOutputFile( outputFile ); |
273 | |
|
274 | 12 | archive.setAddMavenDescriptor( false ); |
275 | 12 | archive.setForced( forceCreation ); |
276 | |
|
277 | 12 | archiver.createArchive( project, archive ); |
278 | |
} |
279 | 0 | catch ( IOException e ) |
280 | |
{ |
281 | 0 | throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e ); |
282 | |
} |
283 | 0 | catch ( ArchiverException e ) |
284 | |
{ |
285 | 0 | throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e ); |
286 | |
} |
287 | 0 | catch ( DependencyResolutionRequiredException e ) |
288 | |
{ |
289 | 0 | throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e ); |
290 | |
} |
291 | 0 | catch ( ManifestException e ) |
292 | |
{ |
293 | 0 | throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e ); |
294 | 12 | } |
295 | |
|
296 | 12 | if ( attach ) |
297 | |
{ |
298 | 0 | projectHelper.attachArtifact( project, getType(), getClassifier(), outputFile ); |
299 | |
} |
300 | |
else |
301 | |
{ |
302 | 12 | getLog().info( "NOT adding java-sources to attached artifacts list." ); |
303 | |
} |
304 | 12 | } |
305 | |
|
306 | |
protected void archiveProjectContent( MavenProject p, Archiver archiver ) |
307 | |
throws MojoExecutionException |
308 | |
{ |
309 | 12 | if ( includePom ) |
310 | |
{ |
311 | |
try |
312 | |
{ |
313 | 2 | archiver.addFile( p.getFile(), p.getFile().getName() ); |
314 | |
} |
315 | 0 | catch ( ArchiverException e ) |
316 | |
{ |
317 | 0 | throw new MojoExecutionException( "Error adding POM file to target jar file.", e ); |
318 | 2 | } |
319 | |
} |
320 | |
|
321 | 12 | for ( Iterator i = getSources( p ).iterator(); i.hasNext(); ) |
322 | |
{ |
323 | 12 | String s = (String) i.next(); |
324 | |
|
325 | 12 | File sourceDirectory = new File( s ); |
326 | |
|
327 | 12 | if ( sourceDirectory.exists() ) |
328 | |
{ |
329 | 12 | addDirectory( archiver, sourceDirectory, getCombinedIncludes( null ), getCombinedExcludes( null ) ); |
330 | |
} |
331 | 12 | } |
332 | |
|
333 | |
|
334 | 12 | for ( Iterator i = getResources( p ).iterator(); i.hasNext(); ) |
335 | |
{ |
336 | 12 | Resource resource = (Resource) i.next(); |
337 | |
|
338 | 12 | File sourceDirectory = new File( resource.getDirectory() ); |
339 | |
|
340 | 12 | if ( !sourceDirectory.exists() ) |
341 | |
{ |
342 | 0 | continue; |
343 | |
} |
344 | |
|
345 | 12 | List resourceIncludes = resource.getIncludes(); |
346 | |
|
347 | 12 | String[] combinedIncludes = getCombinedIncludes( resourceIncludes ); |
348 | |
|
349 | 12 | List resourceExcludes = resource.getExcludes(); |
350 | |
|
351 | 12 | String[] combinedExcludes = getCombinedExcludes( resourceExcludes ); |
352 | |
|
353 | 12 | String targetPath = resource.getTargetPath(); |
354 | 12 | if ( targetPath != null ) |
355 | |
{ |
356 | 0 | if ( !targetPath.trim().endsWith( "/" ) ) |
357 | |
{ |
358 | 0 | targetPath += "/"; |
359 | |
} |
360 | 0 | addDirectory( archiver, sourceDirectory, targetPath, combinedIncludes, combinedExcludes ); |
361 | |
} |
362 | |
else |
363 | |
{ |
364 | 12 | addDirectory( archiver, sourceDirectory, combinedIncludes, combinedExcludes ); |
365 | |
} |
366 | 12 | } |
367 | 12 | } |
368 | |
|
369 | |
protected MavenArchiver createArchiver() |
370 | |
throws MojoExecutionException |
371 | |
{ |
372 | 12 | MavenArchiver archiver = new MavenArchiver(); |
373 | 12 | archiver.setArchiver( jarArchiver ); |
374 | |
|
375 | 12 | if ( project.getBuild() != null ) |
376 | |
{ |
377 | 12 | List resources = project.getBuild().getResources(); |
378 | |
|
379 | 12 | for ( Iterator i = resources.iterator(); i.hasNext(); ) |
380 | |
{ |
381 | 0 | Resource r = (Resource) i.next(); |
382 | |
|
383 | 0 | if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) ) |
384 | |
{ |
385 | 0 | addDirectory( archiver.getArchiver(), new File( r.getDirectory() ), getCombinedIncludes( null ), |
386 | |
getCombinedExcludes( null ) ); |
387 | |
} |
388 | 0 | } |
389 | |
} |
390 | |
|
391 | 12 | return archiver; |
392 | |
} |
393 | |
|
394 | |
protected void addDirectory( Archiver archiver, File sourceDirectory, String[] includes, String[] excludes ) |
395 | |
throws MojoExecutionException |
396 | |
{ |
397 | |
try |
398 | |
{ |
399 | 24 | archiver.addDirectory( sourceDirectory, includes, excludes ); |
400 | |
} |
401 | 0 | catch ( ArchiverException e ) |
402 | |
{ |
403 | 0 | throw new MojoExecutionException( "Error adding directory to source archive.", e ); |
404 | 24 | } |
405 | 24 | } |
406 | |
|
407 | |
protected void addDirectory( Archiver archiver, File sourceDirectory, String prefix, String[] includes, |
408 | |
String[] excludes ) |
409 | |
throws MojoExecutionException |
410 | |
{ |
411 | |
try |
412 | |
{ |
413 | 0 | archiver.addDirectory( sourceDirectory, prefix, includes, excludes ); |
414 | |
} |
415 | 0 | catch ( ArchiverException e ) |
416 | |
{ |
417 | 0 | throw new MojoExecutionException( "Error adding directory to source archive.", e ); |
418 | 0 | } |
419 | 0 | } |
420 | |
|
421 | |
protected String getExtension() |
422 | |
{ |
423 | 12 | return ".jar"; |
424 | |
} |
425 | |
|
426 | |
protected MavenProject getProject( MavenProject p ) |
427 | |
{ |
428 | 12 | if ( p.getExecutionProject() != null ) |
429 | |
{ |
430 | 0 | return p.getExecutionProject(); |
431 | |
} |
432 | |
|
433 | 12 | return p; |
434 | |
} |
435 | |
|
436 | |
protected String getType() |
437 | |
{ |
438 | 0 | return "java-source"; |
439 | |
} |
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
private String[] getCombinedIncludes( List additionalIncludes ) |
449 | |
{ |
450 | 24 | ArrayList combinedIncludes = new ArrayList(); |
451 | |
|
452 | 24 | if ( includes != null && includes.length > 0 ) |
453 | |
{ |
454 | 0 | combinedIncludes.addAll( Arrays.asList( includes ) ); |
455 | |
} |
456 | |
|
457 | 24 | if ( additionalIncludes != null && additionalIncludes.size() > 0 ) |
458 | |
{ |
459 | 2 | combinedIncludes.addAll( additionalIncludes ); |
460 | |
} |
461 | |
|
462 | |
|
463 | 24 | if ( combinedIncludes.size() == 0 ) |
464 | |
{ |
465 | 22 | combinedIncludes.addAll( Arrays.asList( DEFAULT_INCLUDES ) ); |
466 | |
} |
467 | |
|
468 | 24 | return (String[]) combinedIncludes.toArray( new String[combinedIncludes.size()] ); |
469 | |
} |
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
private String[] getCombinedExcludes( List additionalExcludes ) |
480 | |
{ |
481 | 24 | ArrayList combinedExcludes = new ArrayList(); |
482 | |
|
483 | 24 | if ( useDefaultExcludes ) |
484 | |
{ |
485 | 0 | combinedExcludes.addAll( FileUtils.getDefaultExcludesAsList() ); |
486 | |
} |
487 | |
|
488 | 24 | if ( excludes != null && excludes.length > 0 ) |
489 | |
{ |
490 | 0 | combinedExcludes.addAll( Arrays.asList( excludes ) ); |
491 | |
} |
492 | |
|
493 | 24 | if ( additionalExcludes != null && additionalExcludes.size() > 0 ) |
494 | |
{ |
495 | 4 | combinedExcludes.addAll( additionalExcludes ); |
496 | |
} |
497 | |
|
498 | 24 | if ( combinedExcludes.size() == 0 ) |
499 | |
{ |
500 | 20 | combinedExcludes.addAll( Arrays.asList( DEFAULT_EXCLUDES ) ); |
501 | |
} |
502 | |
|
503 | 24 | return (String[]) combinedExcludes.toArray( new String[combinedExcludes.size()] ); |
504 | |
} |
505 | |
} |