001 package 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 022 import org.apache.maven.model.Build; 023 import org.apache.maven.model.Model; 024 import org.apache.maven.model.Plugin; 025 import org.apache.maven.model.PluginManagement; 026 import org.apache.maven.model.ReportPlugin; 027 import org.apache.maven.model.ReportSet; 028 import org.apache.maven.model.Reporting; 029 import org.apache.maven.model.building.ModelBuildingRequest; 030 import org.apache.maven.model.building.ModelProblem; 031 import org.apache.maven.model.building.ModelProblemCollector; 032 import org.apache.maven.model.building.ModelProblem.Severity; 033 import org.apache.maven.model.building.ModelProblem.Version; 034 import org.apache.maven.model.building.ModelProblemCollectorRequest; 035 import org.codehaus.plexus.component.annotations.Component; 036 import org.codehaus.plexus.util.StringUtils; 037 import org.codehaus.plexus.util.xml.Xpp3Dom; 038 039 /** 040 * Handles conversion of the legacy reporting section into the configuration of the new Maven Site Plugin. 041 * 042 * @author Benjamin Bentmann 043 */ 044 @Component( role = ReportingConverter.class ) 045 public class DefaultReportingConverter 046 implements ReportingConverter 047 { 048 049 public void convertReporting( Model model, ModelBuildingRequest request, ModelProblemCollector problems ) 050 { 051 Reporting reporting = model.getReporting(); 052 053 if ( reporting == null ) 054 { 055 return; 056 } 057 058 Build build = model.getBuild(); 059 060 if ( build == null ) 061 { 062 build = new Build(); 063 model.setBuild( build ); 064 } 065 066 Plugin sitePlugin = findSitePlugin( build ); 067 068 if ( sitePlugin == null ) 069 { 070 sitePlugin = new Plugin(); 071 sitePlugin.setArtifactId( "maven-site-plugin" ); 072 PluginManagement pluginManagement = build.getPluginManagement(); 073 if ( pluginManagement == null ) 074 { 075 pluginManagement = new PluginManagement(); 076 build.setPluginManagement( pluginManagement ); 077 } 078 pluginManagement.addPlugin( sitePlugin ); 079 } 080 081 Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration(); 082 083 if ( configuration == null ) 084 { 085 configuration = new Xpp3Dom( "configuration" ); 086 sitePlugin.setConfiguration( configuration ); 087 } 088 089 Xpp3Dom reportPlugins = configuration.getChild( "reportPlugins" ); 090 091 if ( reportPlugins != null ) 092 { 093 // new-style report configuration already present, assume user handled entire conversion 094 return; 095 } 096 097 if ( configuration.getChild( "outputDirectory" ) == null ) 098 { 099 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 }