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