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 = "org.apache.maven:maven-model-builder:"
55 + this.getClass().getPackage().getImplementationVersion() + ":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 @Override
63 public void convertReporting(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
64 Reporting reporting = model.getReporting();
65
66 if (reporting == null) {
67 return;
68 }
69
70 Build build = model.getBuild();
71
72 if (build == null) {
73 build = new Build();
74 model.setBuild(build);
75 model.setLocation("build", location);
76 }
77
78 Plugin sitePlugin = findSitePlugin(build);
79
80 if (sitePlugin == null) {
81 sitePlugin = new Plugin();
82 sitePlugin.setArtifactId("maven-site-plugin");
83 sitePlugin.setLocation("artifactId", location);
84 PluginManagement pluginManagement = build.getPluginManagement();
85 if (pluginManagement == null) {
86 pluginManagement = new PluginManagement();
87 build.setPluginManagement(pluginManagement);
88 }
89 pluginManagement.addPlugin(sitePlugin);
90 }
91
92 Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration();
93
94 if (configuration == null) {
95 configuration = new Xpp3Dom("configuration", location);
96 sitePlugin.setConfiguration(configuration);
97 }
98
99 Xpp3Dom reportPlugins = configuration.getChild("reportPlugins");
100
101 if (reportPlugins != null) {
102
103
104 problems.add(new ModelProblemCollectorRequest(Severity.WARNING, Version.BASE)
105 .setMessage("Reporting configuration should be done in <reporting> section, "
106 + "not in maven-site-plugin <configuration> as reportPlugins parameter.")
107 .setLocation(sitePlugin.getLocation("configuration")));
108 return;
109 }
110
111 if (configuration.getChild("outputDirectory") == null) {
112 addDom(
113 configuration,
114 "outputDirectory",
115 reporting.getOutputDirectory(),
116 reporting.getLocation("outputDirectory"));
117 }
118
119 reportPlugins = new Xpp3Dom("reportPlugins", location);
120 configuration.addChild(reportPlugins);
121
122 boolean hasMavenProjectInfoReportsPlugin = false;
123
124
125
126
127
128
129
130
131
132
133
134
135 for (ReportPlugin plugin : reporting.getPlugins()) {
136 Xpp3Dom reportPlugin = convert(plugin);
137 reportPlugins.addChild(reportPlugin);
138
139 if (!reporting.isExcludeDefaults()
140 && !hasMavenProjectInfoReportsPlugin
141 && "org.apache.maven.plugins".equals(plugin.getGroupId())
142 && "maven-project-info-reports-plugin".equals(plugin.getArtifactId())) {
143 hasMavenProjectInfoReportsPlugin = true;
144 }
145 }
146
147 if (!reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin) {
148 Xpp3Dom dom = new Xpp3Dom("reportPlugin", location);
149
150 addDom(dom, "groupId", "org.apache.maven.plugins");
151 addDom(dom, "artifactId", "maven-project-info-reports-plugin");
152
153 reportPlugins.addChild(dom);
154 }
155 }
156
157 private Plugin findSitePlugin(Build build) {
158 for (Plugin plugin : build.getPlugins()) {
159 if (isSitePlugin(plugin)) {
160 return plugin;
161 }
162 }
163
164 PluginManagement pluginManagement = build.getPluginManagement();
165 if (pluginManagement != null) {
166 for (Plugin plugin : pluginManagement.getPlugins()) {
167 if (isSitePlugin(plugin)) {
168 return plugin;
169 }
170 }
171 }
172
173 return null;
174 }
175
176 private boolean isSitePlugin(Plugin plugin) {
177 return "maven-site-plugin".equals(plugin.getArtifactId())
178 && "org.apache.maven.plugins".equals(plugin.getGroupId());
179 }
180
181 private Xpp3Dom convert(ReportPlugin plugin) {
182 Xpp3Dom dom = new Xpp3Dom("reportPlugin", plugin.getLocation(""));
183
184 addDom(dom, "groupId", plugin.getGroupId(), plugin.getLocation("groupId"));
185 addDom(dom, "artifactId", plugin.getArtifactId(), plugin.getLocation("artifactId"));
186 addDom(dom, "version", plugin.getVersion(), plugin.getLocation("version"));
187
188 Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
189 if (configuration != null) {
190 configuration = new Xpp3Dom(configuration);
191 dom.addChild(configuration);
192 }
193
194 if (!plugin.getReportSets().isEmpty()) {
195 Xpp3Dom reportSets = new Xpp3Dom("reportSets", plugin.getLocation("reportSets"));
196 for (ReportSet reportSet : plugin.getReportSets()) {
197 Xpp3Dom rs = convert(reportSet);
198 reportSets.addChild(rs);
199 }
200 dom.addChild(reportSets);
201 }
202
203 return dom;
204 }
205
206 private Xpp3Dom convert(ReportSet reportSet) {
207 Xpp3Dom dom = new Xpp3Dom("reportSet", reportSet.getLocation(""));
208
209 InputLocation idLocation = reportSet.getLocation("id");
210 addDom(dom, "id", reportSet.getId(), idLocation == null ? location : idLocation);
211
212 Xpp3Dom configuration = (Xpp3Dom) reportSet.getConfiguration();
213 if (configuration != null) {
214 configuration = new Xpp3Dom(configuration);
215 dom.addChild(configuration);
216 }
217
218 if (!reportSet.getReports().isEmpty()) {
219 InputLocation location = reportSet.getLocation("reports");
220 Xpp3Dom reports = new Xpp3Dom("reports", location);
221 int n = 0;
222 for (String report : reportSet.getReports()) {
223 addDom(reports, "report", report, (location == null) ? null : location.getLocation(n++));
224 }
225 dom.addChild(reports);
226 }
227
228 return dom;
229 }
230
231 private void addDom(Xpp3Dom parent, String childName, String childValue) {
232 addDom(parent, childName, childValue, location);
233 }
234
235 private void addDom(Xpp3Dom parent, String childName, String childValue, InputLocation location) {
236 if (StringUtils.isNotEmpty(childValue)) {
237 parent.addChild(newDom(childName, childValue, location));
238 }
239 }
240
241 private Xpp3Dom newDom(String name, String value, InputLocation location) {
242 Xpp3Dom dom = new Xpp3Dom(name, location);
243 dom.setValue(value);
244 return dom;
245 }
246 }