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