1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.function.Function;
24
25 import org.apache.maven.api.di.Named;
26 import org.apache.maven.api.di.Singleton;
27 import org.apache.maven.api.model.Build;
28 import org.apache.maven.api.model.Model;
29 import org.apache.maven.api.model.Plugin;
30 import org.apache.maven.api.model.PluginManagement;
31 import org.apache.maven.api.model.ReportPlugin;
32 import org.apache.maven.api.model.Reporting;
33 import org.apache.maven.api.services.ModelBuilderRequest;
34 import org.apache.maven.api.services.ModelProblemCollector;
35 import org.apache.maven.api.services.model.PluginConfigurationExpander;
36 import org.apache.maven.api.xml.XmlNode;
37
38
39
40
41
42 @Named
43 @Singleton
44 public class DefaultPluginConfigurationExpander implements PluginConfigurationExpander {
45
46 @Override
47 public Model expandPluginConfiguration(Model model, ModelBuilderRequest request, ModelProblemCollector problems) {
48 Build build = model.getBuild();
49 if (build != null) {
50 build = build.withPlugins(expandPlugin(build.getPlugins()));
51 PluginManagement pluginManagement = build.getPluginManagement();
52 if (pluginManagement != null) {
53 build = build.withPluginManagement(
54 pluginManagement.withPlugins(expandPlugin(pluginManagement.getPlugins())));
55 }
56 model = model.withBuild(build);
57 }
58 Reporting reporting = model.getReporting();
59 if (reporting != null) {
60 expandReport(reporting.getPlugins());
61 }
62 return model.withBuild(build);
63 }
64
65 private List<Plugin> expandPlugin(List<Plugin> oldPlugins) {
66 return map(oldPlugins, plugin -> {
67 XmlNode pluginConfiguration = plugin.getConfiguration();
68 if (pluginConfiguration != null) {
69 return plugin.withExecutions(map(
70 plugin.getExecutions(),
71 execution -> execution.withConfiguration(
72 XmlNode.merge(execution.getConfiguration(), pluginConfiguration))));
73 } else {
74 return plugin;
75 }
76 });
77 }
78
79 private List<ReportPlugin> expandReport(List<ReportPlugin> oldPlugins) {
80 return map(oldPlugins, plugin -> {
81 XmlNode pluginConfiguration = plugin.getConfiguration();
82 if (pluginConfiguration != null) {
83 return plugin.withReportSets(map(
84 plugin.getReportSets(),
85 report -> report.withConfiguration(
86 XmlNode.merge(report.getConfiguration(), pluginConfiguration))));
87 } else {
88 return plugin;
89 }
90 });
91 }
92
93 static <T> List<T> map(List<T> list, Function<T, T> mapper) {
94 List<T> newList = list;
95 for (int i = 0; i < newList.size(); i++) {
96 T oldT = newList.get(i);
97 T newT = mapper.apply(oldT);
98 if (newT != oldT) {
99 if (newList == list) {
100 newList = new ArrayList<>(list);
101 }
102 newList.set(i, newT);
103 }
104 }
105 return newList;
106 }
107 }