001package org.apache.maven.model.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.model.Build;
023import org.apache.maven.model.Model;
024import org.apache.maven.model.Plugin;
025import org.apache.maven.model.PluginManagement;
026import org.apache.maven.model.ReportPlugin;
027import org.apache.maven.model.ReportSet;
028import org.apache.maven.model.Reporting;
029import org.apache.maven.model.building.ModelBuildingRequest;
030import org.apache.maven.model.building.ModelProblemCollector;
031import org.codehaus.plexus.component.annotations.Component;
032import org.codehaus.plexus.util.StringUtils;
033import org.codehaus.plexus.util.xml.Xpp3Dom;
034
035/**
036 * Handles conversion of the legacy reporting section into the configuration of the new Maven Site Plugin.
037 *
038 * @author Benjamin Bentmann
039 */
040@Component( role = ReportingConverter.class )
041public class DefaultReportingConverter
042    implements ReportingConverter
043{
044
045    @Override
046    public void convertReporting( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
047    {
048        Reporting reporting = model.getReporting();
049
050        if ( reporting == null )
051        {
052            return;
053        }
054
055        Build build = model.getBuild();
056
057        if ( build == null )
058        {
059            build = new Build();
060            model.setBuild( build );
061        }
062
063        Plugin sitePlugin = findSitePlugin( build );
064
065        if ( sitePlugin == null )
066        {
067            sitePlugin = new Plugin();
068            sitePlugin.setArtifactId( "maven-site-plugin" );
069            PluginManagement pluginManagement = build.getPluginManagement();
070            if ( pluginManagement == null )
071            {
072                pluginManagement = new PluginManagement();
073                build.setPluginManagement( pluginManagement );
074            }
075            pluginManagement.addPlugin( sitePlugin );
076        }
077
078        Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration();
079
080        if ( configuration == null )
081        {
082            configuration = new Xpp3Dom( "configuration" );
083            sitePlugin.setConfiguration( configuration );
084        }
085
086        Xpp3Dom reportPlugins = configuration.getChild( "reportPlugins" );
087
088        if ( reportPlugins != null )
089        {
090            // new-style report configuration already present, assume user handled entire conversion
091            return;
092        }
093
094        if ( configuration.getChild( "outputDirectory" ) == null )
095        {
096            addDom( configuration, "outputDirectory", reporting.getOutputDirectory() );
097        }
098
099        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}