View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
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(a.getGroupId() + ':' + a.getArtifactId(), a));
105         return builder.build();
106     }
107 }