1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.license;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.SortedMap;
30 import java.util.SortedSet;
31 import java.util.TreeMap;
32
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.IOUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.maven.plugin.MojoFailureException;
38 import org.apache.maven.plugin.logging.Log;
39 import org.apache.maven.project.MavenProject;
40 import org.apache.maven.project.ProjectBuildingException;
41 import org.codehaus.mojo.license.model.LicenseMap;
42 import org.springframework.core.io.DefaultResourceLoader;
43 import org.springframework.core.io.Resource;
44 import org.springframework.core.io.ResourceLoader;
45
46
47
48
49
50
51
52 public abstract class AbstractAddThirdPartyMojo extends AbstractLicenseMojo {
53
54
55
56
57
58
59
60
61
62 protected File outputDirectory;
63
64
65
66
67
68
69
70
71 protected String thirdPartyFilename;
72
73
74
75
76
77
78
79 protected boolean useMissingFile;
80
81
82
83
84
85
86
87 protected File missingFile;
88
89
90
91
92
93
94
95
96 protected String artifactLicenseMapping;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 protected List<String> licenseMerges;
116
117
118
119
120
121
122
123
124
125
126 protected String bundleThirdPartyPath;
127
128
129
130
131
132
133
134
135
136
137 protected boolean generateBundle;
138
139
140
141
142
143
144
145 protected boolean force;
146
147
148
149
150
151
152
153 protected boolean failIfWarning;
154
155
156
157
158
159
160
161
162
163
164
165 protected boolean groupByLicense;
166
167
168
169
170
171
172
173 protected String excludedScopes;
174
175
176
177
178
179
180
181 protected String includedScopes;
182
183
184
185
186
187
188
189 protected String excludedGroups;
190
191
192
193
194
195
196
197 protected String includedGroups;
198
199
200
201
202
203
204
205 protected String excludedArtifacts;
206
207
208
209
210
211
212
213 protected String includedArtifacts;
214
215
216
217
218
219
220
221 protected boolean includeTransitiveDependencies;
222
223
224
225
226
227
228
229
230 private ThirdPartyTool thirdPartyTool;
231
232 private SortedMap<String, MavenProject> projectDependencies;
233
234 private LicenseMap licenseMap;
235
236 private SortedSet<MavenProject> unsafeDependencies;
237
238 private File thirdPartyFile;
239
240 private SortedProperties unsafeMappings;
241
242 private boolean doGenerate;
243
244 private boolean doGenerateBundle;
245
246 public static final String NO_DEPENDENCIES_MESSAGE = "the project has no dependencies.";
247
248 private static SortedMap<String, MavenProject> artifactCache;
249
250 public static SortedMap<String, MavenProject> getArtifactCache() {
251 if (artifactCache == null) {
252 artifactCache = new TreeMap<String, MavenProject>();
253 }
254 return artifactCache;
255 }
256
257 protected abstract SortedMap<String, MavenProject> loadDependencies();
258
259 protected abstract SortedProperties createUnsafeMapping() throws ProjectBuildingException, IOException,
260 ThirdPartyToolException;
261
262 protected boolean exists(String location) {
263 if (StringUtils.isBlank(location)) {
264 return false;
265 }
266 File file = new File(location);
267 if (file.exists()) {
268 return true;
269 }
270 ResourceLoader loader = new DefaultResourceLoader();
271 Resource resource = loader.getResource(location);
272 return resource.exists();
273 }
274
275 protected InputStream getInputStream(String location) throws IOException {
276 File file = new File(location);
277 if (file.exists()) {
278 return new FileInputStream(file);
279 }
280 ResourceLoader loader = new DefaultResourceLoader();
281 Resource resource = loader.getResource(location);
282 if (!resource.exists()) {
283 throw new IllegalArgumentException("Can't open an input stream for " + location);
284 } else {
285 return resource.getInputStream();
286 }
287 }
288
289 protected File copyToFileSystem(String location) {
290 File temp = new File(getProject().getBuild().getDirectory() + "/license/THIRD-PARTY.properties");
291 return copyToFileSystem(location, temp);
292 }
293
294 protected File copyToFileSystem(String location, File file) {
295 InputStream in = null;
296 OutputStream out = null;
297 try {
298 in = getInputStream(location);
299 out = FileUtils.openOutputStream(file);
300 IOUtils.copy(in, out);
301 getLog().debug("Copied " + location + " to " + file);
302 return file;
303 } catch (IOException e) {
304 throw new IllegalArgumentException(e);
305 } finally {
306 IOUtils.closeQuietly(in);
307 IOUtils.closeQuietly(out);
308 }
309 }
310
311 @Override
312 protected void init() throws Exception {
313 if (exists(getArtifactLicenseMapping())) {
314 File propertiesFile = copyToFileSystem(getArtifactLicenseMapping());
315 setMissingFile(propertiesFile);
316 }
317
318 Log log = getLog();
319
320 if (log.isDebugEnabled()) {
321
322
323 setVerbose(true);
324 }
325
326 File file = new File(getOutputDirectory(), getThirdPartyFilename());
327
328 setThirdPartyFile(file);
329
330 long buildTimestamp = getBuildTimestamp();
331
332 if (isVerbose()) {
333 log.info("Build start at : " + buildTimestamp);
334 log.info("third-party file : " + file.lastModified());
335 }
336
337 setDoGenerate(isForce() || !file.exists() || buildTimestamp > file.lastModified());
338
339 if (isGenerateBundle()) {
340
341 File bundleFile = FileUtil.getFile(getOutputDirectory(), getBundleThirdPartyPath());
342
343 if (isVerbose()) {
344 log.info("bundle third-party file : " + bundleFile.lastModified());
345 }
346 setDoGenerateBundle(isForce() || !bundleFile.exists() || buildTimestamp > bundleFile.lastModified());
347 } else {
348
349
350 setDoGenerateBundle(false);
351 }
352
353 projectDependencies = loadDependencies();
354
355 licenseMap = createLicenseMap(projectDependencies);
356
357 SortedSet<MavenProject> unsafeDependencies = getThirdPartyTool().getProjectsWithNoLicense(licenseMap,
358 isVerbose());
359
360 setUnsafeDependencies(unsafeDependencies);
361
362 if (!CollectionUtils.isEmpty(unsafeDependencies) && isUseMissingFile() && isDoGenerate()) {
363
364
365 unsafeMappings = createUnsafeMapping();
366 }
367
368 if (!CollectionUtils.isEmpty(licenseMerges)) {
369
370
371 Map<String, String[]> mergedLicenses = new HashMap<String, String[]>();
372
373 for (String merge : licenseMerges) {
374 merge = merge.trim();
375 String[] split = merge.split("\\|");
376
377 String mainLicense = split[0];
378
379 if (mergedLicenses.containsKey(mainLicense)) {
380
381
382
383 throw new MojoFailureException(
384 "The merge main license "
385 + mainLicense
386 + " was already registred in the "
387 + "configuration, please use only one such entry as describe in example "
388 + "http://mojo.codehaus.org/license-maven-plugin/examples/example-thirdparty.html#Merge_licenses.");
389 }
390 mergedLicenses.put(mainLicense, split);
391 }
392
393
394
395 for (String[] mergedLicense : mergedLicenses.values()) {
396 if (isVerbose()) {
397 getLog().info("Will merge " + Arrays.toString(mergedLicense) + "");
398 }
399
400 thirdPartyTool.mergeLicenses(licenseMap, mergedLicense);
401 }
402 }
403 }
404
405 protected LicenseMap createLicenseMap(SortedMap<String, MavenProject> dependencies) {
406
407 LicenseMap licenseMap = new LicenseMap();
408
409 for (MavenProject project : dependencies.values()) {
410 thirdPartyTool.addLicense(licenseMap, project, project.getLicenses());
411 }
412 return licenseMap;
413 }
414
415 protected boolean checkUnsafeDependencies() {
416 SortedSet<MavenProject> unsafeDependencies = getUnsafeDependencies();
417 boolean unsafe = !CollectionUtils.isEmpty(unsafeDependencies);
418 if (unsafe) {
419 Log log = getLog();
420 log.debug("There is " + unsafeDependencies.size() + " dependencies with no license :");
421 for (MavenProject dep : unsafeDependencies) {
422
423
424 log.debug(" - " + MojoHelper.getArtifactId(dep.getArtifact()));
425 }
426 }
427 return unsafe;
428 }
429
430 protected void writeThirdPartyFile() throws IOException {
431
432 Log log = getLog();
433 LicenseMap licenseMap = getLicenseMap();
434 File target = getThirdPartyFile();
435
436 if (isDoGenerate()) {
437 StringBuilder sb = new StringBuilder();
438 if (licenseMap.isEmpty()) {
439 sb.append(NO_DEPENDENCIES_MESSAGE);
440 } else {
441 if (isGroupByLicense()) {
442
443
444 sb.append("List of third-party dependencies grouped by " + "their license type.");
445 for (String licenseName : licenseMap.keySet()) {
446 SortedSet<MavenProject> projects = licenseMap.get(licenseName);
447
448
449 if (projects == null || projects.size() == 0) {
450 continue;
451 }
452
453 sb.append("\n\n").append(licenseName).append(" : ");
454
455 for (MavenProject mavenProject : projects) {
456 String s = MojoHelper.getArtifactName(mavenProject);
457 sb.append("\n * ").append(s);
458 }
459 }
460
461 } else {
462
463
464 SortedMap<MavenProject, String[]> map = licenseMap.toDependencyMap();
465
466 sb.append("List of ").append(map.size()).append(" third-party dependencies.\n");
467
468 List<String> lines = new ArrayList<String>();
469
470 for (Map.Entry<MavenProject, String[]> entry : map.entrySet()) {
471 String artifact = MojoHelper.getArtifactName(entry.getKey());
472 StringBuilder buffer = new StringBuilder();
473 for (String license : entry.getValue()) {
474 buffer.append(" (").append(license).append(")");
475 }
476 String licenses = buffer.toString();
477 String line = licenses + " " + artifact;
478 lines.add(line);
479 }
480
481 Collections.sort(lines);
482 for (String line : lines) {
483 sb.append('\n').append(line);
484 }
485 lines.clear();
486 }
487 }
488 String content = sb.toString();
489
490 log.info("Writing third-party file to " + target);
491 if (isVerbose()) {
492 log.info(content);
493 }
494
495 FileUtil.writeString(target, content, getEncoding());
496 }
497
498 if (isDoGenerateBundle()) {
499
500
501 File bundleTarget = FileUtil.getFile(getOutputDirectory(), getBundleThirdPartyPath());
502 log.info("Writing bundled third-party file to " + bundleTarget);
503 FileUtil.copyFile(target, bundleTarget);
504 }
505 }
506
507 public boolean isGroupByLicense() {
508 return groupByLicense;
509 }
510
511 public void setGroupByLicense(boolean groupByLicense) {
512 this.groupByLicense = groupByLicense;
513 }
514
515 public File getOutputDirectory() {
516 return outputDirectory;
517 }
518
519 public String getThirdPartyFilename() {
520 return thirdPartyFilename;
521 }
522
523 public String getBundleThirdPartyPath() {
524 return bundleThirdPartyPath;
525 }
526
527 public boolean isGenerateBundle() {
528 return generateBundle;
529 }
530
531 public boolean isFailIfWarning() {
532 return failIfWarning;
533 }
534
535 public SortedMap<String, MavenProject> getProjectDependencies() {
536 return projectDependencies;
537 }
538
539 public SortedSet<MavenProject> getUnsafeDependencies() {
540 return unsafeDependencies;
541 }
542
543 public void setUnsafeDependencies(SortedSet<MavenProject> unsafeDependencies) {
544 this.unsafeDependencies = unsafeDependencies;
545 }
546
547 public File getThirdPartyFile() {
548 return thirdPartyFile;
549 }
550
551 public LicenseMap getLicenseMap() {
552 return licenseMap;
553 }
554
555 public void setOutputDirectory(File outputDirectory) {
556 this.outputDirectory = outputDirectory;
557 }
558
559 public void setThirdPartyFilename(String thirdPartyFilename) {
560 this.thirdPartyFilename = thirdPartyFilename;
561 }
562
563 public void setBundleThirdPartyPath(String bundleThirdPartyPath) {
564 this.bundleThirdPartyPath = bundleThirdPartyPath;
565 }
566
567 public void setGenerateBundle(boolean generateBundle) {
568 this.generateBundle = generateBundle;
569 }
570
571 public void setThirdPartyFile(File thirdPartyFile) {
572 this.thirdPartyFile = thirdPartyFile;
573 }
574
575 public boolean isUseMissingFile() {
576 return useMissingFile;
577 }
578
579 public File getMissingFile() {
580 return missingFile;
581 }
582
583 public void setUseMissingFile(boolean useMissingFile) {
584 this.useMissingFile = useMissingFile;
585 }
586
587 public void setMissingFile(File missingFile) {
588 this.missingFile = missingFile;
589 }
590
591 public void setFailIfWarning(boolean failIfWarning) {
592 this.failIfWarning = failIfWarning;
593 }
594
595 public SortedProperties getUnsafeMappings() {
596 return unsafeMappings;
597 }
598
599 public boolean isForce() {
600 return force;
601 }
602
603 public boolean isDoGenerate() {
604 return doGenerate;
605 }
606
607 public void setForce(boolean force) {
608 this.force = force;
609 }
610
611 public void setDoGenerate(boolean doGenerate) {
612 this.doGenerate = doGenerate;
613 }
614
615 public boolean isDoGenerateBundle() {
616 return doGenerateBundle;
617 }
618
619 public void setDoGenerateBundle(boolean doGenerateBundle) {
620 this.doGenerateBundle = doGenerateBundle;
621 }
622
623 public List<String> getExcludedScopes() {
624 String[] split = excludedScopes == null ? new String[0] : excludedScopes.split(",");
625 return Arrays.asList(split);
626 }
627
628 public void setExcludedScopes(String excludedScopes) {
629 this.excludedScopes = excludedScopes;
630 }
631
632 public List<String> getIncludedScopes() {
633 String[] split = includedScopes == null ? new String[0] : includedScopes.split(",");
634 return Arrays.asList(split);
635 }
636
637 public void setIncludedScopes(String includedScopes) {
638 this.includedScopes = includedScopes;
639 }
640
641 public String getExcludedGroups() {
642 return excludedGroups;
643 }
644
645 public void setExcludedGroups(String excludedGroups) {
646 this.excludedGroups = excludedGroups;
647 }
648
649 public String getIncludedGroups() {
650 return includedGroups;
651 }
652
653 public void setIncludedGroups(String includedGroups) {
654 this.includedGroups = includedGroups;
655 }
656
657 public String getExcludedArtifacts() {
658 return excludedArtifacts;
659 }
660
661 public void setExcludedArtifacts(String excludedArtifacts) {
662 this.excludedArtifacts = excludedArtifacts;
663 }
664
665 public String getIncludedArtifacts() {
666 return includedArtifacts;
667 }
668
669 public void setIncludedArtifacts(String includedArtifacts) {
670 this.includedArtifacts = includedArtifacts;
671 }
672
673 public ThirdPartyTool getThirdPartyTool() {
674 return thirdPartyTool;
675 }
676
677 public void setThirdPartyTool(ThirdPartyTool thridPartyTool) {
678 this.thirdPartyTool = thridPartyTool;
679 }
680
681 public String getArtifactLicenseMapping() {
682 return artifactLicenseMapping;
683 }
684
685 public void setArtifactLicenseMapping(String artifactLicenseMapping) {
686 this.artifactLicenseMapping = artifactLicenseMapping;
687 }
688 }