1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.repack;
16
17 import static java.lang.String.format;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.SortedMap;
25 import java.util.function.BiConsumer;
26
27 import com.google.common.base.Strings;
28 import com.google.common.collect.ImmutableSortedMap;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.shared.utils.logging.MessageBuilder;
31 import org.apache.maven.shared.utils.logging.MessageUtils;
32
33 final class Reporter {
34
35 private static final PluginLog LOG = new PluginLog(Reporter.class);
36
37 private static final Set<Artifact> runtimeUnpackedArtifacts = new HashSet<>();
38 private static final Set<Artifact> optionalArtifacts = new HashSet<>();
39 private static final Map<Artifact, String> excludedArtifacts = new HashMap<>();
40 private static final Set<Artifact> includedArtifacts = new HashSet<>();
41
42
43 static void addRuntimeUnpacked(Artifact artifact) {
44 runtimeUnpackedArtifacts.add(artifact);
45 }
46
47 static void addOptional(Artifact artifact) {
48 optionalArtifacts.add(artifact);
49 }
50
51 static void addExcluded(Artifact artifact, String reason) {
52 excludedArtifacts.put(artifact, reason);
53 }
54
55 static void addIncluded(Artifact artifact) {
56 includedArtifacts.add(artifact);
57 }
58
59 public static void report(boolean quiet, Artifact source, String classifier) {
60
61 LOG.report(quiet, "");
62 header(quiet, format(Locale.ROOT, "Summary Report for: %s:%s (%s)", source.getGroupId(), source.getArtifactId(), classifier));
63 LOG.report(quiet, "");
64
65 header(quiet, "Included dependencies (" + includedArtifacts.size() + ")");
66 logReport(quiet, includedArtifacts, (messageBuilder, artifact) -> messageBuilder.strong(artifact.getScope()));
67
68 header(quiet, "Included optional dependencies (" + optionalArtifacts.size() + ")");
69 logReport(quiet, optionalArtifacts, null);
70
71 header(quiet, "Excluded dependencies (" + excludedArtifacts.size() + ")");
72 logReport(quiet, excludedArtifacts.keySet(), (messageBuilder, artifact) -> messageBuilder.strong(excludedArtifacts.get(artifact)));
73
74 header(quiet, "Runtime unpacked dependencies (" + runtimeUnpackedArtifacts.size() + ")");
75 logReport(quiet, runtimeUnpackedArtifacts, null);
76 }
77
78 private static void header(boolean quiet, String value) {
79 LOG.report(quiet, value);
80 LOG.report(quiet, Strings.repeat("=", value.length()));
81 }
82
83 private static void logReport(boolean quiet, Set<Artifact> artifacts, BiConsumer<MessageBuilder, Artifact> consumer) {
84 if (!artifacts.isEmpty()) {
85 SortedMap<String, Artifact> names = computeNames(artifacts);
86 final int namePadding = names.keySet().stream().map(String::length).reduce(0, Math::max);
87
88 for (Map.Entry<String, Artifact> entry : names.entrySet()) {
89 final MessageBuilder mb = MessageUtils.buffer();
90 if (consumer == null) {
91 mb.a(entry.getKey());
92 } else {
93 mb.a(Strings.padEnd(entry.getKey() + ':', namePadding + 2, ' '));
94 consumer.accept(mb, entry.getValue());
95 }
96 LOG.report(quiet, "%s", mb);
97 }
98 }
99 LOG.report(quiet, "");
100 }
101
102 private static SortedMap<String, Artifact> computeNames(Set<Artifact> artifacts) {
103 ImmutableSortedMap.Builder<String, Artifact> builder = ImmutableSortedMap.naturalOrder();
104 artifacts.forEach(a -> builder.put(computeArtifactName(a), a));
105 return builder.build();
106 }
107
108 private static String computeArtifactName(Artifact artifact) {
109 if (artifact.hasClassifier()) {
110 return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getClassifier();
111 } else {
112 return artifact.getGroupId() + ':' + artifact.getArtifactId();
113 }
114 }
115 }