1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.site.descriptor;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.StringWriter;
24 import java.io.Writer;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.doxia.site.SiteModel;
28 import org.apache.maven.doxia.site.io.xpp3.SiteXpp3Writer;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
34 import org.codehaus.plexus.util.xml.XMLWriter;
35 import org.codehaus.plexus.util.xml.XmlStreamWriter;
36 import org.codehaus.plexus.util.xml.XmlWriterUtil;
37
38
39
40
41
42
43
44
45
46 @Mojo(name = "effective-site", requiresReports = true)
47 public class EffectiveSiteMojo extends AbstractSiteDescriptorMojo {
48
49
50
51
52
53
54 @Parameter(property = "output")
55 protected File output;
56
57
58
59
60 @Override
61 public void execute() throws MojoExecutionException, MojoFailureException {
62 SiteModel siteModel = prepareSiteModel(getLocales().get(0));
63
64 StringWriter w = new StringWriter();
65 XMLWriter writer = new PrettyPrintXMLWriter(
66 w, StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), siteModel.getModelEncoding(), null);
67
68 writeHeader(writer);
69
70 writeEffectiveSite(siteModel, writer);
71
72 String effectiveSite = w.toString();
73
74 if (output != null) {
75 try {
76 writeXmlFile(output, effectiveSite);
77 } catch (IOException e) {
78 throw new MojoExecutionException("Cannot write effective site descriptor to output: " + output, e);
79 }
80
81 if (getLog().isInfoEnabled()) {
82 getLog().info("Effective site descriptor written to: " + output);
83 }
84 } else {
85 StringBuilder message = new StringBuilder();
86
87 message.append("\nEffective site descriptor, after inheritance and interpolation:\n\n");
88 message.append(effectiveSite);
89 message.append("\n");
90
91 if (getLog().isInfoEnabled()) {
92 getLog().info(message.toString());
93 }
94 }
95 }
96
97
98
99
100
101
102 protected static void writeHeader(XMLWriter writer) {
103 XmlWriterUtil.writeCommentLineBreak(writer);
104 XmlWriterUtil.writeComment(writer, " ");
105 XmlWriterUtil.writeComment(writer, "Generated by Maven Site Plugin");
106 XmlWriterUtil.writeComment(writer, "See: https://maven.apache.org/plugins/maven-site-plugin/");
107 XmlWriterUtil.writeComment(writer, " ");
108 XmlWriterUtil.writeCommentLineBreak(writer);
109
110 XmlWriterUtil.writeLineBreak(writer);
111 }
112
113
114
115
116
117
118
119 protected static void writeComment(XMLWriter writer, String comment) {
120 XmlWriterUtil.writeCommentLineBreak(writer);
121 XmlWriterUtil.writeComment(writer, " ");
122 XmlWriterUtil.writeComment(writer, comment);
123 XmlWriterUtil.writeComment(writer, " ");
124 XmlWriterUtil.writeCommentLineBreak(writer);
125
126 XmlWriterUtil.writeLineBreak(writer);
127 }
128
129 private void writeEffectiveSite(SiteModel siteModel, XMLWriter writer) throws MojoExecutionException {
130 String effectiveSite;
131
132 StringWriter sWriter = new StringWriter();
133 SiteXpp3Writer siteWriter = new SiteXpp3Writer();
134 try {
135 siteWriter.write(sWriter, siteModel);
136 } catch (IOException e) {
137 throw new MojoExecutionException("Cannot serialize site descriptor to XML", e);
138 }
139
140 effectiveSite = sWriter.toString();
141
142 int xmlPrologStart = effectiveSite.indexOf("<?xml");
143 int xmlPrologEnd = effectiveSite.indexOf("?>", xmlPrologStart);
144 effectiveSite = effectiveSite.substring(xmlPrologEnd + 2).trim();
145
146 writeComment(writer, "Effective site descriptor for project \'" + project.getId() + "\'");
147
148 writer.writeMarkup(effectiveSite);
149 }
150
151 protected static void writeXmlFile(File output, String content) throws IOException {
152 try (Writer out = new XmlStreamWriter(output)) {
153 output.getParentFile().mkdirs();
154
155 out.write(content);
156 }
157 }
158 }