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