View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Handles expansion of general build plugin configuration into individual executions.
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 }