/
AbstractThirdPartyReportMojo.java
658 lines (582 loc) · 21.1 KB
/
AbstractThirdPartyReportMojo.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package org.codehaus.mojo.license;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2012 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
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.ProjectBuildingException;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.mojo.license.api.ArtifactFilters;
import org.codehaus.mojo.license.api.DefaultThirdPartyDetails;
import org.codehaus.mojo.license.api.DefaultThirdPartyHelper;
import org.codehaus.mojo.license.api.DependenciesTool;
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ThirdPartyDetails;
import org.codehaus.mojo.license.api.ThirdPartyHelper;
import org.codehaus.mojo.license.api.ThirdPartyTool;
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.mojo.license.utils.UrlRequester;
import org.codehaus.plexus.i18n.I18N;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.eclipse.aether.RepositorySystemSession;
/**
* Base class for third-party reports.
*
* @author tchemit dev@tchemit.fr
* @since 1.1
*/
public abstract class AbstractThirdPartyReportMojo extends AbstractMavenReport
implements MavenProjectDependenciesConfigurator
{
// ----------------------------------------------------------------------
// Mojo Parameters
// ----------------------------------------------------------------------
/**
* A filter to exclude some scopes.
*
* @since 1.1
*/
@Parameter( property = "license.excludedScopes", defaultValue = "system" )
private String excludedScopes;
/**
* A filter to include only some scopes, if let empty then all scopes will be used (no filter).
*
* @since 1.1
*/
@Parameter( property = "license.includedScopes" )
private String includedScopes;
/**
* A filter to exclude some types.
*
* @since 1.15
*/
@Parameter( property = "license.excludedTypes" )
private String excludedTypes;
/**
* A filter to include only some types, if let empty then all types will be used (no filter).
*
* @since 1.15
*/
@Parameter( property = "license.includedTypes" )
private String includedTypes;
/**
* A filter to exclude some GroupIds
*
* @since 1.1
*/
@Parameter( property = "license.excludedGroups" )
private String excludedGroups;
/**
* A filter to include only some GroupIds
*
* @since 1.1
*/
@Parameter( property = "license.includedGroups" )
private String includedGroups;
/**
* A filter to exclude some ArtifactsIds
*
* @since 1.1
*/
@Parameter( property = "license.excludedArtifacts" )
private String excludedArtifacts;
/**
* A filter to include only some ArtifactsIds
*
* @since 1.1
*/
@Parameter( property = "license.includedArtifacts" )
private String includedArtifacts;
/**
* Include transitive dependencies when looking for missing licenses and downloading license files.
*
* @since 1.1
*/
@Parameter( property = "license.includeTransitiveDependencies", defaultValue = "true" )
private boolean includeTransitiveDependencies;
/**
* A filter to exclude transitive dependencies from excluded artifacts.
*
* @since 1.13
*/
@Parameter( property = "license.excludeTransitiveDependencies", defaultValue = "false" )
private boolean excludeTransitiveDependencies;
/**
* If {@code true} both optional and non-optional dependencies will be included in the list of artifacts for
* creating the license report; otherwise only non-optional dependencies will be considered.
*
* @since 1.19
*/
@Parameter( property = "license.includeOptional", defaultValue = "true" )
boolean includeOptional;
/**
* A flag to use the missing licenses file to consolidate the THID-PARTY file.
*
* @since 1.1
*/
@Parameter( property = "license.useMissingFile", defaultValue = "false" )
private boolean useMissingFile;
/**
* The file where to fill the license for dependencies with unknwon license.
*
* @since 1.1
*/
@Parameter( property = "license.missingFile", defaultValue = "src/license/THIRD-PARTY.properties" )
private File missingFile;
/**
* The Url that holds the missing license dependency entries. This is an extension to {@link #missingFile}.
* If set then the entries that will be found at this URL will be added additionally to the entries of the
* missing file.<br>
* <br>
* <b>NOTE:</b><br>
* the response of the URL endpoint must return content that matches the THIRD-PARTY.properties file!
*
* @since 1.15
*/
@Parameter( property = "license.missingFileUrl" )
String missingFileUrl;
/**
* A file containing the override license information for dependencies.
* <b>Note:</b> Specify either {@link #overrideUrl} (preferred) or {@link #overrideFile}.
* If none of these is specified, then {@value LicenseMojoUtils#DEFAULT_OVERRIDE_THIRD_PARTY} resolved against
* <code>${basedir}</code> will be used if it exists.
*
* @since 1.11
* @deprecated Use {@link #overrideUrl} instead
*/
@Deprecated
@Parameter( property = "license.overrideFile" )
private File overrideFile;
/**
* A URL pointing at a property file with the override license information for dependencies.
* <b>Note:</b> Specify either {@link #overrideUrl} (preferred) or {@link #overrideFile}.
* If none of these is specified, then {@value LicenseMojoUtils#DEFAULT_OVERRIDE_THIRD_PARTY} resolved against
* <code>${basedir}</code> will be used if it exists.
* <p>
* An example of the file content:
* <pre>
* org.jboss.xnio--xnio-api--3.3.6.Final=The Apache Software License, Version 2.0
* </pre>
*
* @since 1.17
*/
@Parameter( property = "license.overrideUrl" )
private String overrideUrl;
/**
* A {@link URL} prepared either our of {@link #overrideFile} or {@link #overrideUrl} or the default value.
*
* @see LicenseMojoUtils#prepareThirdPartyOverrideUrl(URL, File, String, File)
*/
String resolvedOverrideUrl;
/**
* Load from repositories third party missing files.
*
* @since 1.0
*/
@Parameter( property = "license.useRepositoryMissingFiles", defaultValue = "true" )
private boolean useRepositoryMissingFiles;
/**
* To merge licenses in final file.
* <p>
* Each entry represents a merge (first license is main license to keep), licenses are separated by {@code |}.
* <p>
* Example :
* <p>
* <pre>
* <licenseMerges>
* <licenseMerge>The Apache Software License|Version 2.0,Apache License, Version 2.0</licenseMerge>
* </licenseMerges>
* </pre>
*
* <b>Note:</b> This option will be overridden by {@link #licenseMergesUrl} if it is used by command line.
* @since 1.0
*/
@Parameter
private List<String> licenseMerges;
/**
* Location of file with the merge licenses in order to be used by command line.
* <b>Note:</b> This option overrides {@link #licenseMerges}.
*
* @since 1.18
*/
@Parameter( property = "license.licenseMergesUrl" )
protected String licenseMergesUrl;
/**
* The output directory for the report. Note that this parameter is only evaluated if the goal is run directly from
* the command line. If the goal is run indirectly as part of a site generation, the output directory configured in
* the Maven Site Plugin is used instead.
*
* @since 1.1
*/
@Parameter( defaultValue = "${project.reporting.outputDirectory}", required = true )
private File outputDirectory;
/**
* Flag to activate verbose mode.
* <p>
* <b>Note:</b> Verbose mode is always on if you starts a debug maven instance
* (says via {@code -X}).
*
* @since 1.0
*/
@Parameter( property = "license.verbose", defaultValue = "${maven.verbose}" )
private boolean verbose;
/**
* Encoding used to read and writes files.
* <p>
* <b>Note:</b> If nothing is filled here, we will use the system
* property {@code file.encoding}.
*
* @since 1.0
*/
@Parameter( property = "license.encoding", defaultValue = "${project.build.sourceEncoding}" )
private String encoding;
/**
* The Maven Project.
*
* @since 1.1
*/
@Parameter( defaultValue = "${project}", readonly = true )
private MavenProject project;
// ----------------------------------------------------------------------
// Plexus Components
// ----------------------------------------------------------------------
/**
* Doxia Site Renderer component.
*
* @since 1.1
*/
@Component
private Renderer siteRenderer;
/**
* Internationalization component.
*
* @since 1.1
*/
@Component
private I18N i18n;
/**
* dependencies tool.
*
* @since 1.1
*/
@Component
private DependenciesTool dependenciesTool;
/**
* third party tool.
*
* @since 1.1
*/
@Component
private ThirdPartyTool thirdPartyTool;
/**
* Repository system session used by Aether.
*/
@Parameter( defaultValue = "${repositorySystemSession}", required = true, readonly = true )
private RepositorySystemSession aetherRepoSession;
/**
* A URL returning a plain text file that contains include/exclude artifact filters in the following format:
* <pre>
* {@code
* # this is a comment
* include gaPattern org\.my-org:my-artifact
* include gaPattern org\.other-org:other-artifact
* exclude gaPattern org\.yet-anther-org:.*
* include scope compile
* include scope test
* exclude scope system
* include type jar
* exclude type war
* }</pre>
*
* @since 1.18
*/
@Parameter( property = "license.artifactFiltersUrl" )
private String artifactFiltersUrl;
private ArtifactFilters artifactFilters;
// ----------------------------------------------------------------------
// Protected Abstract Methods
// ----------------------------------------------------------------------
protected abstract Collection<ThirdPartyDetails> createThirdPartyDetails()
throws IOException, ThirdPartyToolException, ProjectBuildingException, MojoFailureException,
InvalidDependencyVersionException, ArtifactNotFoundException, ArtifactResolutionException,
DependenciesToolException, MojoExecutionException;
// ----------------------------------------------------------------------
// AbstractMavenReport Implementation
// ----------------------------------------------------------------------
/**
* Method to initialize the mojo before doing any concrete actions.
*
* <b>Note:</b> The method is invoked before the {@link #executeReport()} method.
* @throws IOException
*/
protected void init()
throws IOException
{
if ( licenseMergesUrl != null )
{
getLog().warn( "" );
getLog().warn( "licenseMerges will be overridden by licenseMergesUrl." );
getLog().warn( "" );
if ( UrlRequester.isStringUrl( licenseMergesUrl ) )
{
licenseMerges = Arrays.asList( UrlRequester.getFromUrl( licenseMergesUrl ).split( "[\n\r]+" ) );
}
}
}
/**
* {@inheritDoc}
*/
protected void executeReport( Locale locale )
throws MavenReportException
{
resolvedOverrideUrl = LicenseMojoUtils.prepareThirdPartyOverrideUrl( resolvedOverrideUrl, overrideFile,
overrideUrl, project.getBasedir(), getLog() );
Collection<ThirdPartyDetails> details;
try
{
init();
details = createThirdPartyDetails();
}
catch ( IOException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( ThirdPartyToolException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( ProjectBuildingException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( ArtifactNotFoundException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( ArtifactResolutionException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( MojoFailureException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( DependenciesToolException e )
{
throw new MavenReportException( e.getMessage(), e );
}
catch ( MojoExecutionException e )
{
throw new MavenReportException( e.getMessage(), e );
}
ThirdPartyReportRenderer renderer =
new ThirdPartyReportRenderer( getSink(), i18n, getOutputName(), locale, details );
renderer.render();
}
/**
* {@inheritDoc}
*/
protected MavenProject getProject()
{
return project;
}
/**
* {@inheritDoc}
*/
protected String getOutputDirectory()
{
if ( !outputDirectory.isAbsolute() )
{
outputDirectory = new File( project.getBasedir(), outputDirectory.getPath() );
}
return outputDirectory.getAbsolutePath();
}
/**
* {@inheritDoc}
*/
protected Renderer getSiteRenderer()
{
return siteRenderer;
}
/**
* {@inheritDoc}
*/
public String getDescription( Locale locale )
{
return i18n.getString( getOutputName(), locale, "report.description" );
}
/**
* {@inheritDoc}
*/
public String getName( Locale locale )
{
return i18n.getString( getOutputName(), locale, "report.title" );
}
// ----------------------------------------------------------------------
// MavenProjectDependenciesConfigurator Implementation
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
public boolean isIncludeTransitiveDependencies()
{
return includeTransitiveDependencies;
}
/**
* {@inheritDoc}
*/
public boolean isExcludeTransitiveDependencies()
{
return excludeTransitiveDependencies;
}
/** {@inheritDoc} */
public ArtifactFilters getArtifactFilters()
{
if ( artifactFilters == null )
{
artifactFilters = ArtifactFilters.of( includedGroups, excludedGroups, includedArtifacts, excludedArtifacts,
includedScopes, excludedScopes, includedTypes, excludedTypes,
includeOptional, artifactFiltersUrl , getEncoding() );
}
return artifactFilters;
}
/**
* {@inheritDoc}
*/
public boolean isVerbose()
{
return verbose;
}
// ----------------------------------------------------------------------
// Protected Methods
// ----------------------------------------------------------------------
Collection<ThirdPartyDetails> createThirdPartyDetails( MavenProject project, boolean loadArtifacts )
throws IOException, ThirdPartyToolException, ProjectBuildingException, MojoFailureException,
DependenciesToolException, MojoExecutionException
{
ResolvedProjectDependencies loadedDependencies;
if ( loadArtifacts )
{
loadedDependencies =
new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() );
}
else
{
loadedDependencies = new ResolvedProjectDependencies( getProject().getArtifacts(),
getProject().getDependencyArtifacts() );
}
ThirdPartyHelper thirdPartyHelper =
new DefaultThirdPartyHelper( project, encoding, verbose,
dependenciesTool, thirdPartyTool,
project.getRemoteArtifactRepositories(), project.getRemoteProjectRepositories(), getLog() );
// load dependencies of the project
SortedMap<String, MavenProject> projectDependencies = thirdPartyHelper.loadDependencies( this,
loadedDependencies );
// create licenseMap from it
LicenseMap licenseMap = thirdPartyHelper.createLicenseMap( projectDependencies );
// Get unsafe dependencies (dependencies with no license in pom)
SortedSet<MavenProject> dependenciesWithNoLicense = thirdPartyHelper.getProjectsWithNoLicense( licenseMap );
// compute safe dependencies (with pom licenses)
Set<MavenProject> dependenciesWithPomLicense =
new TreeSet<>( MojoHelper.newMavenProjectComparator() );
dependenciesWithPomLicense.addAll( projectDependencies.values() );
if ( CollectionUtils.isNotEmpty( dependenciesWithNoLicense ) )
{
// there is some unsafe dependencies, remove them from safe dependencies
dependenciesWithPomLicense.removeAll( dependenciesWithNoLicense );
if ( useMissingFile )
{
// Resolve unsafe dependencies using missing files, this will update licenseMap and unsafeDependencies
thirdPartyHelper.createUnsafeMapping( licenseMap, missingFile, missingFileUrl,
useRepositoryMissingFiles, dependenciesWithNoLicense,
projectDependencies, loadedDependencies.getAllDependencies() );
}
}
// LicenseMap is now complete, let's merge licenses if necessary
thirdPartyHelper.mergeLicenses( licenseMerges, licenseMap );
// Add override licenses
thirdPartyTool.overrideLicenses( licenseMap, projectDependencies, encoding, resolvedOverrideUrl );
// let's build third party details for each dependencies
Collection<ThirdPartyDetails> details = new ArrayList<>();
for ( Map.Entry<MavenProject, String[]> entry : licenseMap.toDependencyMap().entrySet() )
{
MavenProject dependency = entry.getKey();
String[] licenses = entry.getValue();
ThirdPartyDetails detail = new DefaultThirdPartyDetails( dependency );
details.add( detail );
if ( dependenciesWithPomLicense.contains( dependency ) )
{
// this is a pom licenses
detail.setPomLicenses( licenses );
}
else if ( !dependenciesWithNoLicense.contains( dependency ) )
{
// this is a third-party licenses
detail.setThirdPartyLicenses( licenses );
}
}
return details;
}
/** {@inheritDoc} */
public String getArtifactFiltersUrl()
{
return artifactFiltersUrl;
}
/** {@inheritDoc} */
public String getEncoding()
{
return encoding;
}
/**
* Set repo session in the components that need it.
* @param session The repository system session
*/
public void setAetherRepoSession( RepositorySystemSession session )
{
aetherRepoSession = session;
dependenciesTool.setAetherRepoSession( aetherRepoSession );
thirdPartyTool.setAetherRepoSession( aetherRepoSession );
}
}