/
AbstractSourceJarMojo.java
597 lines (515 loc) · 19 KB
/
AbstractSourceJarMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package org.apache.maven.plugins.source;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.util.FileUtils;
/**
* Base class for bundling sources into a jar archive.
*
* @since 2.0.3
*/
public abstract class AbstractSourceJarMojo
extends AbstractMojo
{
private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
private static final String[] DEFAULT_EXCLUDES = new String[] {};
/**
* List of files to include. Specified as fileset patterns which are relative to the input directory whose contents
* is being packaged into the JAR.
*
* @since 2.1
*/
@Parameter
private String[] includes;
/**
* List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents
* is being packaged into the JAR.
*
* @since 2.1
*/
@Parameter
private String[] excludes;
/**
* Exclude commonly excluded files such as SCM configuration. These are defined in the plexus
* FileUtils.getDefaultExcludes()
*
* @since 2.1
*/
@Parameter( property = "maven.source.useDefaultExcludes", defaultValue = "true" )
private boolean useDefaultExcludes;
/**
* The Maven Project Object
*/
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
/**
* The Jar archiver.
*/
@Component( role = Archiver.class, hint = "jar" )
private JarArchiver jarArchiver;
/**
* The archive configuration to use. See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
* Archiver Reference</a>. <br/>
* <b>Note: Since 3.0.0 the resulting archives contain a maven descriptor. If you need to suppress the generation of
* the maven descriptor you can simply achieve this by using the
* <a href="http://maven.apache.org/shared/maven-archiver/index.html#archive">archiver configuration</a>.</b>.
*
* @since 2.1
*/
@Parameter
private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
/**
* Path to the default MANIFEST file to use. It will be used if <code>useDefaultManifestFile</code> is set to
* <code>true</code>.
*
* @since 2.1
*/
// CHECKSTYLE_OFF: LineLength
@Parameter( defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", readonly = false, required = true )
// CHECKSTYLE_ON: LineLength
private File defaultManifestFile;
/**
* Set this to <code>true</code> to enable the use of the <code>defaultManifestFile</code>. <br/>
*
* @since 2.1
*/
@Parameter( property = "maven.source.useDefaultManifestFile", defaultValue = "false" )
private boolean useDefaultManifestFile;
/**
* Specifies whether or not to attach the artifact to the project
*/
@Parameter( property = "maven.source.attach", defaultValue = "true" )
private boolean attach;
/**
* Specifies whether or not to exclude resources from the sources-jar. This can be convenient if your project
* includes large resources, such as images, and you don't want to include them in the sources-jar.
*
* @since 2.0.4
*/
@Parameter( property = "maven.source.excludeResources", defaultValue = "false" )
protected boolean excludeResources;
/**
* Specifies whether or not to include the POM file in the sources-jar.
*
* @since 2.1
*/
@Parameter( property = "maven.source.includePom", defaultValue = "false" )
protected boolean includePom;
/**
* Used for attaching the source jar to the project.
*/
@Component
private MavenProjectHelper projectHelper;
/**
* The directory where the generated archive file will be put.
*/
@Parameter( defaultValue = "${project.build.directory}" )
protected File outputDirectory;
/**
* The filename to be used for the generated archive file. For the source:jar goal, "-sources" is appended to this
* filename. For the source:test-jar goal, "-test-sources" is appended.
*/
@Parameter( defaultValue = "${project.build.finalName}" )
protected String finalName;
/**
* Contains the full list of projects in the reactor.
*/
@Parameter( defaultValue = "${reactorProjects}", readonly = true )
protected List<MavenProject> reactorProjects;
/**
* Whether creating the archive should be forced. If set to true, the jar will always be created. If set to false,
* the jar will only be created when the sources are newer than the jar.
*
* @since 2.1
*/
@Parameter( property = "maven.source.forceCreation", defaultValue = "false" )
private boolean forceCreation;
/**
* A flag used to disable the source procedure. This is primarily intended for usage from the command line to
* occasionally adjust the build.
*
* @since 2.2
*/
@Parameter( property = "maven.source.skip", defaultValue = "false" )
private boolean skipSource;
/**
* The Maven session.
*/
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;
/**
* Value like SOURCE_DATE_EPOCH as <a href="https://reproducible-builds.org/specs/source-date-epoch/">defined in
* Reproducible Builds</a>: a UNIX timestamp, defined as the number of seconds, excluding leap seconds, since 01 Jan
* 1970 00:00:00 UTC.
*/
@Parameter( name = "source-date-epoch" )
private int sourceDateEpoch;
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public void execute()
throws MojoExecutionException
{
if ( skipSource )
{
getLog().info( "Skipping source per configuration." );
return;
}
packageSources( project );
}
// ----------------------------------------------------------------------
// Protected methods
// ----------------------------------------------------------------------
/**
* @return the wanted classifier, ie <code>sources</code> or <code>test-sources</code>
*/
protected abstract String getClassifier();
/**
* @param p {@link MavenProject} not null
* @return the compile or test sources
* @throws MojoExecutionException in case of an error.
*/
protected abstract List<String> getSources( MavenProject p )
throws MojoExecutionException;
/**
* @param p {@link MavenProject} not null
* @return the compile or test resources
* @throws MojoExecutionException in case of an error.
*/
protected abstract List<Resource> getResources( MavenProject p )
throws MojoExecutionException;
/**
* @param p {@link MavenProject}
* @throws MojoExecutionException in case of an error.
*/
protected void packageSources( MavenProject p )
throws MojoExecutionException
{
if ( !"pom".equals( p.getPackaging() ) )
{
packageSources( Arrays.asList( p ) );
}
}
/**
* @param theProjects {@link MavenProject}
* @throws MojoExecutionException in case of an error.
*/
protected void packageSources( List<MavenProject> theProjects )
throws MojoExecutionException
{
if ( project.getArtifact().getClassifier() != null )
{
getLog().warn( "NOT adding sources to artifacts with classifier as Maven only supports one classifier "
+ "per artifact. Current artifact [" + project.getArtifact().getId() + "] has a ["
+ project.getArtifact().getClassifier() + "] classifier." );
return;
}
MavenArchiver archiver = createArchiver();
if ( sourceDateEpoch != 0 )
{
// configure for Reproducible Builds based on source date epoch value
archiver.getArchiver().configureReproducible( sourceDateEpoch );
}
for ( MavenProject pItem : theProjects )
{
MavenProject subProject = getProject( pItem );
if ( "pom".equals( subProject.getPackaging() ) )
{
continue;
}
archiveProjectContent( subProject, archiver.getArchiver() );
}
if ( archiver.getArchiver().getResources().hasNext() || forceCreation )
{
if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null )
{
getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() );
archive.setManifestFile( defaultManifestFile );
}
File outputFile = new File( outputDirectory, finalName + "-" + getClassifier() + getExtension() );
try
{
archiver.setOutputFile( outputFile );
archive.setForced( forceCreation );
archiver.createArchive( session, project, archive );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
}
catch ( DependencyResolutionRequiredException e )
{
throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
}
catch ( ManifestException e )
{
throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
}
if ( attach )
{
projectHelper.attachArtifact( project, getType(), getClassifier(), outputFile );
}
else
{
getLog().info( "NOT adding java-sources to attached artifacts list." );
}
}
else
{
getLog().info( "No sources in project. Archive not created." );
}
}
/**
* @param p {@link MavenProject}
* @param archiver {@link Archiver}
* @throws MojoExecutionException in case of an error.
*/
protected void archiveProjectContent( MavenProject p, Archiver archiver )
throws MojoExecutionException
{
if ( includePom )
{
try
{
archiver.addFile( p.getFile(), p.getFile().getName() );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error adding POM file to target jar file.", e );
}
}
for ( String s : getSources( p ) )
{
File sourceDirectory = new File( s );
if ( sourceDirectory.exists() )
{
addDirectory( archiver, sourceDirectory, getCombinedIncludes( null ), getCombinedExcludes( null ) );
}
}
// MAPI: this should be taken from the resources plugin
for ( Resource resource : getResources( p ) )
{
File sourceDirectory = new File( resource.getDirectory() );
if ( !sourceDirectory.exists() )
{
continue;
}
List<String> resourceIncludes = resource.getIncludes();
String[] combinedIncludes = getCombinedIncludes( resourceIncludes );
List<String> resourceExcludes = resource.getExcludes();
String[] combinedExcludes = getCombinedExcludes( resourceExcludes );
String targetPath = resource.getTargetPath();
if ( targetPath != null )
{
if ( !targetPath.trim().endsWith( "/" ) )
{
targetPath += "/";
}
addDirectory( archiver, sourceDirectory, targetPath, combinedIncludes, combinedExcludes );
}
else
{
addDirectory( archiver, sourceDirectory, combinedIncludes, combinedExcludes );
}
}
}
/**
* @return {@link MavenArchiver}
* @throws MojoExecutionException in case of an error.
*/
protected MavenArchiver createArchiver()
throws MojoExecutionException
{
MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver( jarArchiver );
if ( project.getBuild() != null )
{
List<Resource> resources = project.getBuild().getResources();
for ( Resource r : resources )
{
if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) )
{
addDirectory( archiver.getArchiver(), new File( r.getDirectory() ), getCombinedIncludes( null ),
getCombinedExcludes( null ) );
}
}
}
return archiver;
}
/**
* @param archiver {@link Archiver}
* @param sourceDirectory {@link File}
* @param pIncludes The list of includes.
* @param pExcludes The list of excludes.
* @throws MojoExecutionException in case of an error.
*/
protected void addDirectory( Archiver archiver, File sourceDirectory, String[] pIncludes, String[] pExcludes )
throws MojoExecutionException
{
try
{
// archiver.addFileSet( fileSet );
archiver.addDirectory( sourceDirectory, pIncludes, pExcludes );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error adding directory to source archive.", e );
}
}
/**
* @param archiver {@link Archiver}
* @param sourceDirectory {@link File}
* @param prefix The prefix.
* @param pIncludes the includes.
* @param pExcludes the excludes.
* @throws MojoExecutionException in case of an error.
*/
protected void addDirectory( Archiver archiver, File sourceDirectory, String prefix, String[] pIncludes,
String[] pExcludes )
throws MojoExecutionException
{
try
{
archiver.addDirectory( sourceDirectory, prefix, pIncludes, pExcludes );
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "Error adding directory to source archive.", e );
}
}
/**
* @return The extension {@code .jar}
*/
protected String getExtension()
{
return ".jar";
}
/**
* @param p {@link MavenProject}
* @return The execution projet.
*/
protected MavenProject getProject( MavenProject p )
{
if ( p.getExecutionProject() != null )
{
return p.getExecutionProject();
}
return p;
}
/**
* @return The type {@code java-source}
*/
protected String getType()
{
return "java-source";
}
/**
* Combines the includes parameter and additional includes. Defaults to {@link #DEFAULT_INCLUDES} If the
* additionalIncludes parameter is null, it is not added to the combined includes.
*
* @param additionalIncludes The includes specified in the pom resources section
* @return The combined array of includes.
*/
private String[] getCombinedIncludes( List<String> additionalIncludes )
{
List<String> combinedIncludes = new ArrayList<String>();
if ( includes != null && includes.length > 0 )
{
combinedIncludes.addAll( Arrays.asList( includes ) );
}
if ( additionalIncludes != null && additionalIncludes.size() > 0 )
{
combinedIncludes.addAll( additionalIncludes );
}
// If there are no other includes, use the default.
if ( combinedIncludes.size() == 0 )
{
combinedIncludes.addAll( Arrays.asList( DEFAULT_INCLUDES ) );
}
return combinedIncludes.toArray( new String[combinedIncludes.size()] );
}
/**
* Combines the user parameter {@link #excludes}, the default excludes from plexus FileUtils, and the contents of
* the parameter addionalExcludes.
*
* @param additionalExcludes Additional excludes to add to the array
* @return The combined list of excludes.
*/
private String[] getCombinedExcludes( List<String> additionalExcludes )
{
List<String> combinedExcludes = new ArrayList<String>();
if ( useDefaultExcludes )
{
combinedExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
}
if ( excludes != null && excludes.length > 0 )
{
combinedExcludes.addAll( Arrays.asList( excludes ) );
}
if ( additionalExcludes != null && additionalExcludes.size() > 0 )
{
combinedExcludes.addAll( additionalExcludes );
}
if ( combinedExcludes.size() == 0 )
{
combinedExcludes.addAll( Arrays.asList( DEFAULT_EXCLUDES ) );
}
return combinedExcludes.toArray( new String[combinedExcludes.size()] );
}
/**
* @return The current project.
*/
protected MavenProject getProject()
{
return project;
}
/**
* @param project {@link MavenProject}
*/
protected void setProject( MavenProject project )
{
this.project = project;
}
}