/
AddThirdPartyMojo.java
471 lines (406 loc) · 16.4 KB
/
AddThirdPartyMojo.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
package org.codehaus.mojo.license;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.api.ArtifactFilters;
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.FileUtil;
import org.codehaus.mojo.license.utils.SortedProperties;
// CHECKSTYLE_OFF: LineLength
/**
* Goal to generate the third-party license file.
* <p>
* This file contains a list of the dependencies and their licenses. Each dependency and its
* license is displayed on a single line in the format
* <pre>
* (<license-name>) <project-name> <groupId>:<artifactId>:<version> - <project-url>
* </pre>
* The directory containing the license database file is added to the classpath as an additional resource.
*
* @author tchemit dev@tchemit.fr
* @since 1.0
*/
// CHECKSTYLE_ON: LineLength
@Mojo( name = "add-third-party", requiresDependencyResolution = ResolutionScope.TEST,
defaultPhase = LifecyclePhase.GENERATE_RESOURCES )
public class AddThirdPartyMojo extends AbstractAddThirdPartyMojo implements MavenProjectDependenciesConfigurator
{
// ----------------------------------------------------------------------
// Mojo Parameters
// ----------------------------------------------------------------------
/**
* To skip execution of this mojo.
*
* @since 1.5
*/
@Parameter( property = "license.skipAddThirdParty", defaultValue = "false" )
private boolean skipAddThirdParty;
// ----------------------------------------------------------------------
// Private Fields
// ----------------------------------------------------------------------
/**
* Internal flag to know if missing file must be generated.
*/
private boolean doGenerateMissing;
/**
* Whether this is an aggregate build, or a single-project goal. This setting determines which dependency artifacts
* will be examined by the plugin. AddThirdParty needs to load dependencies only for the single project it is run
* for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*/
private boolean isAggregatorBuild = false;
/**
* The reactor projects. When resolving dependencies, the aggregator goal needs to do custom handling
* of sibling dependencies for projects in the reactor,
* to avoid trying to load artifacts for projects that haven't been built/published yet.
*/
private List<MavenProject> reactorProjectDependencies;
/**
* Copies of the project's dependency sets. AddThirdParty needs to load dependencies only for the single project it
* is run for, while AggregateAddThirdParty needs to load dependencies for the parent project, as well as all child
* projects in the reactor.
*
* In cases where one child project A in a reactor depends on another project B in the same reactor,
* B is not necessarily built/published. The plugin needs to resolve B's dependencies manually.
* This field stores the result of that manual resolution.
*/
private ResolvedProjectDependencies dependencyArtifacts;
private ArtifactFilters artifactFilters;
// ----------------------------------------------------------------------
// AbstractLicenseMojo Implementaton
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public boolean isSkip()
{
return skipAddThirdParty;
}
/**
* {@inheritDoc}
*/
@Override
protected boolean checkPackaging()
{
if ( acceptPomPackaging )
{
// rejects nothing
return true;
}
// can reject pom packaging
return rejectPackaging( "pom" );
}
/**
* {@inheritDoc}
*/
@Override
protected boolean checkSkip()
{
if ( !doGenerate && !doGenerateBundle && !doGenerateMissing )
{
getLog().info( "All files are up to date, skip goal execution." );
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
protected void doAction()
throws Exception
{
consolidate();
checkUnsafeDependencies();
boolean safeLicense = checkForbiddenLicenses();
checkBlacklist( safeLicense );
writeThirdPartyFile();
if ( doGenerateMissing )
{
writeMissingFile();
}
boolean unsafe = CollectionUtils.isNotEmpty( unsafeDependencies );
checkMissing( unsafe );
if ( !unsafe && useMissingFile && MapUtils.isEmpty( unsafeMappings ) && missingFile.exists() )
{
// there is no missing dependencies, but still a missing file, delete it
getLog().info( "There is no dependency to put in missing file, delete it at " + missingFile );
FileUtil.deleteFile( missingFile );
}
if ( !unsafe && deployMissingFile && MapUtils.isNotEmpty( unsafeMappings ) )
{
// can deploy missing file
getLog().info( "Will attach third party file from " + missingFile );
getHelper().attachThirdPartyDescriptor( missingFile );
}
addResourceDir( outputDirectory, "**/*.txt" );
}
// ----------------------------------------------------------------------
// AbstractAddThirdPartyMojo Implementation
// ----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
protected SortedMap<String, MavenProject> loadDependencies() throws DependenciesToolException
{
return getHelper().loadDependencies( this, resolveDependencyArtifacts() );
}
/**
* Resolves the transitive and direct dependency sets for this project.
*
* @return The set of all dependencies, and the set of only direct dependency artifacts.
* @throws org.codehaus.mojo.license.api.DependenciesToolException if the dependencies could not be resolved
*/
protected ResolvedProjectDependencies resolveDependencyArtifacts() throws DependenciesToolException
{
if ( dependencyArtifacts != null )
{
return dependencyArtifacts;
}
if ( isAggregatorBuild )
{
dependencyArtifacts =
new ResolvedProjectDependencies( project.getArtifacts(), project.getDependencyArtifacts() );
}
else
{
dependencyArtifacts = new ResolvedProjectDependencies( project.getArtifacts(),
project.getDependencyArtifacts() );
}
return dependencyArtifacts;
}
/**
* {@inheritDoc}
*/
@Override
protected SortedProperties createUnsafeMapping()
throws ProjectBuildingException, IOException, ThirdPartyToolException,
MojoExecutionException, DependenciesToolException
{
SortedProperties unsafeMappings =
getHelper().createUnsafeMapping( licenseMap, missingFile, missingFileUrl,
useRepositoryMissingFiles, unsafeDependencies,
projectDependencies,
resolveDependencyArtifacts().getAllDependencies() );
if ( isVerbose() )
{
getLog().info( "found " + unsafeMappings.size() + " unsafe mappings" );
}
// compute if missing file should be (re)-generate
doGenerateMissing = computeDoGenerateMissingFile( unsafeMappings, unsafeDependencies );
if ( doGenerateMissing && isVerbose() )
{
StringBuilder sb = new StringBuilder();
sb.append( "Will use from missing file " );
sb.append( unsafeMappings.size() );
sb.append( " dependencies :" );
for ( Map.Entry<Object, Object> entry : unsafeMappings.entrySet() )
{
String id = (String) entry.getKey();
String license = (String) entry.getValue();
sb.append( "\n - " ).append( id ).append( " - " ).append( license );
}
getLog().info( sb.toString() );
}
else
{
if ( useMissingFile && !unsafeMappings.isEmpty() )
{
getLog().info( "Missing file " + missingFile + " is up-to-date." );
}
}
return unsafeMappings;
}
// ----------------------------------------------------------------------
// MavenProjectDependenciesConfigurator Implementaton
// ----------------------------------------------------------------------
/**
* {@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;
}
// ----------------------------------------------------------------------
// Private Methods
// ----------------------------------------------------------------------
/**
* @param unsafeMappings the unsafe mapping coming from the missing file
* @param unsafeDependencies the unsafe dependencies from the project
* @return {@code true} if missing ifle should be (re-)generated, {@code false} otherwise
* @throws IOException if any IO problem
* @since 1.0
*/
private boolean computeDoGenerateMissingFile( SortedProperties unsafeMappings,
SortedSet<MavenProject> unsafeDependencies ) throws IOException
{
if ( !useMissingFile )
{
// never use the missing file
return false;
}
if ( force )
{
// the mapping for missing file is not empty, regenerate it
return !CollectionUtils.isEmpty( unsafeMappings.keySet() );
}
if ( !CollectionUtils.isEmpty( unsafeDependencies ) )
{
// there is some unsafe dependencies from the project, must
// regenerate missing file
return true;
}
if ( !missingFile.exists() )
{
// the missing file does not exists, this happens when
// using remote missing file from dependencies
return true;
}
// check if the missing file has changed
SortedProperties oldUnsafeMappings = new SortedProperties( getEncoding() );
oldUnsafeMappings.load( missingFile );
return !unsafeMappings.equals( oldUnsafeMappings );
}
/**
* Write the missing file ({@link #getMissingFile()}.
*
* @throws IOException if error while writing missing file
*/
private void writeMissingFile()
throws IOException
{
Log log = getLog();
FileUtil.createDirectoryIfNecessary( missingFile.getParentFile() );
log.info( "Regenerate missing license file " + missingFile );
FileOutputStream writer = new FileOutputStream( missingFile );
try
{
StringBuilder sb = new StringBuilder( " Generated by " + getClass().getName() );
List<String> licenses = new ArrayList<>( licenseMap.keySet() );
licenses.remove( LicenseMap.UNKNOWN_LICENSE_MESSAGE );
if ( !licenses.isEmpty() )
{
sb.append( "\n-------------------------------------------------------------------------------" );
sb.append( "\n Already used licenses in project :" );
for ( String license : licenses )
{
sb.append( "\n - " ).append( license );
}
}
sb.append( "\n-------------------------------------------------------------------------------" );
sb.append( "\n Please fill the missing licenses for dependencies :\n\n" );
unsafeMappings.store( writer, sb.toString() );
}
finally
{
writer.close();
}
}
void initFromMojo( AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject,
List<MavenProject> reactorProjects ) throws Exception
{
project = mavenProject;
deployMissingFile = mojo.deployMissingFile;
useRepositoryMissingFiles = mojo.useRepositoryMissingFiles;
acceptPomPackaging = mojo.acceptPomPackaging;
includeOptional = mojo.includeOptional;
excludedScopes = mojo.excludedScopes;
includedScopes = mojo.includedScopes;
excludedGroups = mojo.excludedGroups;
includedGroups = mojo.includedGroups;
excludedArtifacts = mojo.excludedArtifacts;
includedArtifacts = mojo.includedArtifacts;
includeTransitiveDependencies = mojo.includeTransitiveDependencies;
excludeTransitiveDependencies = mojo.excludeTransitiveDependencies;
thirdPartyFilename = mojo.thirdPartyFilename;
useMissingFile = mojo.useMissingFile;
String absolutePath = mojo.getProject().getBasedir().getAbsolutePath();
missingFile = new File( project.getBasedir(),
mojo.missingFile.getAbsolutePath().substring( absolutePath.length() ) );
resolvedOverrideUrl = mojo.resolvedOverrideUrl;
missingLicensesFileArtifact = mojo.missingLicensesFileArtifact;
localRepository = mojo.localRepository;
dependencies = new HashSet<>( mavenProject.getDependencyArtifacts() );
licenseMerges = mojo.licenseMerges;
licenseMergesFile = mojo.licenseMergesFile;
includedLicenses = mojo.includedLicenses;
excludedLicenses = mojo.excludedLicenses;
bundleThirdPartyPath = mojo.bundleThirdPartyPath;
generateBundle = mojo.generateBundle;
force = mojo.force;
failIfWarning = mojo.failIfWarning;
failOnMissing = mojo.failOnMissing;
failOnBlacklist = mojo.failOnBlacklist;
sortArtifactByName = mojo.sortArtifactByName;
fileTemplate = mojo.fileTemplate;
session = mojo.session;
verbose = mojo.verbose;
encoding = mojo.encoding;
setLog( mojo.getLog() );
isAggregatorBuild = true;
reactorProjectDependencies = reactorProjects;
init();
consolidate();
}
}