View Javadoc
1   package org.apache.maven.model.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import org.apache.maven.model.Build;
26  import org.apache.maven.model.InputLocation;
27  import org.apache.maven.model.InputSource;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.model.PluginManagement;
31  import org.apache.maven.model.ReportPlugin;
32  import org.apache.maven.model.ReportSet;
33  import org.apache.maven.model.Reporting;
34  import org.apache.maven.model.building.ModelBuildingRequest;
35  import org.apache.maven.model.building.ModelProblemCollector;
36  import org.apache.maven.model.building.ModelProblemCollectorRequest;
37  import org.apache.maven.model.building.ModelProblem.Severity;
38  import org.apache.maven.model.building.ModelProblem.Version;
39  import org.codehaus.plexus.util.StringUtils;
40  import org.codehaus.plexus.util.xml.Xpp3Dom;
41  
42  /**
43   * Handles conversion of the <code>&lt;reporting&gt;</code> section into the configuration of Maven Site Plugin 3.x,
44   * i.e. <code>reportPlugins</code> and <code>outputDirectory</code> parameters.
45   *
46   * @author Benjamin Bentmann
47   */
48  @Named
49  @Singleton
50  public class DefaultReportingConverter
51      implements ReportingConverter
52  {
53      private final InputLocation location;
54      {
55          String modelId = "org.apache.maven:maven-model-builder:"
56              + this.getClass().getPackage().getImplementationVersion() + ":reporting-converter";
57          InputSource inputSource = new InputSource();
58          inputSource.setModelId( modelId );
59          location = new InputLocation( -1, -1, inputSource );
60          location.setLocation( 0, location );
61      }
62  
63      @Override
64      public void convertReporting( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
65      {
66          Reporting reporting = model.getReporting();
67  
68          if ( reporting == null )
69          {
70              return;
71          }
72  
73          Build build = model.getBuild();
74  
75          if ( build == null )
76          {
77              build = new Build();
78              model.setBuild( build );
79              model.setLocation( "build", location );
80          }
81  
82          Plugin sitePlugin = findSitePlugin( build );
83  
84          if ( sitePlugin == null )
85          {
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              {
92                  pluginManagement = new PluginManagement();
93                  build.setPluginManagement( pluginManagement );
94              }
95              pluginManagement.addPlugin( sitePlugin );
96          }
97  
98          Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration();
99  
100         if ( configuration == null )
101         {
102             configuration = new Xpp3Dom( "configuration", location );
103             sitePlugin.setConfiguration( configuration );
104         }
105 
106         Xpp3Dom reportPlugins = configuration.getChild( "reportPlugins" );
107 
108         if ( reportPlugins != null )
109         {
110             // new-style report configuration already present: warn since this new style has been deprecated
111             // in favor of classical reporting section MSITE-647 / MSITE-684
112             problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.BASE )
113                     .setMessage( "Reporting configuration should be done in <reporting> section, "
114                           + "not in maven-site-plugin <configuration> as reportPlugins parameter." )
115                     .setLocation( sitePlugin.getLocation( "configuration" ) ) );
116             return;
117         }
118 
119         if ( configuration.getChild( "outputDirectory" ) == null )
120         {
121             addDom( configuration, "outputDirectory", reporting.getOutputDirectory(),
122                     reporting.getLocation( "outputDirectory" ) );
123         }
124 
125         reportPlugins = new Xpp3Dom( "reportPlugins", location );
126         configuration.addChild( reportPlugins );
127 
128         boolean hasMavenProjectInfoReportsPlugin = false;
129 
130         /* waiting for MSITE-484 before deprecating <reporting> section
131         if ( !reporting.getPlugins().isEmpty()
132             && request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1 )
133         {
134 
135             problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.V31 )
136                     .setMessage( "The <reporting> section is deprecated, please move the reports to the <configuration>"
137                                  + " section of the new Maven Site Plugin." )
138                     .setLocation( reporting.getLocation( "" ) ) );
139         }*/
140 
141         for ( ReportPlugin plugin : reporting.getPlugins() )
142         {
143             Xpp3Dom reportPlugin = convert( plugin );
144             reportPlugins.addChild( reportPlugin );
145 
146             if ( !reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin
147                 && "org.apache.maven.plugins".equals( plugin.getGroupId() )
148                 && "maven-project-info-reports-plugin".equals( plugin.getArtifactId() ) )
149             {
150                 hasMavenProjectInfoReportsPlugin = true;
151             }
152         }
153 
154         if ( !reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin )
155         {
156             Xpp3Dom dom = new Xpp3Dom( "reportPlugin", location );
157 
158             addDom( dom, "groupId", "org.apache.maven.plugins" );
159             addDom( dom, "artifactId", "maven-project-info-reports-plugin" );
160 
161             reportPlugins.addChild( dom );
162         }
163     }
164 
165     private Plugin findSitePlugin( Build build )
166     {
167         for ( Plugin plugin : build.getPlugins() )
168         {
169             if ( isSitePlugin( plugin ) )
170             {
171                 return plugin;
172             }
173         }
174 
175         PluginManagement pluginManagement = build.getPluginManagement();
176         if ( pluginManagement != null )
177         {
178             for ( Plugin plugin : pluginManagement.getPlugins() )
179             {
180                 if ( isSitePlugin( plugin ) )
181                 {
182                     return plugin;
183                 }
184             }
185         }
186 
187         return null;
188     }
189 
190     private boolean isSitePlugin( Plugin plugin )
191     {
192         return "maven-site-plugin".equals( plugin.getArtifactId() )
193             && "org.apache.maven.plugins".equals( plugin.getGroupId() );
194     }
195 
196     private Xpp3Dom convert( ReportPlugin plugin )
197     {
198         Xpp3Dom dom = new Xpp3Dom( "reportPlugin", plugin.getLocation( "" ) );
199 
200         addDom( dom, "groupId", plugin.getGroupId(), plugin.getLocation( "groupId" ) );
201         addDom( dom, "artifactId", plugin.getArtifactId(), plugin.getLocation( "artifactId" ) );
202         addDom( dom, "version", plugin.getVersion(), plugin.getLocation( "version" ) );
203 
204         Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
205         if ( configuration != null )
206         {
207             configuration = new Xpp3Dom( configuration );
208             dom.addChild( configuration );
209         }
210 
211         if ( !plugin.getReportSets().isEmpty() )
212         {
213             Xpp3Dom reportSets = new Xpp3Dom( "reportSets", plugin.getLocation( "reportSets" ) );
214             for ( ReportSet reportSet : plugin.getReportSets() )
215             {
216                 Xpp3Dom rs = convert( reportSet );
217                 reportSets.addChild( rs );
218             }
219             dom.addChild( reportSets );
220         }
221 
222         return dom;
223     }
224 
225     private Xpp3Dom convert( ReportSet reportSet )
226     {
227         Xpp3Dom dom = new Xpp3Dom( "reportSet", reportSet.getLocation( "" ) );
228 
229         InputLocation idLocation = reportSet.getLocation( "id" );
230         addDom( dom, "id", reportSet.getId(), idLocation == null ? location : idLocation );
231 
232         Xpp3Dom configuration = (Xpp3Dom) reportSet.getConfiguration();
233         if ( configuration != null )
234         {
235             configuration = new Xpp3Dom( configuration );
236             dom.addChild( configuration );
237         }
238 
239         if ( !reportSet.getReports().isEmpty() )
240         {
241             InputLocation location = reportSet.getLocation( "reports" );
242             Xpp3Dom reports = new Xpp3Dom( "reports", location );
243             int n = 0;
244             for ( String report : reportSet.getReports() )
245             {
246                 addDom( reports, "report", report, ( location == null ) ? null : location.getLocation( n++ ) );
247             }
248             dom.addChild( reports );
249         }
250 
251         return dom;
252     }
253 
254     private void addDom( Xpp3Dom parent, String childName, String childValue )
255     {
256         addDom( parent, childName, childValue, location );
257     }
258 
259     private void addDom( Xpp3Dom parent, String childName, String childValue, InputLocation location )
260     {
261         if ( StringUtils.isNotEmpty( childValue ) )
262         {
263             parent.addChild( newDom( childName, childValue, location ) );
264         }
265     }
266 
267     private Xpp3Dom newDom( String name, String value, InputLocation location )
268     {
269         Xpp3Dom dom = new Xpp3Dom( name, location );
270         dom.setValue( value );
271         return dom;
272     }
273 
274 }