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.model.plugin;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.apache.maven.model.Build;
25  import org.apache.maven.model.InputLocation;
26  import org.apache.maven.model.InputSource;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Plugin;
29  import org.apache.maven.model.PluginManagement;
30  import org.apache.maven.model.ReportPlugin;
31  import org.apache.maven.model.ReportSet;
32  import org.apache.maven.model.Reporting;
33  import org.apache.maven.model.building.ModelBuildingRequest;
34  import org.apache.maven.model.building.ModelProblem.Severity;
35  import org.apache.maven.model.building.ModelProblem.Version;
36  import org.apache.maven.model.building.ModelProblemCollector;
37  import org.apache.maven.model.building.ModelProblemCollectorRequest;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.codehaus.plexus.util.xml.Xpp3Dom;
40  
41  /**
42   * Handles conversion of the <code>&lt;reporting&gt;</code> section into the configuration of Maven Site Plugin 3.x,
43   * i.e. <code>reportPlugins</code> and <code>outputDirectory</code> parameters.
44   *
45   * @author Benjamin Bentmann
46   */
47  @Named
48  @Singleton
49  public class DefaultReportingConverter implements ReportingConverter {
50      private final InputLocation location;
51  
52      {
53          String modelId = "org.apache.maven:maven-model-builder:"
54                  + this.getClass().getPackage().getImplementationVersion() + ":reporting-converter";
55          InputSource inputSource = new InputSource();
56          inputSource.setModelId(modelId);
57          location = new InputLocation(-1, -1, inputSource);
58          location.setLocation(0, location);
59      }
60  
61      @Override
62      public void convertReporting(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
63          Reporting reporting = model.getReporting();
64  
65          if (reporting == null) {
66              return;
67          }
68  
69          Build build = model.getBuild();
70  
71          if (build == null) {
72              build = new Build();
73              model.setBuild(build);
74              model.setLocation("build", location);
75          }
76  
77          Plugin sitePlugin = findSitePlugin(build);
78  
79          if (sitePlugin == null) {
80              sitePlugin = new Plugin();
81              sitePlugin.setArtifactId("maven-site-plugin");
82              sitePlugin.setLocation("artifactId", location);
83              PluginManagement pluginManagement = build.getPluginManagement();
84              if (pluginManagement == null) {
85                  pluginManagement = new PluginManagement();
86                  build.setPluginManagement(pluginManagement);
87              }
88              pluginManagement.addPlugin(sitePlugin);
89          }
90  
91          Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration();
92  
93          if (configuration == null) {
94              configuration = new Xpp3Dom("configuration", location);
95              sitePlugin.setConfiguration(configuration);
96          }
97  
98          Xpp3Dom reportPlugins = configuration.getChild("reportPlugins");
99  
100         if (reportPlugins != null) {
101             // new-style report configuration already present: warn since this new style has been deprecated
102             // in favor of classical reporting section MSITE-647 / MSITE-684
103             problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.BASE)
104                     .setMessage("Reporting configuration should be done in <reporting> section, "
105                             + "not in maven-site-plugin <configuration> as reportPlugins parameter.")
106                     .setLocation(sitePlugin.getLocation("configuration")));
107             return;
108         }
109 
110         if (configuration.getChild("outputDirectory") == null) {
111             addDom(
112                     configuration,
113                     "outputDirectory",
114                     reporting.getOutputDirectory(),
115                     reporting.getLocation("outputDirectory"));
116         }
117 
118         reportPlugins = new Xpp3Dom("reportPlugins", location);
119         configuration.addChild(reportPlugins);
120 
121         boolean hasMavenProjectInfoReportsPlugin = false;
122 
123         /* waiting for MSITE-484 before deprecating <reporting> section
124         if ( !reporting.getPlugins().isEmpty()
125             && request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1 )
126         {
127 
128             problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.V31 )
129                     .setMessage( "The <reporting> section is deprecated, please move the reports to the <configuration>"
130                                  + " section of the new Maven Site Plugin." )
131                     .setLocation( reporting.getLocation( "" ) ) );
132         }*/
133 
134         for (ReportPlugin plugin : reporting.getPlugins()) {
135             Xpp3Dom reportPlugin = convert(plugin);
136             reportPlugins.addChild(reportPlugin);
137 
138             if (!reporting.isExcludeDefaults()
139                     && !hasMavenProjectInfoReportsPlugin
140                     && "org.apache.maven.plugins".equals(plugin.getGroupId())
141                     && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
142                 hasMavenProjectInfoReportsPlugin = true;
143             }
144         }
145 
146         if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
147             Xpp3Dom dom = new Xpp3Dom("reportPlugin", location);
148 
149             addDom(dom, "groupId", "org.apache.maven.plugins");
150             addDom(dom, "artifactId", "maven-project-info-reports-plugin");
151 
152             reportPlugins.addChild(dom);
153         }
154     }
155 
156     private Plugin findSitePlugin(Build build) {
157         for (Plugin plugin : build.getPlugins()) {
158             if (isSitePlugin(plugin)) {
159                 return plugin;
160             }
161         }
162 
163         PluginManagement pluginManagement = build.getPluginManagement();
164         if (pluginManagement != null) {
165             for (Plugin plugin : pluginManagement.getPlugins()) {
166                 if (isSitePlugin(plugin)) {
167                     return plugin;
168                 }
169             }
170         }
171 
172         return null;
173     }
174 
175     private boolean isSitePlugin(Plugin plugin) {
176         return "maven-site-plugin".equals(plugin.getArtifactId())
177                 && "org.apache.maven.plugins".equals(plugin.getGroupId());
178     }
179 
180     private Xpp3Dom convert(ReportPlugin plugin) {
181         Xpp3Dom dom = new Xpp3Dom("reportPlugin", plugin.getLocation(""));
182 
183         addDom(dom, "groupId", plugin.getGroupId(), plugin.getLocation("groupId"));
184         addDom(dom, "artifactId", plugin.getArtifactId(), plugin.getLocation("artifactId"));
185         addDom(dom, "version", plugin.getVersion(), plugin.getLocation("version"));
186 
187         Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
188         if (configuration != null) {
189             configuration = new Xpp3Dom(configuration);
190             dom.addChild(configuration);
191         }
192 
193         if (!plugin.getReportSets().isEmpty()) {
194             Xpp3Dom reportSets = new Xpp3Dom("reportSets", plugin.getLocation("reportSets"));
195             for (ReportSet reportSet : plugin.getReportSets()) {
196                 Xpp3Dom rs = convert(reportSet);
197                 reportSets.addChild(rs);
198             }
199             dom.addChild(reportSets);
200         }
201 
202         return dom;
203     }
204 
205     private Xpp3Dom convert(ReportSet reportSet) {
206         Xpp3Dom dom = new Xpp3Dom("reportSet", reportSet.getLocation(""));
207 
208         InputLocation idLocation = reportSet.getLocation("id");
209         addDom(dom, "id", reportSet.getId(), idLocation == null ? location : idLocation);
210 
211         Xpp3Dom configuration = (Xpp3Dom) reportSet.getConfiguration();
212         if (configuration != null) {
213             configuration = new Xpp3Dom(configuration);
214             dom.addChild(configuration);
215         }
216 
217         if (!reportSet.getReports().isEmpty()) {
218             InputLocation location = reportSet.getLocation("reports");
219             Xpp3Dom reports = new Xpp3Dom("reports", location);
220             int n = 0;
221             for (String report : reportSet.getReports()) {
222                 addDom(reports, "report", report, (location == null) ? null : location.getLocation(n++));
223             }
224             dom.addChild(reports);
225         }
226 
227         return dom;
228     }
229 
230     private void addDom(Xpp3Dom parent, String childName, String childValue) {
231         addDom(parent, childName, childValue, location);
232     }
233 
234     private void addDom(Xpp3Dom parent, String childName, String childValue, InputLocation location) {
235         if (StringUtils.isNotEmpty(childValue)) {
236             parent.addChild(newDom(childName, childValue, location));
237         }
238     }
239 
240     private Xpp3Dom newDom(String name, String value, InputLocation location) {
241         Xpp3Dom dom = new Xpp3Dom(name, location);
242         dom.setValue(value);
243         return dom;
244     }
245 }