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