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