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