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