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