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 @Deprecated
50 public class DefaultReportingConverter implements ReportingConverter {
51 private final InputLocation location;
52
53 {
54 String modelId =
55 "org.apache.maven:maven-model-builder:" + getImplementationVersion(getClass()) + ":reporting-converter";
56 InputSource inputSource = new InputSource();
57 inputSource.setModelId(modelId);
58 location = new InputLocation(-1, -1, inputSource);
59 location.setLocation(0, location);
60 }
61
62 static String getImplementationVersion(Class<?> clazz) {
63 Package packageInfo = clazz.getPackage();
64 return packageInfo != null ? packageInfo.getImplementationVersion() : null;
65 }
66
67 @Override
68 public void convertReporting(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
69 Reporting reporting = model.getReporting();
70
71 if (reporting == null) {
72 return;
73 }
74
75 Build build = model.getBuild();
76
77 if (build == null) {
78 build = new Build();
79 model.setBuild(build);
80 model.setLocation("build", location);
81 }
82
83 Plugin sitePlugin = findSitePlugin(build);
84
85 if (sitePlugin == null) {
86 sitePlugin = new Plugin();
87 sitePlugin.setArtifactId("maven-site-plugin");
88 sitePlugin.setLocation("artifactId", location);
89 PluginManagement pluginManagement = build.getPluginManagement();
90 if (pluginManagement == null) {
91 pluginManagement = new PluginManagement();
92 build.setPluginManagement(pluginManagement);
93 }
94 pluginManagement.addPlugin(sitePlugin);
95 }
96
97 Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration();
98
99 if (configuration == null) {
100 configuration = new Xpp3Dom("configuration", location);
101 sitePlugin.setConfiguration(configuration);
102 }
103
104 Xpp3Dom reportPlugins = configuration.getChild("reportPlugins");
105
106 if (reportPlugins != null) {
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 addDom(
118 configuration,
119 "outputDirectory",
120 reporting.getOutputDirectory(),
121 reporting.getLocation("outputDirectory"));
122 }
123
124 reportPlugins = new Xpp3Dom("reportPlugins", location);
125 configuration.addChild(reportPlugins);
126
127 boolean hasMavenProjectInfoReportsPlugin = false;
128
129
130
131
132
133
134
135
136
137
138
139
140 for (ReportPlugin plugin : reporting.getPlugins()) {
141 Xpp3Dom reportPlugin = convert(plugin);
142 reportPlugins.addChild(reportPlugin);
143
144 if (!reporting.isExcludeDefaults()
145 && !hasMavenProjectInfoReportsPlugin
146 && "org.apache.maven.plugins".equals(plugin.getGroupId())
147 && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
148 hasMavenProjectInfoReportsPlugin = true;
149 }
150 }
151
152 if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
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 for (Plugin plugin : build.getPlugins()) {
164 if (isSitePlugin(plugin)) {
165 return plugin;
166 }
167 }
168
169 PluginManagement pluginManagement = build.getPluginManagement();
170 if (pluginManagement != null) {
171 for (Plugin plugin : pluginManagement.getPlugins()) {
172 if (isSitePlugin(plugin)) {
173 return plugin;
174 }
175 }
176 }
177
178 return null;
179 }
180
181 private boolean isSitePlugin(Plugin plugin) {
182 return "maven-site-plugin".equals(plugin.getArtifactId())
183 && "org.apache.maven.plugins".equals(plugin.getGroupId());
184 }
185
186 private Xpp3Dom convert(ReportPlugin plugin) {
187 Xpp3Dom dom = new Xpp3Dom("reportPlugin", plugin.getLocation(""));
188
189 addDom(dom, "groupId", plugin.getGroupId(), plugin.getLocation("groupId"));
190 addDom(dom, "artifactId", plugin.getArtifactId(), plugin.getLocation("artifactId"));
191 addDom(dom, "version", plugin.getVersion(), plugin.getLocation("version"));
192
193 Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
194 if (configuration != null) {
195 configuration = new Xpp3Dom(configuration);
196 dom.addChild(configuration);
197 }
198
199 if (!plugin.getReportSets().isEmpty()) {
200 Xpp3Dom reportSets = new Xpp3Dom("reportSets", plugin.getLocation("reportSets"));
201 for (ReportSet reportSet : plugin.getReportSets()) {
202 Xpp3Dom rs = convert(reportSet);
203 reportSets.addChild(rs);
204 }
205 dom.addChild(reportSets);
206 }
207
208 return dom;
209 }
210
211 private Xpp3Dom convert(ReportSet reportSet) {
212 Xpp3Dom dom = new Xpp3Dom("reportSet", reportSet.getLocation(""));
213
214 InputLocation idLocation = reportSet.getLocation("id");
215 addDom(dom, "id", reportSet.getId(), idLocation == null ? location : idLocation);
216
217 Xpp3Dom configuration = (Xpp3Dom) reportSet.getConfiguration();
218 if (configuration != null) {
219 configuration = new Xpp3Dom(configuration);
220 dom.addChild(configuration);
221 }
222
223 if (!reportSet.getReports().isEmpty()) {
224 InputLocation location = reportSet.getLocation("reports");
225 Xpp3Dom reports = new Xpp3Dom("reports", location);
226 int n = 0;
227 for (String report : reportSet.getReports()) {
228 addDom(reports, "report", report, (location == null) ? null : location.getLocation(n++));
229 }
230 dom.addChild(reports);
231 }
232
233 return dom;
234 }
235
236 private void addDom(Xpp3Dom parent, String childName, String childValue) {
237 addDom(parent, childName, childValue, location);
238 }
239
240 private void addDom(Xpp3Dom parent, String childName, String childValue, InputLocation location) {
241 if (StringUtils.isNotEmpty(childValue)) {
242 parent.addChild(newDom(childName, childValue, location));
243 }
244 }
245
246 private Xpp3Dom newDom(String name, String value, InputLocation location) {
247 Xpp3Dom dom = new Xpp3Dom(name, location);
248 dom.setValue(value);
249 return dom;
250 }
251 }