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