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