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