/
DefaultThirdPartyHelper.java
347 lines (293 loc) · 11.8 KB
/
DefaultThirdPartyHelper.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
package org.codehaus.mojo.license.api;
/*
* #%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.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.SortedProperties;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import org.eclipse.aether.repository.RemoteRepository;
/**
* Default implementation of the {@link org.codehaus.mojo.license.api.ThirdPartyHelper}.
*
* @author tchemit dev@tchemit.fr
* @since 1.1
*/
public class DefaultThirdPartyHelper
implements ThirdPartyHelper
{
/**
* DependenciesTool to load dependencies.
*
* @see DependenciesTool
*/
private final DependenciesTool dependenciesTool;
/**
* ThirdPartyTool to load third-parties descriptors.
*
* @see ThirdPartyTool
*/
private final ThirdPartyTool thirdPartyTool;
/**
* List of remote repositories. Same as remoteRepositories, just a different API.
*/
private final List<ArtifactRepository> remoteRepositoriesCoreApi;
/**
* List of remote repositories.
*/
private final List<RemoteRepository> remoteRepositories;
/**
* Current maven project.
*/
private final MavenProject project;
/**
* Encoding used to read and write files.
*/
private final String encoding;
/**
* Verbose flag.
*/
private final boolean verbose;
/**
* Instance logger.
*/
private final Log log;
/**
* Cache of dependencies (as maven project) loaded.
*/
private static SortedMap<String, MavenProject> artifactCache;
/**
* Constructor of the helper.
*
* @param project Current maven project
* @param encoding Encoding used to read and write files
* @param verbose Verbose flag
* @param dependenciesTool tool to load dependencies
* @param thirdPartyTool tool to load third-parties descriptors
* @param remoteRepositoriesCoreApi maven remote repositories, in the core api format
* @param remoteRepositories maven remote repositories
* @param log logger
*/
// CHECKSTYLE_OFF: ParameterNumber
public DefaultThirdPartyHelper( MavenProject project, String encoding, boolean verbose,
DependenciesTool dependenciesTool, ThirdPartyTool thirdPartyTool,
List<ArtifactRepository> remoteRepositoriesCoreApi,
List<RemoteRepository> remoteRepositories, Log log )
{
// CHECKSTYLE_ON: ParameterNumber
this.project = project;
this.encoding = encoding;
this.verbose = verbose;
this.dependenciesTool = dependenciesTool;
this.thirdPartyTool = thirdPartyTool;
this.remoteRepositoriesCoreApi = remoteRepositoriesCoreApi;
this.remoteRepositories = remoteRepositories;
this.log = log;
this.thirdPartyTool.setVerbose( verbose );
}
/**
* {@inheritDoc}
*/
public SortedMap<String, MavenProject> getArtifactCache()
{
if ( artifactCache == null )
{
artifactCache = new TreeMap<>();
}
return artifactCache;
}
/**
* {@inheritDoc}
*/
public SortedMap<String, MavenProject> loadDependencies( MavenProjectDependenciesConfigurator configuration,
ResolvedProjectDependencies dependencyArtifacts )
{
return dependenciesTool.loadProjectDependencies( dependencyArtifacts, configuration,
remoteRepositoriesCoreApi, getArtifactCache() );
}
/**
* {@inheritDoc}
*/
public SortedProperties loadThirdPartyDescriptorForUnsafeMapping( Set<Artifact> topLevelDependencies,
SortedSet<MavenProject> unsafeDependencies,
Collection<MavenProject> projects,
LicenseMap licenseMap )
throws ThirdPartyToolException, IOException
{
return thirdPartyTool.loadThirdPartyDescriptorsForUnsafeMapping( topLevelDependencies, encoding, projects,
unsafeDependencies, licenseMap, remoteRepositories );
}
/**
* {@inheritDoc}
*/
public SortedProperties loadUnsafeMapping( LicenseMap licenseMap, File missingFile, String missingFileUrl,
SortedMap<String, MavenProject> projectDependencies )
throws IOException, MojoExecutionException
{
return thirdPartyTool.loadUnsafeMapping( licenseMap, projectDependencies, encoding, missingFile,
missingFileUrl );
}
/**
* {@inheritDoc}
*/
public LicenseMap createLicenseMap( SortedMap<String, MavenProject> dependencies )
{
LicenseMap licenseMap = new LicenseMap();
for ( MavenProject project : dependencies.values() )
{
thirdPartyTool.addLicense( licenseMap, project, project.getLicenses() );
}
return licenseMap;
}
/**
* {@inheritDoc}
*/
public void attachThirdPartyDescriptor( File file )
{
thirdPartyTool.attachThirdPartyDescriptor( project, file );
}
/**
* {@inheritDoc}
*/
public SortedSet<MavenProject> getProjectsWithNoLicense( LicenseMap licenseMap )
{
return thirdPartyTool.getProjectsWithNoLicense( licenseMap, verbose );
}
/**
* {@inheritDoc}
*/
public SortedProperties createUnsafeMapping( LicenseMap licenseMap, File missingFile, String missingFileUrl,
boolean useRepositoryMissingFiles,
SortedSet<MavenProject> unsafeDependencies,
SortedMap<String, MavenProject> projectDependencies,
Set<Artifact> dependencyArtifacts )
throws ProjectBuildingException, IOException, ThirdPartyToolException, MojoExecutionException
{
SortedProperties unsafeMappings = loadUnsafeMapping( licenseMap, missingFile, missingFileUrl,
projectDependencies );
if ( CollectionUtils.isNotEmpty( unsafeDependencies ) )
{
// there is some unresolved license
if ( useRepositoryMissingFiles )
{
// try to load missing third party files from dependencies
Collection<MavenProject> projects = new ArrayList<>( projectDependencies.values() );
projects.remove( project );
projects.removeAll( unsafeDependencies );
SortedProperties resolvedUnsafeMapping =
loadThirdPartyDescriptorForUnsafeMapping( dependencyArtifacts, unsafeDependencies, projects,
licenseMap );
// push back resolved unsafe mappings (only for project dependencies)
for ( Object coord : resolvedUnsafeMapping.keySet() )
{
String s = (String) coord;
if ( projectDependencies.containsKey( s ) )
{
unsafeMappings.put( s, resolvedUnsafeMapping.get( s ) );
}
}
}
}
return unsafeMappings;
}
/**
* {@inheritDoc}
*/
public void mergeLicenses( List<String> licenseMerges, LicenseMap licenseMap )
throws MojoFailureException
{
Set<String> licenseFound = new HashSet<>();
if ( !CollectionUtils.isEmpty( licenseMerges ) )
{
// check where is not multi licenses merged main licenses (see MLICENSE-23)
Map<String, Set<String>> mergedLicenses = new HashMap<>();
for ( String merge : licenseMerges )
{
merge = merge.trim();
String[] split = merge.split( "\\s*\\|\\s*" );
String mainLicense = split[0];
Set<String> mergeList;
if ( mergedLicenses.containsKey( mainLicense ) )
{
mergeList = mergedLicenses.get( mainLicense );
}
else
{
mergeList = new HashSet<>();
}
for ( int i = 0; i < split.length; i++ )
{
String licenseToAdd = split[i];
if ( i == 0 )
{
// mainLicense will not be merged (to itself)
continue;
}
// check license not already described to be merged
if ( mergeList.contains( licenseToAdd ) || licenseFound.contains( licenseToAdd )
|| mainLicense.equals( licenseToAdd ) )
{
// this license to merge was already described, fail the build...
throw new MojoFailureException(
"The license " + licenseToAdd + " was already registered in the "
+ "configuration, please use only one such entry as described in the example "
+ "http://mojohaus.org/license-maven-plugin/examples/"
+ "example-thirdparty.html#Merge_licenses" );
}
// can add this license for merge
mergeList.add( licenseToAdd );
licenseFound.add( licenseToAdd );
}
// push back licenses to merge for this main license
mergedLicenses.put( mainLicense, mergeList );
}
// merge licenses in license map
for ( Map.Entry<String, Set<String>> entry : mergedLicenses.entrySet() )
{
String mainLicense = entry.getKey();
Set<String> mergedLicense = entry.getValue();
if ( verbose )
{
log.info( "Will merge to *" + mainLicense + "*, licenses: " + mergedLicense );
}
thirdPartyTool.mergeLicenses( licenseMap, mainLicense, mergedLicense );
}
}
}
}